mini-signals
Advanced tools
Comparing version 0.0.3 to 0.0.4
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "module"], factory); | ||
} else if (typeof exports !== "undefined" && typeof module !== "undefined") { | ||
if (typeof define === 'function' && define.amd) { | ||
define(['exports', 'module'], factory); | ||
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') { | ||
factory(exports, module); | ||
@@ -14,9 +14,9 @@ } else { | ||
})(this, function (exports, module) { | ||
"use strict"; | ||
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
function Node(fn, context) { | ||
function MiniSignalBinding(fn, context) { | ||
var once = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; | ||
@@ -38,3 +38,3 @@ | ||
_createClass(MiniSignals, [{ | ||
key: "listeners", | ||
key: 'listeners', | ||
value: function listeners(exists) { | ||
@@ -57,4 +57,4 @@ var node = this._head; | ||
}, { | ||
key: "dispatch", | ||
value: function dispatch() { | ||
key: 'dispatch', | ||
value: function dispatch(a) { | ||
var node = this._head; | ||
@@ -67,2 +67,3 @@ | ||
while (node) { | ||
node._fn.apply(node._context, arguments); | ||
@@ -78,16 +79,22 @@ if (node._once) { | ||
}, { | ||
key: "add", | ||
key: 'add', | ||
value: function add(fn, context) { | ||
var node = new Node(fn, context || this); | ||
return this._addNode(node); | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignals#add(): First arg must be a Function.'); | ||
} | ||
var node = new MiniSignalBinding(fn, context || this); | ||
return this._addMiniSignalBinding(node); | ||
} | ||
}, { | ||
key: "once", | ||
key: 'once', | ||
value: function once(fn, context) { | ||
var node = new Node(fn, context || this, true); | ||
return this._addNode(node); | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignals#once(): First arg must be a Function.'); | ||
} | ||
var node = new MiniSignalBinding(fn, context || this, true); | ||
return this._addMiniSignalBinding(node); | ||
} | ||
}, { | ||
key: "_addNode", | ||
value: function _addNode(node) { | ||
key: '_addMiniSignalBinding', | ||
value: function _addMiniSignalBinding(node) { | ||
if (!this._head) { | ||
@@ -110,3 +117,3 @@ this._head = node; | ||
}, { | ||
key: "remove", | ||
key: 'remove', | ||
value: function remove(fn, context) { | ||
@@ -116,2 +123,8 @@ if (!fn) { | ||
} | ||
if (fn instanceof MiniSignalBinding) { | ||
return this.detach(fn); | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignals#remove(): First arg must be a Function.'); | ||
} | ||
@@ -131,4 +144,7 @@ var node = this._head; | ||
}, { | ||
key: "detach", | ||
key: 'detach', | ||
value: function detach(node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error('MiniSignals#detach(): First arg must be a MiniSignalBinding object.'); | ||
} | ||
if (!node._fn) { | ||
@@ -155,3 +171,3 @@ return; | ||
}, { | ||
key: "removeAll", | ||
key: 'removeAll', | ||
value: function removeAll() { | ||
@@ -158,0 +174,0 @@ var node = this._head; |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){if(typeof define==="function"&&define.amd){define(["exports","module"],factory)}else if(typeof exports!=="undefined"&&typeof module!=="undefined"){factory(exports,module)}else{var mod={exports:{}};factory(mod.exports,mod);global.miniSignals=mod.exports}})(this,function(exports,module){"use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function Node(fn,context){var once=arguments.length<=2||arguments[2]===undefined?false:arguments[2];this.fn=fn;this.context=context;this.next=this.prev=null;this.once=once}var MiniSignals=function(){function MiniSignals(){_classCallCheck(this,MiniSignals);this._head=this._tail=undefined}_createClass(MiniSignals,[{key:"listeners",value:function listeners(exists){var node=this._head;if(exists){return!!node}var ee=[];while(node){ee.push(node.fn);node=node.next}return ee}},{key:"dispatch",value:function dispatch(){var node=this._head;if(!node){return false}while(node){node.fn.apply(node.context,arguments);if(node.once){this._removeNode(node)}node=node.next}return true}},{key:"add",value:function add(fn,context){var node=new Node(fn,context||this);return this._addNode(node)}},{key:"addOnce",value:function addOnce(fn,context){var node=new Node(fn,context||this,true);return this._addNode(node)}},{key:"_addNode",value:function _addNode(node){if(!this._head){this._head=node;this._tail=node}else{this._tail.next=node;node.prev=this._tail;this._tail=node}return this}},{key:"remove",value:function remove(fn,context){if(!fn){return this.removeAll()}var node=this._head;while(node){if(node.fn===fn&&(!context||node.context===context)){this._removeNode(node)}node=node.next}return this}},{key:"_removeNode",value:function _removeNode(node){if(node===this._head){this._head=node.next;if(!this._head){this._tail=null}else{this._head.prev=null}}else if(node===this._tail){this._tail=node.prev;this._tail.next=null}else{node.prev.next=node.next;node.next.prev=node.prev}}},{key:"removeAll",value:function removeAll(){var node=this._head;if(!node){return this}this._head=this._tail=null;return this}}]);return MiniSignals}();module.exports=MiniSignals}); | ||
(function(global,factory){if(typeof define==="function"&&define.amd){define(["exports","module"],factory)}else if(typeof exports!=="undefined"&&typeof module!=="undefined"){factory(exports,module)}else{var mod={exports:{}};factory(mod.exports,mod);global.miniSignals=mod.exports}})(this,function(exports,module){"use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function Node(fn,context){var once=arguments.length<=2||arguments[2]===undefined?false:arguments[2];this._fn=fn;this._context=context;this._next=this._prev=null;this._once=once}var MiniSignals=function(){function MiniSignals(){_classCallCheck(this,MiniSignals);this._head=this._tail=undefined}_createClass(MiniSignals,[{key:"listeners",value:function listeners(exists){var node=this._head;if(exists){return!!node}var ee=[];while(node){ee.push(node._fn);node=node._next}return ee}},{key:"dispatch",value:function dispatch(){var node=this._head;if(!node){return false}while(node){node._fn.apply(node._context,arguments);if(node._once){this.detach(node)}node=node._next}return true}},{key:"add",value:function add(fn,context){var node=new Node(fn,context||this);return this._addNode(node)}},{key:"once",value:function once(fn,context){var node=new Node(fn,context||this,true);return this._addNode(node)}},{key:"_addNode",value:function _addNode(node){if(!this._head){this._head=node;this._tail=node}else{this._tail._next=node;node._prev=this._tail;this._tail=node}var self=this;node.detach=function(){self.detach(this)}.bind(node);return node}},{key:"remove",value:function remove(fn,context){if(!fn){return this.removeAll()}var node=this._head;while(node){if(node._fn===fn&&(!context||node._context===context)){this.detach(node)}node=node._next}return this}},{key:"detach",value:function detach(node){if(!node._fn){return}if(node===this._head){this._head=node._next;if(!this._head){this._tail=null}else{this._head._prev=null}}else if(node===this._tail){this._tail=node._prev;this._tail._next=null}else{node._prev._next=node._next;node._next._prev=node._prev}node._fn=null;node._context=null}},{key:"removeAll",value:function removeAll(){var node=this._head;if(!node){return this}this._head=this._tail=null;return this}}]);return MiniSignals}();module.exports=MiniSignals}); |
{ | ||
"name": "mini-signals", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "signals, in JavaScript, fast", | ||
@@ -14,3 +14,3 @@ "main": "mini-signals.js", | ||
"bench": "node ./bench/emit.js", | ||
"go": "npm run mocha && npm run babel && npm run bench" | ||
"prepublish": "npm run mocha && npm run babel" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -10,3 +10,3 @@ /*jshint -W097 */ | ||
*/ | ||
function Node(fn, context, once = false) { | ||
function MiniSignalBinding(fn, context, once = false) { | ||
this._fn = fn; | ||
@@ -56,3 +56,3 @@ this._context = context; | ||
*/ | ||
dispatch() { | ||
dispatch(a) { | ||
var node = this._head; | ||
@@ -63,2 +63,14 @@ | ||
while (node) { | ||
/* switch (arguments.length) { | ||
case 0: | ||
node._fn.call(node._context); | ||
break; | ||
case 1: | ||
node._fn.call(node._context, a); | ||
break; | ||
default: | ||
node._fn.apply(node._context, arguments); | ||
} */ | ||
node._fn.apply(node._context, arguments); | ||
@@ -73,19 +85,34 @@ if (node._once) { this.detach(node); } | ||
/** | ||
* Register a new EventListener. | ||
* Register a new listener. | ||
* | ||
* @param {Functon} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @return {SignalBinding} An Object representing the binding between the Signal and listener. | ||
* @api public | ||
*/ | ||
add(fn, context) { | ||
var node = new Node(fn, context || this); | ||
return this._addNode(node); | ||
if (typeof fn !== 'function') { | ||
throw new Error( 'MiniSignals#add(): First arg must be a Function.' ); | ||
} | ||
var node = new MiniSignalBinding(fn, context || this); | ||
return this._addMiniSignalBinding(node); | ||
} | ||
/** | ||
* Register a new listener that will be executed only once. | ||
* | ||
* @param {Functon} fn Callback function. | ||
* @param {Mixed} context The context of the function. | ||
* @return {MiniSignalBinding} An Object representing the binding between the Signal and listener. | ||
* @api public | ||
*/ | ||
once(fn, context) { | ||
var node = new Node(fn, context || this, true); | ||
return this._addNode(node); | ||
if (typeof fn !== 'function') { | ||
throw new Error( 'MiniSignals#once(): First arg must be a Function.' ); | ||
} | ||
var node = new MiniSignalBinding(fn, context || this, true); | ||
return this._addMiniSignalBinding(node); | ||
} | ||
_addNode(node) { | ||
_addMiniSignalBinding(node) { | ||
if (!this._head) { | ||
@@ -111,3 +138,3 @@ this._head = node; | ||
* | ||
* @param {Function} fn The listener that we need to find. | ||
* @param {Mixed} fn The listener that we need to find or the MiniSignalBinding to remove. | ||
* @param {Mixed} context Only remove listeners matching this context. | ||
@@ -117,2 +144,6 @@ * @api public */ | ||
if (!fn) { return this.removeAll(); } // maybe change this | ||
if (fn instanceof MiniSignalBinding) { return this.detach(fn); } | ||
if (typeof fn !== 'function') { | ||
throw new Error( 'MiniSignals#remove(): First arg must be a Function.' ); | ||
} | ||
@@ -135,5 +166,8 @@ var node = this._head; | ||
* | ||
* @param {Node} node The binding node that will be removed. | ||
* @param {MiniSignalBinding} node The binding node that will be removed. | ||
* @api public */ | ||
detach(node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error( 'MiniSignals#detach(): First arg must be a MiniSignalBinding object.' ); | ||
} | ||
if (!node._fn) { return; } | ||
@@ -158,3 +192,2 @@ if (node === this._head) { // first node | ||
/** | ||
@@ -161,0 +194,0 @@ * Remove all listeners. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17369
337