Skip to content

Instantly share code, notes, and snippets.

@dmitrymatveev
Created September 22, 2016 04:39
Show Gist options
  • Save dmitrymatveev/1fc0d2b0c118bfd4d257cd9b494b097c to your computer and use it in GitHub Desktop.
Save dmitrymatveev/1fc0d2b0c118bfd4d257cd9b494b097c to your computer and use it in GitHub Desktop.
"use strict";
class Signal {
constructor() {
this._callbacks = new Map();
}
add(fnc) {
this._callbacks.set(fnc, {count:0});
return fnc;
}
once(fnc) {
this._callbacks.set(fnc, {count:1});
return fnc;
}
times(count, fnc) {
this._callbacks.set(fnc, {count});
return fnc;
}
dispatch() {
for(let entry of this._callbacks) {
entry[0]();
if (--entry[1].count === 0) {
this._callbacks.delete(entry[0]);
}
}
}
remove(fnc) {
this._callbacks.delete(fnc);
}
removeAll() {
this._callbacks.clear();
}
}
window.dm = window.dm || {};
dm.Signal = Signal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment