Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

handoff

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

handoff - npm Package Compare versions

Comparing version 1.1.2 to 1.2.0

214

handoff.js

@@ -1,159 +0,149 @@

'use strict';
'use strict'
let onHold = false
let holdQueue = []
let interests = {}
let pending = []
let handoff,
onHold = false,
holdQueue = [],
interests = {},
pending = [];
class Notification {
constructor (name, args) {
this.name = name
this.args = args
this.status = 0
this.pointer = 0
return this
}
constructor (name, args) {
this.name = name;
this.args = args;
this.status = 0;
this.pointer = 0;
return this;
}
cancel () {
this.status = 0;
this.pointer = 0;
cancelNotification(this);
}
cancel () {
this.status = 0
this.pointer = 0
cancelNotification(this)
}
}
function notifyObjects (n) {
let name, subs
let name,
subs;
let next = function () {
if (n.status === 1 && n.pointer < subs.length) {
return new Promise((resolve, reject) => {
try {
resolve(subs[n.pointer++].apply(null, [].concat(n, n.args)));
}
catch (err) {
reject(err);
}
}).then(response => {
n.response = response;
return next();
});
let next = function () {
if (n.status === 1 && n.pointer < subs.length) {
return new Promise((resolve, reject) => {
try {
resolve(subs[n.pointer++].apply(null, [].concat(n, n.args)))
} catch (err) {
reject(err)
}
}).then(response => {
n.response = response
return next()
})
} else {
subs = null
else {
if (n.status === 1 || n.response != null) {
let response = n.response
n.cancel()
return Promise.resolve(response)
}
subs = null;
let err = new Error('Notification (' + n.name + ') was cancelled.')
err.code = 'ECANCELED'
n.cancel()
if (n.status === 1 || n.response != null) {
let response = n.response;
n.cancel();
return Promise.resolve(response);
}
return Promise.reject(err)
}
}
let err = new Error('Notification (' + n.name + ') was cancelled.');
err.code = 'ECANCELED';
n.cancel();
name = n.name
return Promise.reject(err);
}
};
if (interests[name] && interests[name].length) {
subs = interests[name].slice(0)
return next()
}
name = n.name;
let err = new Error(n.name + ' was published but has no subscribers.')
err.code = 'ENOSYS'
if (interests[name] && interests[name].length) {
subs = interests[name].slice(0);
return next();
}
let err = new Error(n.name + ' was published but has no subscribers.');
err.code = 'ENOSYS';
return Promise.reject(err);
return Promise.reject(err)
}
function publishNotification (notification) {
if (onHold) {
return new Promise((resolve, reject) => {
holdQueue.push({resolve, reject, notification});
});
}
notification.status = 1;
notification.pointer = 0;
pending.push(notification);
return notifyObjects(notification);
if (onHold) {
return new Promise((resolve, reject) => {
holdQueue.push({ resolve, reject, notification })
})
}
notification.status = 1
notification.pointer = 0
pending.push(notification)
return notifyObjects(notification)
}
function cancelNotification (notification) {
let idx = pending.indexOf(notification);
if (!~idx) {return;}
pending.splice(idx, 1);
let idx = pending.indexOf(notification)
if (!~idx) {
return
}
pending.splice(idx, 1)
}
function publish () {
let args = Array.prototype.slice.call(arguments)
let name = args[0]
let args = Array.prototype.slice.call(arguments),
name = args[0];
args = args.slice(1, args.length)
args = args.slice(1, args.length);
let notification = new Notification(name, args);
return publishNotification(notification);
let notification = new Notification(name, args)
return publishNotification(notification)
}
function subscribe (name, fn, priority) {
priority = isNaN(priority) ? -1 : priority
interests[name] = interests[name] || []
priority = isNaN(priority) ? -1 : priority;
interests[name] = interests[name] || [];
if (priority <= -1 || priority >= interests[name].length) {
interests[name].push(fn)
} else {
interests[name].splice(priority, priority, fn)
}
if (priority <= -1 || priority >= interests[name].length) {
interests[name].push(fn);
}
else {
interests[name].splice(priority, priority, fn);
}
return fn;
return fn
}
function unsubscribe (name, fn) {
if (!fn) {
interests[name] = []
return
}
let fnIndex = interests[name].indexOf(fn);
if (fnIndex > -1) {
interests[name].splice(fnIndex, 1);
}
let fnIndex = interests[name].indexOf(fn)
if (fnIndex > -1) {
interests[name].splice(fnIndex, 1)
}
}
function __reset () {
interests = {};
pending.forEach(cancelNotification);
pending = [];
interests = {}
pending.forEach(cancelNotification)
pending = []
}
function hold () {
onHold = true;
onHold = true
}
function resume () {
onHold = false;
holdQueue.forEach(item => {
item.resolve(publishNotification(item.notification));
});
holdQueue = [];
onHold = false
holdQueue.forEach(item => {
item.resolve(publishNotification(item.notification))
})
holdQueue = []
}
module.exports = handoff = {
publish,
subscribe,
unsubscribe,
hold,
resume,
__reset,
};
module.exports = {
publish,
subscribe,
unsubscribe,
hold,
resume,
__reset
}
{
"name": "handoff",
"version": "1.1.2",
"version": "1.2.0",
"description": "PubSub with Promises",
"main": "handoff.js",
"scripts": {
"test": "node tests/run.js"
"test": "node ./node_modules/standard/bin/cmd | node ./node_modules/snazzy/bin/cmd && node tests/run.js"
},

@@ -30,3 +30,7 @@ "keywords": [

"mocha": "^2.3.4"
},
"dependencies": {
"snazzy": "^7.0.0",
"standard": "^10.0.2"
}
}

@@ -62,8 +62,12 @@ # handoff.js

### handoff.ignoreErrors
### Rejections
By default, handoff will throw an `Error` if you `publish()` something that nobody is subscribed to.
This is to help alleviate some of the issues that PubSub and loose coupling introduce. You can disable this behavior by doing `handoff.ignoreErrors = true`
Handoff will reject a promise in two instances :
- A subscriber cancelled the Notification. In these cases the Error object
passed to the rejection handler will have a `code` property with a value of `ECANCELED`.
- There are no subscribers for a notification. In these cases the Error object
passed to the rejection handler will have a `code` property with a value of `ENOSYS`.
#### License

@@ -70,0 +74,0 @@

@@ -1,13 +0,14 @@

var mocha = require('mocha');
global.expect = require('chai').expect
global.expect = require('chai').expect;
const path = require('path')
const Mocha = require('mocha')
mocha = new mocha({
ui : 'bdd',
reporter : 'spec'
});
const mocha = new Mocha({
ui: 'bdd',
reporter: 'spec'
})
mocha.checkLeaks();
mocha.checkLeaks()
mocha.addFile(__dirname + '/tests.js');
mocha.run();
mocha.addFile(path.join(__dirname, 'tests.js'))
mocha.run()

@@ -1,142 +0,133 @@

var handoff = require('../handoff');
const handoff = require('../handoff')
const ignore = () => {}
/* global expect, describe, it, afterEach */
describe('pub/sub', () => {
afterEach(() => {
handoff.__reset()
})
afterEach(() => {
handoff.__reset();
});
it('should publish/subscribe and unsubscribe to a notification successfully', done => {
let a = 0
let subscriber1
it('should publish/subscribe and unsubscribe to a notification successfully', done => {
subscriber1 = handoff.subscribe('sub-test', () => {
a++
})
var a = 0,
subscriber1;
handoff.publish('sub-test')
handoff.unsubscribe('sub-test', subscriber1)
subscriber1 = handoff.subscribe('sub-test', () => {
a ++;
});
handoff.publish('sub-test').catch(ignore)
handoff.publish('sub-test');
handoff.unsubscribe('sub-test', subscriber1);
expect(a).to.equal(1)
done()
})
try {
handoff.publish('sub-test');
} catch (err) {}
it('should reject if nobody is listening', done => {
handoff.publish('sub-test').catch(err => {
ignore(err)
done()
})
})
expect(a).to.equal(1);
it('should hold and resume notifications', function () {
let timerRan = false
done();
});
handoff.subscribe('sub-test', function () {
return 'hello!'
})
it('should reject if nobody is listening', done => {
handoff.publish('sub-test').catch(err => {
done();
});
});
handoff.hold()
setTimeout(function () {
timerRan = true
handoff.resume()
}, 0)
it('should hold and resume notifications', function () {
var timerRan = false;
return handoff.publish('sub-test').then(() => {
expect(timerRan).to.equal(true)
})
})
handoff.subscribe('sub-test', function () {
return 'hello!';
});
it('should publish a notification with data', done => {
handoff.subscribe('pub-data-test', function (n, data) {
expect(data).to.eql({
x: 1,
y: 2
})
handoff.hold();
setTimeout(function () {
timerRan = true;
handoff.resume();
}, 0);
done()
})
return handoff.publish('sub-test').then(() => {
expect(timerRan).to.equal(true);
});
});
handoff.publish('pub-data-test', {
x: 1,
y: 2
})
})
it('should publish a notification with multiple arguments', done => {
handoff.subscribe('pub-args-test', function (n, arg1, arg2, arg3) {
expect(arg1).to.equal(1)
expect(arg2).to.equal(2)
expect(arg3).to.equal('z')
done()
})
it('should publish a notification with data', done => {
handoff.publish('pub-args-test', 1, 2, 'z')
})
handoff.subscribe('pub-data-test', function (n, data) {
expect(data).to.eql({
x : 1,
y : 2
});
describe('notifications', () => {
it('should support subscribers that return promises', done => {
let didHold = false
done();
});
handoff.subscribe('hold-test-2', function (n) {
return new Promise(function (resolve, reject) {
setTimeout(() => {
didHold = true
resolve()
}, 10)
})
})
handoff.publish('pub-data-test', {
x : 1,
y : 2
});
});
handoff.subscribe('hold-test-2', function (n) {
if (didHold) {
done()
}
})
it('should publish a notification with multiple arguments', done => {
handoff.publish('hold-test-2')
})
handoff.subscribe('pub-args-test', function (n, arg1, arg2, arg3) {
expect(arg1).to.equal(1);
expect(arg2).to.equal(2);
expect(arg3).to.equal('z');
done();
});
it('should support subscribers that return values', done => {
handoff.subscribe('respond-test-2', function (n) {
expect(n).to.be.an('object')
return {x: 1, y: 2}
})
handoff.publish('pub-args-test', 1, 2, 'z');
});
handoff.publish('respond-test-2', {}).then(function (obj) {
expect(obj).to.eql({
x: 1,
y: 2
})
describe('notifications', () => {
done()
})
})
it('should support subscribers that return promises', done => {
it('should be able to cancel notifications', function () {
handoff.subscribe('cancel-test-2', function (n) {
n.cancel()
return 'cancelled'
})
var didHold = false;
handoff.subscribe('cancel-test-2', function (n) {
return 'not cancelled'
})
handoff.subscribe('hold-test-2', function (n) {
return new Promise(function (resolve, reject) {
setTimeout(() => {
didHold = true;
resolve();
}, 10);
});
});
handoff.subscribe('hold-test-2', function (n) {
if (didHold) {
done();
}
});
handoff.publish('hold-test-2');
});
it('should support subscribers that return values', done => {
handoff.subscribe('respond-test-2', function (n) {
expect(n).to.be.an('object');
return {x : 1, y : 2};
});
handoff.publish('respond-test-2', {}).then(function (obj) {
expect(obj).to.eql({
x : 1,
y : 2
});
done();
});
});
it('should be able to cancel notifications', function () {
handoff.subscribe('cancel-test-2', function (n) {
n.cancel();
return 'cancelled';
});
handoff.subscribe('cancel-test-2', function (n) {
return 'not cancelled';
});
return handoff.publish('cancel-test-2').then(response => {
expect(response).to.equal('cancelled');
});
});
});
});
return handoff.publish('cancel-test-2').then(response => {
expect(response).to.equal('cancelled')
})
})
})
})
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