Socket
Socket
Sign inDemoInstall

mini-signals

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mini-signals - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

108

mini-signals.js

@@ -21,5 +21,8 @@ (function (global, factory) {

function Node(fn, context) {
this.fn = fn;
this.context = context;
this.next = this.prev = null;
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;
}

@@ -42,12 +45,8 @@

}
if (!node) {
return [];
}
var i = 0,
ee = new Array();
var ee = [];
while (node) {
ee.push(node.fn);
node = node.next;
ee.push(node._fn);
node = node._next;
}

@@ -67,4 +66,7 @@

while (node) {
node.fn.apply(node.context, arguments);
node = node.next;
node._fn.apply(node._context, arguments);
if (node._once) {
this.detach(node);
}
node = node._next;
}

@@ -77,5 +79,14 @@

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) {

@@ -85,8 +96,13 @@ this._head = node;

} else {
this._tail.next = node;
node.prev = this._tail;
this._tail._next = node;
node._prev = this._tail;
this._tail = node;
}
return this;
var self = this;
node.detach = (function () {
self.detach(this);
}).bind(node);
return node;
}

@@ -96,7 +112,2 @@ }, {

value: function remove(fn, context) {
var node = this._head,
next;
if (!node) {
return this;
}
if (!fn) {

@@ -106,24 +117,10 @@ return this.removeAll();

var node = this._head;
while (node) {
next = node.next;
if (node.fn === fn && (!context || node.context === context)) {
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.next = node.prev = null;
if (node._fn === fn && (!context || node._context === context)) {
this.detach(node);
}
node = next;
node = node._next;
}

@@ -134,6 +131,28 @@

}, {
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,
next;
var node = this._head;
if (!node) {

@@ -143,7 +162,2 @@ return this;

while (node) {
next = node.next;
node.next = node.prev = null;
node = next;
}
this._head = this._tail = null;

@@ -150,0 +164,0 @@ return this;

@@ -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){this.fn=fn;this.context=context;this.next=this.prev=null}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}if(!node){return[]}var i=0,ee=new Array;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);node=node.next}return true}},{key:"add",value:function add(fn,context){var node=new Node(fn,context||this);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){var node=this._head,next;if(!node){return this}if(!fn){return this.removeAll()}while(node){next=node.next;if(node.fn===fn&&(!context||node.context===context)){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.next=node.prev=null}node=next}return this}},{key:"removeAll",value:function removeAll(){var node=this._head,next;if(!node){return this}while(node){next=node.next;node.next=node.prev=null;node=next}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._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});
{
"name": "mini-signals",
"version": "0.0.2",
"version": "0.0.3",
"description": "signals, in JavaScript, fast",

@@ -8,3 +8,3 @@ "main": "mini-signals.js",

"test": "npm run mocha",
"build": "npm run babel && npm run uglify",
"build": "npm test && npm run babel && npm run uglify",
"babel": "babel src/mini-signals.js -m umd -o mini-signals.js --no-comments",

@@ -15,4 +15,3 @@ "uglify": "uglifyjs mini-signals.js -o mini-signals.min.js",

"bench": "node ./bench/emit.js",
"go": "npm run mocha && npm run babel && npm run bench",
"prepublish": "npm test && npm run build"
"go": "npm run mocha && npm run babel && npm run bench"
},

@@ -37,12 +36,9 @@ "repository": {

"babel": "^5.8.23",
"benchmark": "^1.0.0",
"eventemitter3": "^1.1.1",
"istanbul": "^0.3.19",
"mocha": "^2.2.5",
"benchmark": "^1.0.0",
"eventemitter3": "^1.1.1",
"signals": "^1.0.0"
},
"jspm": {
"format": "es6",
"main": "src/mini-signals"
"signals": "^1.0.0",
"uglifyjs": "^2.4.10"
}
}
# mini-signals
signals, in JavaScript, fast
[![NPM](https://img.shields.io/npm/v/mini-signals.svg)](https://www.npmjs.com/package/mini-signals) [![Build Status](https://travis-ci.org/Hypercubed/mini-signals.svg)](https://travis-ci.org/Hypercubed/mini-signals/) [![Codacy Badge](https://api.codacy.com/project/badge/18fa3fdfb90b43c7966f817124307d66)](https://www.codacy.com/app/hypercubed/mini-signals) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hypercubed/mini-signals/blob/master/LICENSE)
## Description

@@ -8,3 +10,3 @@

There are several advantages to signals over event-emitters (see [Comparison between different Observer Pattern implementations](https://github.com/millermedeiros/js-signals/wiki/Comparison-between-different-Observer-Pattern-implementations)). However, the current implementation of [js-signals](https://github.com/millermedeiros/js-signals) is (arguably) slow compared to other implementations (see [EventsSpeedTests](https://github.com/Hypercubed/EventsSpeedTests)). `mini-signals` is a fast, minimal emitter, that is mostly API compatible with[js-signals](https://github.com/millermedeiros/js-signals).
There are several advantages to signals over event-emitters (see [Comparison between different Observer Pattern implementations](https://github.com/millermedeiros/js-signals/wiki/Comparison-between-different-Observer-Pattern-implementations)). However, the current implementation of [js-signals](https://github.com/millermedeiros/js-signals) is (arguably) slow compared to other implementations (see [EventsSpeedTests](https://github.com/Hypercubed/EventsSpeedTests)). `mini-signals` is a fast, minimal emitter, with an API similar to [js-signals](https://github.com/millermedeiros/js-signals).

@@ -31,12 +33,34 @@ ## Install

mySignal.add(onSignal); //add listener
mySignal.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
mySignal.remove(onStarted); //remove a single listener
var binding = mySignal.add(onSignal); //add listener
mySignal.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
binding.detach(); //remove a single listener
function onSignal(foo, bar) {
/* */
assert(foo === 'foo');
assert(bar === 'bar');
}
```
## Another Example
```
var Signal = require('mini-signals');
var myObject = {
foo: 'bar',
updated: new Signal()
}
myObject.updated.add(onUpdated, myObject); //add listener with context
myObject.foo = 'baz';
myObject.updated.dispatch(); //dispatch signal
function onUpdated() {
assert(this === myObject);
assert(this.foo === 'baz');
}
```
## License

@@ -43,0 +67,0 @@

@@ -10,6 +10,7 @@ /*jshint -W097 */

*/
function Node(fn, context) {
this.fn = fn;
this.context = context;
this.next = this.prev = null;
function Node(fn, context, once = false) {
this._fn = fn;
this._context = context;
this._next = this._prev = null;
this._once = once;
}

@@ -21,3 +22,3 @@

*/
class MiniSignals {
export default class MiniSignals {

@@ -39,13 +40,12 @@ constructor() {

if (exists) { return !!node; }
if (!node) { return []; }
var i = 0, ee = new Array();
var ee = [];
while (node) {
ee.push(node.fn);
node = node.next;
ee.push(node._fn);
node = node._next;
}
return ee;
};
}

@@ -64,8 +64,9 @@ /**

while (node) {
node.fn.apply(node.context, arguments);
node = node.next;
node._fn.apply(node._context, arguments);
if (node._once) { this.detach(node); }
node = node._next;
}
return true;
};
}

@@ -80,5 +81,12 @@ /**

add(fn, context) {
var node = new Node(fn, context || this);
return this._addNode(node);
}
once(fn, context) {
var node = new Node(fn, context || this, true);
return this._addNode(node);
}
_addNode(node) {
if (!this._head) {

@@ -88,50 +96,64 @@ this._head = node;

} else {
this._tail.next = node;
node.prev = this._tail;
this._tail._next = node;
node._prev = this._tail;
this._tail = node;
}
return this;
};
var self = this;
node.detach = (function() {
self.detach(this);
}).bind(node);
return node;
}
/**
* Remove event listeners.
* Remove event listeners. (may be deprecated)
*
* @param {Function} fn The listener that we need to find.
* @param {Mixed} context Only remove listeners matching this context.
* @api public
*/
* @api public */
remove(fn, context) {
var node = this._head, next;
if (!node) { return this; }
if (!fn) { return this.removeAll(); } // maybe change this
var node = this._head;
while (node) {
next = node.next;
if (node.fn === fn && (!context || node.context === context)) {
if (node === this._head) { // first node
this._head = node.next;
if (!this._head){
this._tail = null;
} else {
this._head.prev = null;
}
} else if (node === this._tail) { // last node
this._tail = node.prev;
this._tail.next = null;
} else { // middle
node.prev.next = node.next;
node.next.prev = node.prev;
}
node.next = node.prev = null;
if (node._fn === fn && (!context || node._context === context)) {
this.detach(node);
}
node = next;
node = node._next;
}
return this;
};
}
/**
* Remove binding object. (may be deprecated)
*
* @param {Node} node The binding node that will be removed.
* @api public */
detach(node) {
if (!node._fn) { return; }
if (node === this._head) { // first node
this._head = node._next;
if (!this._head){
this._tail = null;
} else {
this._head._prev = null;
}
} else if (node === this._tail) { // last node
this._tail = node._prev;
this._tail._next = null;
} else { // middle
node._prev._next = node._next;
node._next._prev = node._prev;
}
node._fn = null;
node._context = null;
}
/**
* Remove all listeners.

@@ -142,18 +164,8 @@ *

removeAll() {
var node = this._head, next;
var node = this._head;
if (!node) { return this; }
while (node) {
next = node.next;
node.next = node.prev = null;
node = next;
}
this._head = this._tail = null;
return this;
};
}
}
//
// Expose the module.
//
export default MiniSignals;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc