by

Signals.js ...  even more event insanity!  ...

Just another manifestation of my fixation on events.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/230055/Signals-js------even-more-event-insanity?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
// Signals.js (function () { 'use strict'; function Signals () {}; var exports = this, signalsMap = {}, _signalRtnVal_ = {}, signals = Signals.prototype; function isAry(obj) { return (obj.constructor === Array); } function _setSignalOnceRV (val) { _signalRtnVal_ = val; return this; } function _getSignalOnceRV () { if (_signalRtnVal_) { return _signalRtnVal_; } else { return true; } } function wen(nam, func) { if (!signalsMap) { signalsMap = {}; } if (!signalsMap[nam]) { signalsMap[nam] = func; } else if (isAry(signalsMap[nam])) { signalsMap[nam].push(func); } else { signalsMap[nam] = [signalsMap[nam], func]; } return this; } signals.signaled = function(nam, func) { return wen(nam, function(evnt){ return func(evnt); }); }; signals.dropReceivers = function (nam) { if (!nam) { signalsMap = {}; return this; } if (signalsMap && signalsMap[nam]) { signalsMap[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 (!signalsMap) { signalsMap = {}; } if (!signalsMap[nam]) { signalsMap[nam] = []; } if (!isAry(signalsMap[nam])) { signalsMap[nam] = [signalsMap[nam]]; } return signalsMap[nam]; }; signals.addReceiver = function(nam, func) { return wen(nam, function(evnt){ return func(evnt); }); }; signals.signaledOnce = function (nam, func) { var slf = this; function onit () { slf.dropReceiver(nam, wen); func.apply(this, args); } wen.receiver = func; this.onit(nam, wen); return this; }; signals.receiveOnce = function(nam, func) { return signaledOnce(nam, function(evnt) { return func(evnt); }); }; signals.dropReceiver = function (nam, func) { if (signalsMap && signalsMap[nam]) { var sigLst = signalsMap[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 signalsMap[nam]; } } else if (sigLst === func || (sigLst.receiver && sigLst.receiver === func)) { delete signalsMap[nam]; } } return this; }; 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.dropReceiver(nam, recvr); } rspns = recvr.call(this, args || []); if (rspns === _getSignalOnceRV()) { this.dropReceiver(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));
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus