Initial commit
This commit is contained in:
38
com.pdma.notion-timer.sdPlugin/ui/libs/events.js
Normal file
38
com.pdma.notion-timer.sdPlugin/ui/libs/events.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/** ELGEvents
|
||||
* Publish/Subscribe pattern to quickly signal events to
|
||||
* the plugin, property inspector and data.
|
||||
*/
|
||||
|
||||
const ELGEvents = {
|
||||
eventEmitter: function (name, fn) {
|
||||
const eventList = new Map();
|
||||
|
||||
const on = (name, fn) => {
|
||||
if (!eventList.has(name)) eventList.set(name, ELGEvents.pubSub());
|
||||
|
||||
return eventList.get(name).sub(fn);
|
||||
};
|
||||
|
||||
const has = name => eventList.has(name);
|
||||
|
||||
const emit = (name, data) => eventList.has(name) && eventList.get(name).pub(data);
|
||||
|
||||
return Object.freeze({on, has, emit, eventList});
|
||||
},
|
||||
|
||||
pubSub: function pubSub() {
|
||||
const subscribers = new Set();
|
||||
|
||||
const sub = fn => {
|
||||
subscribers.add(fn);
|
||||
return () => {
|
||||
subscribers.delete(fn);
|
||||
};
|
||||
};
|
||||
|
||||
const pub = data => subscribers.forEach(fn => fn(data));
|
||||
return Object.freeze({pub, sub});
|
||||
}
|
||||
};
|
||||
|
||||
const EventEmitter = ELGEvents.eventEmitter();
|
||||
Reference in New Issue
Block a user