Comparing version
@@ -0,1 +1,4 @@ | ||
## v0.1.1 - Feb 20 2018 | ||
+ Adds support for creating a subscription with a config object | ||
## v0.1.0 - Feb 14 2018 | ||
@@ -2,0 +5,0 @@ + Adds RegExp support to subscriptions |
@@ -121,4 +121,4 @@ 'use strict'; | ||
function scheduleCall(callback, payload, topic) { | ||
setTimeout(callback, 0, payload, topic); | ||
function scheduleCall(handler, payload, def, topic) { | ||
setTimeout(handler, 0, valueOrDefault(payload, def), topic); | ||
} | ||
@@ -135,3 +135,3 @@ | ||
subs.forEach(function (sub) { | ||
scheduleCall(sub.subFn, store[topic], topic); | ||
scheduleCall(sub.handler, store[topic], sub.default, topic); | ||
}); | ||
@@ -142,13 +142,22 @@ } | ||
function subscribe(topic, callback, def) { | ||
var subscriber = { | ||
function subscribe(topic, handler, def) { | ||
var subscription = { | ||
topic: topic, | ||
default: def, | ||
subFn: function subFn(payload, topic) { | ||
callback(valueOrDefault(payload, def), topic); | ||
} | ||
default: undefined, | ||
handler: function handler() {} | ||
}; | ||
addSub(subscriber); | ||
if (typeof handler === 'function') { | ||
subscription.default = def; | ||
subscription.handler = handler; | ||
} else if ((typeof handler === 'undefined' ? 'undefined' : _typeof(handler)) === 'object') { | ||
for (var key in handler) { | ||
if (subscription.hasOwnProperty(key)) { | ||
subscription[key] = handler[key]; | ||
} | ||
} | ||
} | ||
addSub(subscription); | ||
var stored = void 0; | ||
@@ -174,3 +183,3 @@ | ||
if (isSet(item.val)) { | ||
scheduleCall(subscriber.subFn, item.val, item.topic); | ||
scheduleCall(subscription.handler, item.val, subscription.default, item.topic); | ||
} | ||
@@ -180,3 +189,3 @@ }); | ||
return function () { | ||
removeSub(subscriber); | ||
removeSub(subscription); | ||
}; | ||
@@ -183,0 +192,0 @@ } |
{ | ||
"name": "pubst", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Basic JavaScript Pub/Sub Event Emitter", | ||
@@ -5,0 +5,0 @@ "main": "lib/pubst.js", |
@@ -104,4 +104,4 @@ /* | ||
function scheduleCall(callback, payload, topic) { | ||
setTimeout(callback, 0, payload, topic); | ||
function scheduleCall(handler, payload, def, topic) { | ||
setTimeout(handler, 0, valueOrDefault(payload, def), topic); | ||
} | ||
@@ -118,3 +118,3 @@ | ||
subs.forEach(sub => { | ||
scheduleCall(sub.subFn, store[topic], topic); | ||
scheduleCall(sub.handler, store[topic], sub.default, topic); | ||
}); | ||
@@ -125,13 +125,22 @@ } | ||
function subscribe(topic, callback, def) { | ||
const subscriber = { | ||
function subscribe(topic, handler, def) { | ||
const subscription = { | ||
topic, | ||
default: def, | ||
subFn: (payload, topic) => { | ||
callback(valueOrDefault(payload, def), topic); | ||
} | ||
default: undefined, | ||
handler: () => {} | ||
}; | ||
addSub(subscriber); | ||
if (typeof handler === 'function') { | ||
subscription.default = def; | ||
subscription.handler = handler; | ||
} else if (typeof handler === 'object') { | ||
for (const key in handler) { | ||
if (subscription.hasOwnProperty(key)) { | ||
subscription[key] = handler[key]; | ||
} | ||
} | ||
} | ||
addSub(subscription); | ||
let stored; | ||
@@ -155,3 +164,3 @@ | ||
if (isSet(item.val)) { | ||
scheduleCall(subscriber.subFn, item.val, item.topic); | ||
scheduleCall(subscription.handler, item.val, subscription.default, item.topic); | ||
} | ||
@@ -161,3 +170,3 @@ }); | ||
return () => { | ||
removeSub(subscriber); | ||
removeSub(subscription); | ||
}; | ||
@@ -164,0 +173,0 @@ } |
@@ -157,2 +157,32 @@ const clearRequire = require('clear-require'); | ||
it('allows subscriptions to be made with config objects', () => { | ||
const testPayload = 'test payload'; | ||
const defaultPayload = 'default payload'; | ||
const handler1 = sinon.spy(); | ||
const handler2 = sinon.spy(); | ||
pubst.subscribe(TEST_TOPIC_1, { | ||
handler: handler1, | ||
default: defaultPayload | ||
}); | ||
pubst.subscribe(TEST_TOPIC_1, handler2, defaultPayload); | ||
pubst.publish(TEST_TOPIC_1, testPayload); | ||
clock.tick(1); | ||
expect(handler1).to.have.been.calledWith(testPayload, TEST_TOPIC_1); | ||
expect(handler2).to.have.been.calledWith(testPayload, TEST_TOPIC_1); | ||
handler1.resetHistory(); | ||
handler2.resetHistory(); | ||
pubst.publish(TEST_TOPIC_1, null); | ||
clock.tick(1); | ||
expect(handler1).to.have.been.calledWith(defaultPayload, TEST_TOPIC_1); | ||
expect(handler2).to.have.been.calledWith(defaultPayload, TEST_TOPIC_1); | ||
}); | ||
it('calls all the subscribers for a topic', () => { | ||
@@ -159,0 +189,0 @@ const testPayload = 'test payload'; |
45139
3.88%785
4.95%