mini-signals
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -12,12 +12,26 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MiniSignal = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var MiniSignalBinding = function MiniSignalBinding(fn) { | ||
var once = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var MiniSignalBinding = (function () { | ||
function MiniSignalBinding(fn, once, thisArg) { | ||
if (once === undefined) once = false; | ||
_classCallCheck(this, MiniSignalBinding); | ||
_classCallCheck(this, MiniSignalBinding); | ||
this._fn = fn; | ||
this._next = this._prev = null; | ||
this._once = once; | ||
}; | ||
this._fn = fn; | ||
this._once = once; | ||
this._thisArg = thisArg; | ||
this._next = this._prev = this._owner = null; | ||
} | ||
_createClass(MiniSignalBinding, [{ | ||
key: 'detach', | ||
value: function detach() { | ||
if (this._owner === null) return false; | ||
this._owner.detach(this); | ||
return true; | ||
} | ||
}]); | ||
return MiniSignalBinding; | ||
})(); | ||
function _addMiniSignalBinding(self, node) { | ||
@@ -33,5 +47,3 @@ if (!self._head) { | ||
node.detach = (function () { | ||
self.detach(this); | ||
}).bind(node); | ||
node._owner = self; | ||
@@ -55,5 +67,3 @@ return node; | ||
if (exists) { | ||
return !!node; | ||
} | ||
if (exists) return !!node; | ||
@@ -70,2 +80,11 @@ var ee = []; | ||
}, { | ||
key: 'has', | ||
value: function has(node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error('MiniSignal#has(): First arg must be a MiniSignalBinding object.'); | ||
} | ||
return node._owner === this; | ||
} | ||
}, { | ||
key: 'dispatch', | ||
@@ -75,11 +94,7 @@ value: function dispatch() { | ||
if (!node) { | ||
return false; | ||
} | ||
if (!node) return false; | ||
while (node) { | ||
node._fn.apply(this, arguments); | ||
if (node._once) { | ||
this.detach(node); | ||
} | ||
node._fn.apply(node._thisArg, arguments); | ||
if (node._once) this.detach(node); | ||
node = node._next; | ||
@@ -93,6 +108,8 @@ } | ||
value: function add(fn) { | ||
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignal#add(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, false, thisArg)); | ||
} | ||
@@ -102,6 +119,8 @@ }, { | ||
value: function once(fn) { | ||
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignal#once(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true, thisArg)); | ||
} | ||
@@ -114,11 +133,11 @@ }, { | ||
} | ||
if (!node._fn) { | ||
return this; | ||
} | ||
if (node._owner !== this) return this; | ||
if (node._prev) node._prev._next = node._next; | ||
if (node._next) node._next._prev = node._prev; | ||
if (node === this._head) { | ||
this._head = node._next; | ||
if (!this._head) { | ||
if (node._next === null) { | ||
this._tail = null; | ||
} else { | ||
this._head._prev = null; | ||
} | ||
@@ -128,7 +147,5 @@ } else if (node === this._tail) { | ||
this._tail._next = null; | ||
} else { | ||
node._prev._next = node._next; | ||
node._next._prev = node._prev; | ||
} | ||
node._fn = null; | ||
node._owner = null; | ||
return this; | ||
@@ -139,3 +156,11 @@ } | ||
value: function detachAll() { | ||
var node = this._head; | ||
if (!node) return this; | ||
this._head = this._tail = null; | ||
while (node) { | ||
node._owner = null; | ||
node = node._next; | ||
} | ||
return this; | ||
@@ -142,0 +167,0 @@ } |
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MiniSignal=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});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")}}var MiniSignalBinding=function MiniSignalBinding(fn){var once=arguments.length<=1||arguments[1]===undefined?false:arguments[1];_classCallCheck(this,MiniSignalBinding);this._fn=fn;this._next=this._prev=null;this._once=once};function _addMiniSignalBinding(self,node){if(!self._head){self._head=node;self._tail=node}else{self._tail._next=node;node._prev=self._tail;self._tail=node}node.detach=function(){self.detach(this)}.bind(node);return node}var MiniSignal=function(){function MiniSignal(){_classCallCheck(this,MiniSignal);this._head=this._tail=undefined}_createClass(MiniSignal,[{key:"handlers",value:function handlers(){var exists=arguments.length<=0||arguments[0]===undefined?false:arguments[0];var node=this._head;if(exists){return!!node}var ee=[];while(node){ee.push(node);node=node._next}return ee}},{key:"dispatch",value:function dispatch(){var node=this._head;if(!node){return false}while(node){node._fn.apply(this,arguments);if(node._once){this.detach(node)}node=node._next}return true}},{key:"add",value:function add(fn){if(typeof fn!=="function"){throw new Error("MiniSignal#add(): First arg must be a Function.")}return _addMiniSignalBinding(this,new MiniSignalBinding(fn))}},{key:"once",value:function once(fn){if(typeof fn!=="function"){throw new Error("MiniSignal#once(): First arg must be a Function.")}return _addMiniSignalBinding(this,new MiniSignalBinding(fn,true))}},{key:"detach",value:function detach(node){if(!(node instanceof MiniSignalBinding)){throw new Error("MiniSignal#detach(): First arg must be a MiniSignalBinding object.")}if(!node._fn){return this}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;return this}},{key:"detachAll",value:function detachAll(){this._head=this._tail=null;return this}}]);return MiniSignal}();MiniSignal.MiniSignalBinding=MiniSignalBinding;exports["default"]=MiniSignal;module.exports=exports["default"]},{}]},{},[1])(1)}); | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MiniSignal=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});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")}}var MiniSignalBinding=function(){function MiniSignalBinding(fn,once,thisArg){if(once===undefined)once=false;_classCallCheck(this,MiniSignalBinding);this._fn=fn;this._once=once;this._thisArg=thisArg;this._next=this._prev=this._owner=null}_createClass(MiniSignalBinding,[{key:"detach",value:function detach(){if(this._owner===null)return false;this._owner.detach(this);return true}}]);return MiniSignalBinding}();function _addMiniSignalBinding(self,node){if(!self._head){self._head=node;self._tail=node}else{self._tail._next=node;node._prev=self._tail;self._tail=node}node._owner=self;return node}var MiniSignal=function(){function MiniSignal(){_classCallCheck(this,MiniSignal);this._head=this._tail=undefined}_createClass(MiniSignal,[{key:"handlers",value:function handlers(){var exists=arguments.length<=0||arguments[0]===undefined?false:arguments[0];var node=this._head;if(exists)return!!node;var ee=[];while(node){ee.push(node);node=node._next}return ee}},{key:"has",value:function has(node){if(!(node instanceof MiniSignalBinding)){throw new Error("MiniSignal#has(): First arg must be a MiniSignalBinding object.")}return node._owner===this}},{key:"dispatch",value:function dispatch(){var node=this._head;if(!node)return false;while(node){node._fn.apply(node._thisArg,arguments);if(node._once)this.detach(node);node=node._next}return true}},{key:"add",value:function add(fn){var thisArg=arguments.length<=1||arguments[1]===undefined?null:arguments[1];if(typeof fn!=="function"){throw new Error("MiniSignal#add(): First arg must be a Function.")}return _addMiniSignalBinding(this,new MiniSignalBinding(fn,false,thisArg))}},{key:"once",value:function once(fn){var thisArg=arguments.length<=1||arguments[1]===undefined?null:arguments[1];if(typeof fn!=="function"){throw new Error("MiniSignal#once(): First arg must be a Function.")}return _addMiniSignalBinding(this,new MiniSignalBinding(fn,true,thisArg))}},{key:"detach",value:function detach(node){if(!(node instanceof MiniSignalBinding)){throw new Error("MiniSignal#detach(): First arg must be a MiniSignalBinding object.")}if(node._owner!==this)return this;if(node._prev)node._prev._next=node._next;if(node._next)node._next._prev=node._prev;if(node===this._head){this._head=node._next;if(node._next===null){this._tail=null}}else if(node===this._tail){this._tail=node._prev;this._tail._next=null}node._owner=null;return this}},{key:"detachAll",value:function detachAll(){var node=this._head;if(!node)return this;this._head=this._tail=null;while(node){node._owner=null;node=node._next}return this}}]);return MiniSignal}();MiniSignal.MiniSignalBinding=MiniSignalBinding;exports["default"]=MiniSignal;module.exports=exports["default"]},{}]},{},[1])(1)}); |
@@ -11,12 +11,26 @@ 'use strict'; | ||
var MiniSignalBinding = function MiniSignalBinding(fn) { | ||
var once = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var MiniSignalBinding = (function () { | ||
function MiniSignalBinding(fn, once, thisArg) { | ||
if (once === undefined) once = false; | ||
_classCallCheck(this, MiniSignalBinding); | ||
_classCallCheck(this, MiniSignalBinding); | ||
this._fn = fn; | ||
this._next = this._prev = null; | ||
this._once = once; | ||
}; | ||
this._fn = fn; | ||
this._once = once; | ||
this._thisArg = thisArg; | ||
this._next = this._prev = this._owner = null; | ||
} | ||
_createClass(MiniSignalBinding, [{ | ||
key: 'detach', | ||
value: function detach() { | ||
if (this._owner === null) return false; | ||
this._owner.detach(this); | ||
return true; | ||
} | ||
}]); | ||
return MiniSignalBinding; | ||
})(); | ||
function _addMiniSignalBinding(self, node) { | ||
@@ -32,5 +46,3 @@ if (!self._head) { | ||
node.detach = (function () { | ||
self.detach(this); | ||
}).bind(node); | ||
node._owner = self; | ||
@@ -54,5 +66,3 @@ return node; | ||
if (exists) { | ||
return !!node; | ||
} | ||
if (exists) return !!node; | ||
@@ -69,2 +79,11 @@ var ee = []; | ||
}, { | ||
key: 'has', | ||
value: function has(node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error('MiniSignal#has(): First arg must be a MiniSignalBinding object.'); | ||
} | ||
return node._owner === this; | ||
} | ||
}, { | ||
key: 'dispatch', | ||
@@ -74,11 +93,7 @@ value: function dispatch() { | ||
if (!node) { | ||
return false; | ||
} | ||
if (!node) return false; | ||
while (node) { | ||
node._fn.apply(this, arguments); | ||
if (node._once) { | ||
this.detach(node); | ||
} | ||
node._fn.apply(node._thisArg, arguments); | ||
if (node._once) this.detach(node); | ||
node = node._next; | ||
@@ -92,6 +107,8 @@ } | ||
value: function add(fn) { | ||
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignal#add(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, false, thisArg)); | ||
} | ||
@@ -101,6 +118,8 @@ }, { | ||
value: function once(fn) { | ||
var thisArg = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; | ||
if (typeof fn !== 'function') { | ||
throw new Error('MiniSignal#once(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true, thisArg)); | ||
} | ||
@@ -113,11 +132,11 @@ }, { | ||
} | ||
if (!node._fn) { | ||
return this; | ||
} | ||
if (node._owner !== this) return this; | ||
if (node._prev) node._prev._next = node._next; | ||
if (node._next) node._next._prev = node._prev; | ||
if (node === this._head) { | ||
this._head = node._next; | ||
if (!this._head) { | ||
if (node._next === null) { | ||
this._tail = null; | ||
} else { | ||
this._head._prev = null; | ||
} | ||
@@ -127,7 +146,5 @@ } else if (node === this._tail) { | ||
this._tail._next = null; | ||
} else { | ||
node._prev._next = node._next; | ||
node._next._prev = node._prev; | ||
} | ||
node._fn = null; | ||
node._owner = null; | ||
return this; | ||
@@ -138,3 +155,11 @@ } | ||
value: function detachAll() { | ||
var node = this._head; | ||
if (!node) return this; | ||
this._head = this._tail = null; | ||
while (node) { | ||
node._owner = null; | ||
node = node._next; | ||
} | ||
return this; | ||
@@ -141,0 +166,0 @@ } |
{ | ||
"name": "mini-signals", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "signals, in JavaScript, fast", | ||
@@ -10,4 +10,5 @@ "main": "lib/mini-signals.js", | ||
"test:fast": "mocha --compilers js:babel/register ./test/mini-signals-spec.js", | ||
"test:deopt": "node --trace_opt --trace_deopt ./bench/test-deopt.js | grep \"disabled optimization\"", | ||
"lint": "eslint src/mini-signals.js", | ||
"test:deopt": "node --trace_opt --trace_deopt ./bench/test-deopt.js | grep \"disabled optimization\" || true", | ||
"lint": "semistandard src/*.js test/*.js", | ||
"check": "npm run lint", | ||
"build": "npm run rollup && npm run browserify && npm run uglify && npm run jsdoc2md", | ||
@@ -19,5 +20,6 @@ "rollup": "rollup ./src/index.js | babel -o ./lib/mini-signals.js --no-comments", | ||
"mocha": "mocha --compilers js:babel/register", | ||
"bench": "babel-node ./bench/emit.js", | ||
"bench": "babel-node ./bench/emit.js && babel-node ./bench/emit-context.js", | ||
"_prepublish": "npm run build && npm test", | ||
"jsdoc2md": "jsdoc-parse ./src/mini-signals.js | dmd > API.md" | ||
"jsdoc2md": "jsdoc-parse ./src/mini-signals.js | dmd > API.md", | ||
"version": "chg release -y && git add -A CHANGELOG.md" | ||
}, | ||
@@ -45,3 +47,2 @@ "repository": { | ||
"dmd": "^1.2.0", | ||
"eslint": "^1.5.0", | ||
"eventemitter3": "^1.1.1", | ||
@@ -52,2 +53,3 @@ "istanbul": "^0.3.19", | ||
"rollup": "^0.16.4", | ||
"semistandard": "^7.0.2", | ||
"signals": "^1.0.0", | ||
@@ -54,0 +56,0 @@ "uglifyjs": "^2.4.10" |
@@ -23,3 +23,3 @@ # mini-signals | ||
``` | ||
jspm install mini-signals=github:Hypercubed/mini-signals | ||
jspm install mini-signals=npm:mini-signals | ||
``` | ||
@@ -59,3 +59,3 @@ | ||
myObject.updated.add(onUpdated.bind(myObject)); //add listener with context | ||
myObject.updated.add(onUpdated,myObject); //add listener with context | ||
@@ -62,0 +62,0 @@ myObject.foo = 'baz'; |
@@ -1,2 +0,2 @@ | ||
/*jshint -W097 */ | ||
/* jshint -W097 */ | ||
@@ -10,9 +10,18 @@ export class MiniSignalBinding { | ||
* @param {Boolean} [once=false] Should this listener be removed after dispatch | ||
* @param {Mixed} [thisArg] The context of the callback function. | ||
* @api private | ||
*/ | ||
constructor(fn, once = false) { | ||
constructor (fn, once = false, thisArg) { | ||
this._fn = fn; | ||
this._next = this._prev = null; | ||
this._once = once; | ||
this._thisArg = thisArg; | ||
this._next = this._prev = this._owner = null; | ||
} | ||
detach () { | ||
if (this._owner === null) return false; | ||
this._owner.detach(this); | ||
return true; | ||
} | ||
} | ||
@@ -23,3 +32,3 @@ | ||
*/ | ||
function _addMiniSignalBinding(self, node) { | ||
function _addMiniSignalBinding (self, node) { | ||
if (!self._head) { | ||
@@ -34,5 +43,3 @@ self._head = node; | ||
node.detach = (function() { | ||
self.detach(this); | ||
}).bind(node); | ||
node._owner = self; | ||
@@ -55,3 +62,3 @@ return node; | ||
*/ | ||
constructor() { | ||
constructor () { | ||
this._head = this._tail = undefined; | ||
@@ -67,6 +74,6 @@ } | ||
*/ | ||
handlers(exists = false) { | ||
handlers (exists = false) { | ||
let node = this._head; | ||
if (exists) { return !!node; } | ||
if (exists) return !!node; | ||
@@ -84,2 +91,17 @@ const ee = []; | ||
/** | ||
* Return true if node is a MiniSignalBinding attached to this MiniSignal | ||
* | ||
* @param {MiniSignalBinding} node Node to check. | ||
* @returns {Boolean} True if node is attache to mini-signal | ||
* @api public | ||
*/ | ||
has (node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error('MiniSignal#has(): First arg must be a MiniSignalBinding object.'); | ||
} | ||
return node._owner === this; | ||
} | ||
/** | ||
* Dispaches a signal to all registered listeners. | ||
@@ -90,10 +112,10 @@ * | ||
*/ | ||
dispatch() { | ||
dispatch () { | ||
let node = this._head; | ||
if (!node) { return false; } | ||
if (!node) return false; | ||
while (node) { | ||
node._fn.apply(this, arguments); | ||
if (node._once) { this.detach(node); } | ||
node._fn.apply(node._thisArg, arguments); | ||
if (node._once) this.detach(node); | ||
node = node._next; | ||
@@ -109,24 +131,26 @@ } | ||
* @param {Function} fn Callback function. | ||
* @param {Mixed} [thisArg] The context of the callback function. | ||
* @returns {MiniSignalBinding} The MiniSignalBinding node that was added. | ||
* @api public | ||
*/ | ||
add(fn) { | ||
add (fn, thisArg = null) { | ||
if (typeof fn !== 'function') { | ||
throw new Error( 'MiniSignal#add(): First arg must be a Function.' ); | ||
throw new Error('MiniSignal#add(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, false, thisArg)); | ||
} | ||
/** | ||
* Register a new listener that will be executed only once. | ||
* | ||
* @param {Function} fn Callback function. | ||
* @returns {MiniSignalBinding} The MiniSignalBinding node that was added. | ||
* @api public | ||
*/ | ||
once(fn) { | ||
* Register a new listener that will be executed only once. | ||
* | ||
* @param {Function} fn Callback function. | ||
* @param {Mixed} [thisArg] The context of the callback function. | ||
* @returns {MiniSignalBinding} The MiniSignalBinding node that was added. | ||
* @api public | ||
*/ | ||
once (fn, thisArg = null) { | ||
if (typeof fn !== 'function') { | ||
throw new Error( 'MiniSignal#once(): First arg must be a Function.' ); | ||
throw new Error('MiniSignal#once(): First arg must be a Function.'); | ||
} | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true)); | ||
return _addMiniSignalBinding(this, new MiniSignalBinding(fn, true, thisArg)); | ||
} | ||
@@ -140,13 +164,15 @@ | ||
* @api public */ | ||
detach(node) { | ||
detach (node) { | ||
if (!(node instanceof MiniSignalBinding)) { | ||
throw new Error( 'MiniSignal#detach(): First arg must be a MiniSignalBinding object.' ); | ||
throw new Error('MiniSignal#detach(): First arg must be a MiniSignalBinding object.'); | ||
} | ||
if (!node._fn) { return this; } | ||
if (node === this._head) { // first node | ||
if (node._owner !== this) return this; // todo: or error? | ||
if (node._prev) node._prev._next = node._next; | ||
if (node._next) node._next._prev = node._prev; | ||
if (node === this._head) { // first node | ||
this._head = node._next; | ||
if (!this._head){ | ||
if (node._next === null) { | ||
this._tail = null; | ||
} else { | ||
this._head._prev = null; | ||
} | ||
@@ -156,7 +182,5 @@ } else if (node === this._tail) { // last node | ||
this._tail._next = null; | ||
} else { // middle | ||
node._prev._next = node._next; | ||
node._next._prev = node._prev; | ||
} | ||
node._fn = null; | ||
node._owner = null; | ||
return this; | ||
@@ -171,4 +195,12 @@ } | ||
*/ | ||
detachAll() { | ||
detachAll () { | ||
let node = this._head; | ||
if (!node) return this; | ||
this._head = this._tail = null; | ||
while (node) { | ||
node._owner = null; | ||
node = node._next; | ||
} | ||
return this; | ||
@@ -175,0 +207,0 @@ } |
24808
10
446