Comparing version 1.0.2 to 1.1.0
@@ -5,2 +5,4 @@ 'use strict'; | ||
let handoff, | ||
onHold = false, | ||
holdQueue = [], | ||
interests = {}, | ||
@@ -74,8 +76,11 @@ pending = []; | ||
else if (!handoff.ignoreErrors) { | ||
throw new Error(n.name + ' was published but has no subscribers.'); | ||
} | ||
return Promise.reject(new Error(n.name + ' was published but has no subscribers.')); | ||
} | ||
function publishNotification (notification) { | ||
if (onHold) { | ||
return new Promise((resolve, reject) => { | ||
holdQueue.push({resolve, reject, notification}); | ||
}); | ||
} | ||
pending.push(notification); | ||
@@ -113,3 +118,3 @@ return notifyObjects(notification); | ||
else { | ||
interests[name].splice(priority, 0, fn); | ||
interests[name].splice(priority, priority, fn); | ||
} | ||
@@ -134,2 +139,21 @@ | ||
module.exports = handoff = {publish, subscribe, unsubscribe, __reset, ignoreErrors : false}; | ||
function hold () { | ||
onHold = true; | ||
} | ||
function resume () { | ||
onHold = false; | ||
holdQueue.forEach(item => { | ||
item.resolve(publishNotification(item.notification)); | ||
}); | ||
holdQueue = []; | ||
} | ||
module.exports = handoff = { | ||
publish, | ||
subscribe, | ||
unsubscribe, | ||
hold, | ||
resume, | ||
__reset, | ||
}; |
{ | ||
"name": "handoff", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "PubSub with Promises", | ||
@@ -5,0 +5,0 @@ "main": "handoff.js", |
@@ -12,3 +12,3 @@ # handoff.js | ||
handoff.subscribe('something', (n, message) => { | ||
return new Promise(() => setTimeout(() => resolve('received!'), 500)); | ||
return new Promise((resolve, reject) => setTimeout(() => resolve('received!'), 1000)); | ||
}); | ||
@@ -15,0 +15,0 @@ |
@@ -13,4 +13,2 @@ var mocha = require('mocha'); | ||
mocha.addFile(__dirname + '/tests.js'); | ||
mocha.run(function () { | ||
console.log('done'); | ||
}); | ||
mocha.run(); |
@@ -7,3 +7,2 @@ var handoff = require('../handoff'); | ||
handoff.__reset(); | ||
handoff.ignoreErrors = false; | ||
}); | ||
@@ -32,25 +31,31 @@ | ||
it('should throw an error if nobody is listening', done => { | ||
try { | ||
handoff.publish('sub-test'); | ||
} | ||
catch (err) { | ||
it('should reject if nobody is listening', done => { | ||
handoff.publish('sub-test').catch(err => { | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should not throw an error if nobody is listening and ignoreErrors = true', done => { | ||
it('should hold and resume notifications', function () { | ||
var timerRan = false; | ||
handoff.ignoreErrors = true; | ||
handoff.publish('sub-test'); | ||
handoff.ignoreErrors = false; | ||
done(); | ||
handoff.subscribe('sub-test', function () { | ||
return 'hello!'; | ||
}); | ||
handoff.hold(); | ||
setTimeout(function () { | ||
timerRan = true; | ||
handoff.resume(); | ||
}, 0); | ||
return handoff.publish('sub-test').then(() => { | ||
expect(timerRan).to.equal(true); | ||
}); | ||
}); | ||
it('should publish a notification with data', done => { | ||
handoff.subscribe('pub-data-test', function (n, data) { | ||
expect(data).to.deep.equal({ | ||
expect(data).to.eql({ | ||
x : 1, | ||
@@ -72,5 +77,5 @@ y : 2 | ||
handoff.subscribe('pub-args-test', function (n, arg1, arg2, arg3) { | ||
expect(arg1).to.eql(1); | ||
expect(arg2).to.eql(2); | ||
expect(arg3).to.eql('z'); | ||
expect(arg1).to.equal(1); | ||
expect(arg2).to.equal(2); | ||
expect(arg3).to.equal('z'); | ||
done(); | ||
@@ -77,0 +82,0 @@ }); |
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
11758
231