Created
September 22, 2016 04:39
-
-
Save dmitrymatveev/1fc0d2b0c118bfd4d257cd9b494b097c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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