mediator-js
Advanced tools
Comparing version 0.9.7 to 0.9.8
/*jslint bitwise: true, nomen: true, plusplus: true, white: true */ | ||
/*! | ||
* Mediator.js Library v0.9.7 | ||
* Mediator.js Library v0.9.8 | ||
* https://github.com/ajacksified/Mediator.js | ||
@@ -19,6 +19,3 @@ * | ||
if(typeof exports !== 'undefined') { | ||
// Node/CommonJS | ||
exports.Mediator = factory(); | ||
} else if(typeof define === 'function' && define.amd) { | ||
if(typeof define === 'function' && define.amd) { | ||
// AMD | ||
@@ -29,2 +26,5 @@ define('mediator-js', [], function() { | ||
}); | ||
} else if (typeof exports !== 'undefined') { | ||
// Node/CommonJS | ||
exports.Mediator = factory(); | ||
} else { | ||
@@ -91,3 +91,3 @@ // Browser global | ||
this._subscribers = []; | ||
this._channels = []; | ||
this._channels = {}; | ||
this._parent = parent; | ||
@@ -204,3 +204,3 @@ this.stopped = false; | ||
y = this._subscribers.length, | ||
called = false, | ||
shouldCall = false, | ||
subscriber, l, | ||
@@ -211,29 +211,37 @@ subsBefore,subsAfter; | ||
for(x, y; x < y; x++) { | ||
called = false; | ||
// By default set the flag to false | ||
shouldCall = false; | ||
subscriber = this._subscribers[x]; | ||
if(!this.stopped){ | ||
subscriber = this._subscribers[x]; | ||
subsBefore = this._subscribers.length; | ||
if(subscriber.options !== undefined && typeof subscriber.options.predicate === "function"){ | ||
if(subscriber.options.predicate.apply(subscriber.context, data)){ | ||
subscriber.fn.apply(subscriber.context, data); | ||
called = true; | ||
// The predicate matches, the callback function should be called | ||
shouldCall = true; | ||
} | ||
}else{ | ||
subsBefore = this._subscribers.length; | ||
subscriber.fn.apply(subscriber.context, data); | ||
subsAfter = this._subscribers.length; | ||
y = subsAfter; | ||
if (subsAfter === subsBefore - 1){ | ||
x--; | ||
} | ||
called = true; | ||
// There is no predicate to match, the callback should always be called | ||
shouldCall = true; | ||
} | ||
} | ||
if(called && subscriber.options && subscriber.options !== undefined){ | ||
subscriber.options.calls--; | ||
// Check if the callback should be called | ||
if(shouldCall) { | ||
// Check if the subscriber has options and if this include the calls options | ||
if (subscriber.options && subscriber.options.calls !== undefined){ | ||
// Decrease the number of calls left by one | ||
subscriber.options.calls--; | ||
// Once the number of calls left reaches zero or less we need to remove the subscriber | ||
if(subscriber.options.calls < 1){ | ||
this.removeSubscriber(subscriber.id); | ||
} | ||
} | ||
// Now we call the callback, if this in turns publishes to the same channel it will no longer | ||
// cause the callback to be called as we just removed it as a subscriber | ||
subscriber.fn.apply(subscriber.context, data); | ||
if(subscriber.options.calls < 1){ | ||
this.removeSubscriber(subscriber.id); | ||
y--; | ||
subsAfter = this._subscribers.length; | ||
y = subsAfter; | ||
if (subsAfter === subsBefore - 1){ | ||
x--; | ||
@@ -266,8 +274,9 @@ } | ||
// Returns a channel instance based on namespace, for example | ||
// application:chat:message:received | ||
// application:chat:message:received. If readOnly is true we | ||
// will refrain from creating non existing channels. | ||
getChannel: function(namespace){ | ||
getChannel: function(namespace, readOnly){ | ||
var channel = this._channels, | ||
namespaceHierarchy = namespace.split(':'), | ||
x = 0, | ||
x = 0, | ||
y = namespaceHierarchy.length; | ||
@@ -283,3 +292,7 @@ | ||
if(!channel.hasChannel(namespaceHierarchy[x])){ | ||
channel.addChannel(namespaceHierarchy[x]); | ||
if (readOnly) { | ||
break; | ||
} else { | ||
channel.addChannel(namespaceHierarchy[x]); | ||
} | ||
} | ||
@@ -301,3 +314,3 @@ | ||
subscribe: function(channelName, fn, options, context){ | ||
var channel = this.getChannel(channelName); | ||
var channel = this.getChannel(channelName || "", false); | ||
@@ -326,4 +339,11 @@ options = options || {}; | ||
getSubscriber: function(identifier, channel){ | ||
return this.getChannel(channel || "").getSubscriber(identifier); | ||
getSubscriber: function(identifier, channelName){ | ||
var channel = this.getChannel(channelName || "", true); | ||
// We have to check if channel within the hierarchy exists and if it is | ||
// an exact match for the requested channel | ||
if (channel.namespace !== channelName) { | ||
return null; | ||
} | ||
return channel.getSubscriber(identifier); | ||
}, | ||
@@ -335,3 +355,8 @@ | ||
remove: function(channelName, identifier){ | ||
this.getChannel(channelName).removeSubscriber(identifier); | ||
var channel = this.getChannel(channelName || "", true); | ||
if (channel.namespace !== channelName) { | ||
return false; | ||
} | ||
channel.removeSubscriber(identifier); | ||
}, | ||
@@ -345,8 +370,12 @@ | ||
publish: function(channelName){ | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
channel = this.getChannel(channelName); | ||
var channel = this.getChannel(channelName || "", true); | ||
if (channel.namespace !== channelName) { | ||
return null; | ||
} | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
args.push(channel); | ||
this.getChannel(channelName).publish(args); | ||
channel.publish(args); | ||
} | ||
@@ -366,6 +395,5 @@ }; | ||
Mediator.Subscriber = Subscriber; | ||
Mediator.version = "0.9.7"; | ||
Mediator.version = "0.9.8"; | ||
return Mediator; | ||
})); | ||
@@ -1,1 +0,1 @@ | ||
!function(a,b){"use strict";"undefined"!=typeof exports?exports.Mediator=b():"function"==typeof define&&define.amd?define("mediator-js",[],function(){return a.Mediator=b(),a.Mediator}):a.Mediator=b()}(this,function(){"use strict";function a(){var a=function(){return(0|65536*(1+Math.random())).toString(16).substring(1)};return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()}function b(c,d,e){return this instanceof b?(this.id=a(),this.fn=c,this.options=d,this.context=e,this.channel=null,void 0):new b(c,d,e)}function c(a,b){return this instanceof c?(this.namespace=a||"",this._subscribers=[],this._channels=[],this._parent=b,this.stopped=!1,void 0):new c(a)}function d(){return this instanceof d?(this._channels=new c(""),void 0):new d}return b.prototype={update:function(a){a&&(this.fn=a.fn||this.fn,this.context=a.context||this.context,this.options=a.options||this.options,this.channel&&this.options&&void 0!==this.options.priority&&this.channel.setPriority(this.id,this.options.priority))}},c.prototype={addSubscriber:function(a,c,d){var e=new b(a,c,d);return c&&void 0!==c.priority?(c.priority=c.priority>>0,c.priority<0&&(c.priority=0),c.priority>=this._subscribers.length&&(c.priority=this._subscribers.length-1),this._subscribers.splice(c.priority,0,e)):this._subscribers.push(e),e.channel=this,e},stopPropagation:function(){this.stopped=!0},getSubscriber:function(a){var b=0,c=this._subscribers.length;for(c;c>b;b++)if(this._subscribers[b].id===a||this._subscribers[b].fn===a)return this._subscribers[b]},setPriority:function(a,b){var e,f,g,h,c=0,d=0;for(d=0,h=this._subscribers.length;h>d&&this._subscribers[d].id!==a&&this._subscribers[d].fn!==a;d++)c++;e=this._subscribers[c],f=this._subscribers.slice(0,c),g=this._subscribers.slice(c+1),this._subscribers=f.concat(g),this._subscribers.splice(b,0,e)},addChannel:function(a){this._channels[a]=new c((this.namespace?this.namespace+":":"")+a,this)},hasChannel:function(a){return this._channels.hasOwnProperty(a)},returnChannel:function(a){return this._channels[a]},removeSubscriber:function(a){var b=this._subscribers.length-1;if(!a)return this._subscribers=[],void 0;for(b;b>=0;b--)(this._subscribers[b].fn===a||this._subscribers[b].id===a)&&(this._subscribers[b].channel=null,this._subscribers.splice(b,1))},publish:function(a){var e,g,h,b=0,c=this._subscribers.length,d=!1;for(c;c>b;b++)this.stopped||(e=this._subscribers[b],void 0!==e.options&&"function"==typeof e.options.predicate?e.options.predicate.apply(e.context,a)&&(e.fn.apply(e.context,a),d=!0):(g=this._subscribers.length,e.fn.apply(e.context,a),h=this._subscribers.length,c=h,h===g-1&&b--,d=!0)),d&&e.options&&void 0!==e.options&&(e.options.calls--,e.options.calls<1&&(this.removeSubscriber(e.id),c--,b--));this._parent&&this._parent.publish(a),this.stopped=!1}},d.prototype={getChannel:function(a){var b=this._channels,c=a.split(":"),d=0,e=c.length;if(""===a)return b;if(c.length>0)for(e;e>d;d++)b.hasChannel(c[d])||b.addChannel(c[d]),b=b.returnChannel(c[d]);return b},subscribe:function(a,b,c,d){var e=this.getChannel(a);return c=c||{},d=d||{},e.addSubscriber(b,c,d)},once:function(a,b,c,d){return c=c||{},c.calls=1,this.subscribe(a,b,c,d)},getSubscriber:function(a,b){return this.getChannel(b||"").getSubscriber(a)},remove:function(a,b){this.getChannel(a).removeSubscriber(b)},publish:function(a){var b=Array.prototype.slice.call(arguments,1),c=this.getChannel(a);b.push(c),this.getChannel(a).publish(b)}},d.prototype.on=d.prototype.subscribe,d.prototype.bind=d.prototype.subscribe,d.prototype.emit=d.prototype.publish,d.prototype.trigger=d.prototype.publish,d.prototype.off=d.prototype.remove,d.Channel=c,d.Subscriber=b,d.version="0.9.6",d}); | ||
!function(a,b){"use strict";"function"==typeof define&&define.amd?define("mediator-js",[],function(){return a.Mediator=b(),a.Mediator}):"undefined"!=typeof exports?exports.Mediator=b():a.Mediator=b()}(this,function(){"use strict";function a(){var a=function(){return(0|65536*(1+Math.random())).toString(16).substring(1)};return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()}function b(c,d,e){return this instanceof b?(this.id=a(),this.fn=c,this.options=d,this.context=e,this.channel=null,void 0):new b(c,d,e)}function c(a,b){return this instanceof c?(this.namespace=a||"",this._subscribers=[],this._channels={},this._parent=b,this.stopped=!1,void 0):new c(a)}function d(){return this instanceof d?(this._channels=new c(""),void 0):new d}return b.prototype={update:function(a){a&&(this.fn=a.fn||this.fn,this.context=a.context||this.context,this.options=a.options||this.options,this.channel&&this.options&&void 0!==this.options.priority&&this.channel.setPriority(this.id,this.options.priority))}},c.prototype={addSubscriber:function(a,c,d){var e=new b(a,c,d);return c&&void 0!==c.priority?(c.priority=c.priority>>0,c.priority<0&&(c.priority=0),c.priority>=this._subscribers.length&&(c.priority=this._subscribers.length-1),this._subscribers.splice(c.priority,0,e)):this._subscribers.push(e),e.channel=this,e},stopPropagation:function(){this.stopped=!0},getSubscriber:function(a){var b=0,c=this._subscribers.length;for(c;c>b;b++)if(this._subscribers[b].id===a||this._subscribers[b].fn===a)return this._subscribers[b]},setPriority:function(a,b){var e,f,g,h,c=0,d=0;for(d=0,h=this._subscribers.length;h>d&&this._subscribers[d].id!==a&&this._subscribers[d].fn!==a;d++)c++;e=this._subscribers[c],f=this._subscribers.slice(0,c),g=this._subscribers.slice(c+1),this._subscribers=f.concat(g),this._subscribers.splice(b,0,e)},addChannel:function(a){this._channels[a]=new c((this.namespace?this.namespace+":":"")+a,this)},hasChannel:function(a){return this._channels.hasOwnProperty(a)},returnChannel:function(a){return this._channels[a]},removeSubscriber:function(a){var b=this._subscribers.length-1;if(!a)return this._subscribers=[],void 0;for(b;b>=0;b--)(this._subscribers[b].fn===a||this._subscribers[b].id===a)&&(this._subscribers[b].channel=null,this._subscribers.splice(b,1))},publish:function(a){var e,g,h,b=0,c=this._subscribers.length,d=!1;for(c;c>b;b++)d=!1,e=this._subscribers[b],this.stopped||(g=this._subscribers.length,void 0!==e.options&&"function"==typeof e.options.predicate?e.options.predicate.apply(e.context,a)&&(d=!0):d=!0),d&&(e.options&&void 0!==e.options.calls&&(e.options.calls--,e.options.calls<1&&this.removeSubscriber(e.id)),e.fn.apply(e.context,a),h=this._subscribers.length,c=h,h===g-1&&b--);this._parent&&this._parent.publish(a),this.stopped=!1}},d.prototype={getChannel:function(a,b){var c=this._channels,d=a.split(":"),e=0,f=d.length;if(""===a)return c;if(d.length>0)for(f;f>e;e++){if(!c.hasChannel(d[e])){if(b)break;c.addChannel(d[e])}c=c.returnChannel(d[e])}return c},subscribe:function(a,b,c,d){var e=this.getChannel(a||"",!1);return c=c||{},d=d||{},e.addSubscriber(b,c,d)},once:function(a,b,c,d){return c=c||{},c.calls=1,this.subscribe(a,b,c,d)},getSubscriber:function(a,b){var c=this.getChannel(b||"",!0);return c.namespace!==b?null:c.getSubscriber(a)},remove:function(a,b){var c=this.getChannel(a||"",!0);return c.namespace!==a?!1:(c.removeSubscriber(b),void 0)},publish:function(a){var b=this.getChannel(a||"",!0);if(b.namespace!==a)return null;var c=Array.prototype.slice.call(arguments,1);c.push(b),b.publish(c)}},d.prototype.on=d.prototype.subscribe,d.prototype.bind=d.prototype.subscribe,d.prototype.emit=d.prototype.publish,d.prototype.trigger=d.prototype.publish,d.prototype.off=d.prototype.remove,d.Channel=c,d.Subscriber=b,d.version="0.9.8",d}); |
{ | ||
"name": "mediator-js", | ||
"version": "0.9.7", | ||
"version": "0.9.8", | ||
"description": "Flexible event management. Implementation of the mediator pattern.", | ||
@@ -5,0 +5,0 @@ "author": "Jack Lawson <jlawson@olivinelabs.com>", |
@@ -5,3 +5,3 @@ Mediator.js | ||
Version 0.9.7 | ||
Version 0.9.8 | ||
@@ -209,2 +209,6 @@ * [View the documentation](http://thejacklawson.com/Mediator.js/) | ||
__Version 0.9.8__ | ||
* Accepted a ton of PRs from [tbusser](https://github.com/tbusser) that fixed | ||
some issues and improved performance. | ||
__Version 0.9.7__ | ||
@@ -211,0 +215,0 @@ * Fixed bug where subscribers that failed predicates were decrementing calls. |
@@ -118,3 +118,3 @@ var Mediator = require("../index").Mediator, | ||
expect(spy).called; | ||
expect(spy).called; | ||
expect(spy2).called; | ||
}); | ||
@@ -171,2 +171,23 @@ | ||
it("should allow subscriber to remove itself", function(){ | ||
var removerCalled = false; | ||
var predicate = function(data){ | ||
return true; | ||
}; | ||
var remover = function(){ | ||
removerCalled = true; | ||
mediator.remove("test", sub.id); | ||
}; | ||
var spy1 = sinon.spy(); | ||
var sub = mediator.subscribe("test", remover, {predicate: predicate}); | ||
mediator.subscribe("test", spy1); | ||
mediator.publish("test"); | ||
expect(removerCalled).to.be.true; | ||
expect(spy1).called; | ||
expect(mediator.getChannel("test")._subscribers.length).to.equal(1); | ||
}); | ||
it("should remove subscribers for a given channel / named function pair", function(){ | ||
@@ -330,2 +351,16 @@ var spy = sinon.spy(), | ||
}); | ||
it("should publish to parents of non-existing namespaces", function(){ | ||
var spy = sinon.spy(), | ||
spy2 = sinon.spy(); | ||
mediator.subscribe("test:test1:test2", spy); | ||
mediator.subscribe("test", spy2); | ||
mediator.publish("test:test1", "data"); | ||
expect(spy).not.called; | ||
expect(spy2).called; | ||
}); | ||
}); | ||
@@ -332,0 +367,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
128523
2900
289