by
24
8
444k
2
Top 1% !
Popular
Pearl of Wisdom
Nice
Useful
Easy-to-find
Specified
MultiPlatform
Popularity: 1680th place
Created
Modified Dec 15, 2014
LanguageJavaScript
LicenseMIT_X11
SourceGitHub
SourceMyGists

Signals.js...           A fast and efficient event system in javascript.

Signals.js...           A fast and efficient event system in javascript.: 
Signals.js
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/231230/Signals-js--------------A-fast-and-effic?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
// Signalz.js var Signals = (function () { 'use strict'; var sigCache = {}, 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; setSingleRV(true); function onit () { dropReceiver(eventName, wen); func.apply(this, arguments); }; wen.receiver = onit; 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 (receivers.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; }; 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 (isArry(sigCache[eventName])) { sigCache[eventName].push(func); } else { sigCache[eventName] = [sigCache[eventName], func]; } return; }; return { signal: castSignal, signaled: signalReceived, receive: addReceiver, receiveOnce: signalOnce, signaledOnce: signalOnce, dropReceiver: dropReceiver, assignReceiver: addReceiver, receivers: receivers, 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