by
21
8
4,134
1
Top 1% !
Popular
Famous
Nice
Useful
Easy-to-find
Specified
MultiPlatform
Popularity: 2384th place
Languagejavascript
LicenseMIT_X11

More EveNtIng MaDnEsS!

A super fast and reliable eventbroker for the browser or server. Made it with Luv!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/223889/More-EveNtIng-MaDnEsS!?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
(function () { function Signals () {} var exports = this, signals = Signals.prototype; function isAry(obj) { return (obj.constructor === Array); } signals.wen = function (nam, func) { if (!this.sigsMap) { this.sigsMap = {}; } if (!this.sigsMap[nam]) { this.sigsMap[nam] = func; } else if (isAry(this.sigsMap[nam])) { this.sigsMap[nam].push(func); } else { this.sigsMap[nam] = [this.sigsMap[nam], func]; } return this; }; signals.signaled = function(nam, func) { return this.wen(nam, function(evnt){ return func(evnt); }); }; signals.removeReceivers = function (nam) { if (!nam) { this.sigsMap = {}; return this; } if (this.sigsMap && this.sigsMap[nam]) { this.sigsMap[nam] = null; } return this; }; signals.receiverObjects = function (nam) { var recvrLst = this.receivers(nam), rspns; if (isAry(recvrLst)) { rspns = {}; rspns[nam] = recvrLst; } return rspns || recvrLst; }; signals.receivers = function (nam) { if (!this.sigsMap) { this.sigsMap = {}; } if (!this.sigsMap[nam]) { this.sigsMap[nam] = []; } if (!isAry(this.sigsMap[nam])) { this.sigsMap[nam] = [this.sigsMap[nam]]; } return this.sigsMap[nam]; }; signals.addReceiver = signals.wen; signals.signaledOnce = function (nam, func) { var slf = this; function onit () { slf.removeReceiver(nam, wen); func.apply(this, args); } wen.receiver = func; this.onit(nam, wen); return this; }; signals.removeReceiver = function (nam, func) { if (this.sigsMap && this.sigsMap[nam]) { var sigLst = this.sigsMap[nam]; if (isAry(sigLst)) { var idx = -1; for (var i = 0, l = sigLst.length; i < l; i++) { if (sigLst[i] === func || (sigLst[i].receiver && sigLst[i].receiver === func)) { idx = i; break; } } if (idx < 0) { return this; } sigLst.splice(idx, 1); if (!sigLst.length) { delete this.sigsMap[nam]; } } else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) { delete this.sigsMap[nam]; } } return this; }; signals._setSignalOnceRV = function (val) { this._signalRtnVal_ = val; return this; }; signals._getSignalOnceRV = function () { if (this.hasOwnProperty('_signalRtnVal_')) { return this._signalRtnVal_; } else { return true; } }; signals.signal = function (nam, args) { var recvrLst = this.receiverObjects(nam), k, recvr, i, rspns; for (k in recvrLst ) { if (recvrLst.hasOwnProperty(k)) { i = recvrLst[k].length; while (i--) { recvr = recvrLst[k][i]; if (recvr.signaledOnce === true) { this.removeReceiver(nam, recvr); } rspns = recvr.call(this, args || []); if (rspns === this._getSignalOnceRV()) { this.removeReceiver(nam, recvr); } } } } return this; }; if (typeof module === 'object' && module.exports){ module.exports = Signals; } else if (typeof define === 'function' && define.amd) { define(function () { return Signals; }); } else { exports.Signals = Signals; } }.call(this)); var Signals = function () { 'use strict'; var sigCache = {}; function isAry(obj) { return (obj.constructor === Array); } function receiverObjects(eventName) { var recvrLst = receivers(eventName), rspns; if (isAry(recvrLst)) { rspns = {}; rspns[eventName] = recvrLst; } return rspns || recvrLst; } function receivers(eventName) { if (!sigCache) { sigCache = {}; } if (!sigCache[eventName]) { sigCache[eventName] = []; } if (!isAry(sigCache[eventName])) { sigCache[eventName] = [sigCache[eventName]]; } return sigCache[eventName]; } function wen(eventName, func) { if (!sigCache) { sigCache = {}; } if (!sigCache[eventName]) { sigCache[eventName] = func; } else if (isAry(sigCache[eventName])) { sigCache[eventName].push(func); } else { sigCache[eventName] = [sigCache[eventName], func]; } return this; } var addReceiver = function(eventName, func) { return wen(eventName, function(evnt){ return func(evnt); }); }, signalReceived = function(eventName, func) { return wen(eventName, function(evnt){ return func(evnt); }); }, dropReceivers = function (eventName) { if (!eventName) { sigCache = {}; return this; } if (sigCache && sigCache[eventName]) { sigCache[eventName] = null; } return this; }, signalOnce = function (eventName, func) { var slf = this; function onit () { slf.dropReceiver(eventName, wen); func.apply(this, args); } wen.receiver = func; this.onit(eventName, wen); return this; }, dropReceiver = function (eventName, func) { if (sigCache && sigCache[eventName]) { var sigLst = sigCache[eventName]; if (isAry(sigLst)) { var idx = -1; for (var i = 0, l = sigLst.length; i < l; i++) { if (sigLst[i] === func || (sigLst[i].receiver && sigLst[i].receiver === func)) { idx = i; break; } } if (idx < 0) { return this; } sigLst.splice(idx, 1); if (!sigLst.length) { delete sigCache[eventName]; } } else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) { delete sigCache[eventName]; } } return this; }, setSingleRV = function (val) { singleRtnVal = val; return this; }, getSingleRV = function() { if (sigCache.hasOwnProperty('singleRtnVal')) { return singleRtnVal; } else { return true; } }, castSignal = function(eventName, args) { var recvrLst = receiverObjects(eventName), k, recvr, i, rspns; for (k in recvrLst ) { if (recvrLst.hasOwnProperty(k)) { i = recvrLst[k].length; while (i--) { recvr = recvrLst[k][i]; if (recvr.signalOnce === true) { dropReceiver(eventName, recvr); } rspns = recvr.call(this, args || []); if (rspns === getSingleRV()) { dropReceiver(eventName, recvr); } } } } return this; }; return { signal: castSignal, signaled: signalReceived, receive: addReceiver, receivers: receivers, receiveOnce: signalOnce, dropReceiver: dropReceiver, dropReceivers: dropReceivers }; }; exports.Signals = Signals;
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus