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

mediator-js

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mediator-js - npm Package Compare versions

Comparing version 0.9.3 to 0.9.4

6

lib/mediator.js
/*jslint bitwise: true, nomen: true, plusplus: true, white: true */
/*!
* Mediator.js Library v0.9.2
* Mediator.js Library v0.9.4
* https://github.com/ajacksified/Mediator.js

@@ -224,2 +224,4 @@ *

this.removeSubscriber(subscriber.id);
y--;
x--;
}else{

@@ -347,5 +349,5 @@ subscriber.update(subscriber.options);

Mediator.Subscriber = Subscriber;
Mediator.version = "0.9.2";
Mediator.version = "0.9.4";
return Mediator;
}));

@@ -1,2 +0,1 @@

/*! Mediator.js v0.9.1 | (c) 2013 Jack Lawson | https://github.com/ajacksified/Mediator.js | MIT License */
(function(root,factory){"use strict";if(typeof root.exports==="function"){root.exports.Mediator=factory()}else if(typeof define==="function"&&define.amd){define("Mediator",[],function(){root.Mediator=factory();return root.Mediator()})}else{root.Mediator=factory()}})(this,function(){"use strict";function guidGenerator(){var S4=function(){return((1+Math.random())*65536|0).toString(16).substring(1)};return S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()}function Subscriber(fn,options,context){if(!(this instanceof Subscriber)){return new Subscriber(fn,options,context)}this.id=guidGenerator();this.fn=fn;this.options=options;this.context=context;this.channel=null}Subscriber.prototype={update:function(options){if(options){this.fn=options.fn||this.fn;this.context=options.context||this.context;this.options=options.options||this.options;if(this.channel&&this.options&&this.options.priority!==undefined){this.channel.setPriority(this.id,this.options.priority)}}}};function Channel(namespace,parent){if(!(this instanceof Channel)){return new Channel(namespace)}this.namespace=namespace||"";this._subscribers=[];this._channels=[];this._parent=parent;this.stopped=false}Channel.prototype={addSubscriber:function(fn,options,context){var subscriber=new Subscriber(fn,options,context);if(options&&options.priority!==undefined){options.priority=options.priority>>0;if(options.priority<0){options.priority=0}if(options.priority>=this._subscribers.length){options.priority=this._subscribers.length-1}this._subscribers.splice(options.priority,0,subscriber)}else{this._subscribers.push(subscriber)}subscriber.channel=this;return subscriber},stopPropagation:function(){this.stopped=true},getSubscriber:function(identifier){var x=0,y=this._subscribers.length;for(x,y;x<y;x++){if(this._subscribers[x].id===identifier||this._subscribers[x].fn===identifier){return this._subscribers[x]}}},setPriority:function(identifier,priority){var oldIndex=0,x=0,sub,firstHalf,lastHalf,y;for(x=0,y=this._subscribers.length;x<y;x++){if(this._subscribers[x].id===identifier||this._subscribers[x].fn===identifier){break}oldIndex++}sub=this._subscribers[oldIndex];firstHalf=this._subscribers.slice(0,oldIndex);lastHalf=this._subscribers.slice(oldIndex+1);this._subscribers=firstHalf.concat(lastHalf);this._subscribers.splice(priority,0,sub)},addChannel:function(channel){this._channels[channel]=new Channel((this.namespace?this.namespace+":":"")+channel,this)},hasChannel:function(channel){return this._channels.hasOwnProperty(channel)},returnChannel:function(channel){return this._channels[channel]},removeSubscriber:function(identifier){var x=0,y;y=this._subscribers.length;if(!identifier){this._subscribers=[];return}for(x,y;y>x;y--){if(this._subscribers[x].fn===identifier||this._subscribers[x].id===identifier){this._subscribers[x].channel=null;this._subscribers.splice(x,1)}}},publish:function(data){var x=0,y=this._subscribers.length,called=false,subscriber,l;for(x,y;x<y;x++){if(!this.stopped){subscriber=this._subscribers[x];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}}else{subscriber.fn.apply(subscriber.context,data);called=true}}if(called&&subscriber.options&&subscriber.options!==undefined){subscriber.options.calls--;if(subscriber.options.calls<1){this.removeSubscriber(subscriber.id)}else{subscriber.update(subscriber.options)}}}if(this._parent){this._parent.publish(data)}this.stopped=false}};function Mediator(){if(!(this instanceof Mediator)){return new Mediator}this._channels=new Channel("")}Mediator.prototype={getChannel:function(namespace){var channel=this._channels,namespaceHierarchy=namespace.split(":"),x=0,y=namespaceHierarchy.length;if(namespace===""){return channel}if(namespaceHierarchy.length>0){for(x,y;x<y;x++){if(!channel.hasChannel(namespaceHierarchy[x])){channel.addChannel(namespaceHierarchy[x])}channel=channel.returnChannel(namespaceHierarchy[x])}}return channel},subscribe:function(channelName,fn,options,context){var channel=this.getChannel(channelName);options=options||{};context=context||{};return channel.addSubscriber(fn,options,context)},once:function(channelName,fn,options,context){options=options||{};options.calls=1;return this.subscribe(channelName,fn,options,context)},getSubscriber:function(identifier,channel){return this.getChannel(channel||"").getSubscriber(identifier)},remove:function(channelName,identifier){this.getChannel(channelName).removeSubscriber(identifier)},publish:function(channelName){var args=Array.prototype.slice.call(arguments,1),channel=this.getChannel(channelName);args.push(channel);this.getChannel(channelName).publish(args)}};Mediator.prototype.on=Mediator.prototype.subscribe;Mediator.prototype.bind=Mediator.prototype.subscribe;Mediator.prototype.emit=Mediator.prototype.publish;Mediator.prototype.trigger=Mediator.prototype.publish;Mediator.prototype.off=Mediator.prototype.remove;Mediator.Channel=Channel;Mediator.Subscriber=Subscriber;Mediator.version="0.9.2";return Mediator});
(function(root,factory){"use strict";if(typeof root.exports==="function"){root.exports.Mediator=factory()}else if(typeof define==="function"&&define.amd){define("mediator-js",[],function(){root.Mediator=factory();return root.Mediator()})}else{root.Mediator=factory()}})(this,function(){"use strict";function guidGenerator(){var S4=function(){return((1+Math.random())*65536|0).toString(16).substring(1)};return S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()}function Subscriber(fn,options,context){if(!(this instanceof Subscriber)){return new Subscriber(fn,options,context)}this.id=guidGenerator();this.fn=fn;this.options=options;this.context=context;this.channel=null}Subscriber.prototype={update:function(options){if(options){this.fn=options.fn||this.fn;this.context=options.context||this.context;this.options=options.options||this.options;if(this.channel&&this.options&&this.options.priority!==undefined){this.channel.setPriority(this.id,this.options.priority)}}}};function Channel(namespace,parent){if(!(this instanceof Channel)){return new Channel(namespace)}this.namespace=namespace||"";this._subscribers=[];this._channels=[];this._parent=parent;this.stopped=false}Channel.prototype={addSubscriber:function(fn,options,context){var subscriber=new Subscriber(fn,options,context);if(options&&options.priority!==undefined){options.priority=options.priority>>0;if(options.priority<0){options.priority=0}if(options.priority>=this._subscribers.length){options.priority=this._subscribers.length-1}this._subscribers.splice(options.priority,0,subscriber)}else{this._subscribers.push(subscriber)}subscriber.channel=this;return subscriber},stopPropagation:function(){this.stopped=true},getSubscriber:function(identifier){var x=0,y=this._subscribers.length;for(x,y;x<y;x++){if(this._subscribers[x].id===identifier||this._subscribers[x].fn===identifier){return this._subscribers[x]}}},setPriority:function(identifier,priority){var oldIndex=0,x=0,sub,firstHalf,lastHalf,y;for(x=0,y=this._subscribers.length;x<y;x++){if(this._subscribers[x].id===identifier||this._subscribers[x].fn===identifier){break}oldIndex++}sub=this._subscribers[oldIndex];firstHalf=this._subscribers.slice(0,oldIndex);lastHalf=this._subscribers.slice(oldIndex+1);this._subscribers=firstHalf.concat(lastHalf);this._subscribers.splice(priority,0,sub)},addChannel:function(channel){this._channels[channel]=new Channel((this.namespace?this.namespace+":":"")+channel,this)},hasChannel:function(channel){return this._channels.hasOwnProperty(channel)},returnChannel:function(channel){return this._channels[channel]},removeSubscriber:function(identifier){var x=this._subscribers.length-1;if(!identifier){this._subscribers=[];return}for(x;x>=0;x--){if(this._subscribers[x].fn===identifier||this._subscribers[x].id===identifier){this._subscribers[x].channel=null;this._subscribers.splice(x,1)}}},publish:function(data){var x=0,y=this._subscribers.length,called=false,subscriber,l;for(x,y;x<y;x++){if(!this.stopped){subscriber=this._subscribers[x];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}}else{subscriber.fn.apply(subscriber.context,data);called=true}}if(called&&subscriber.options&&subscriber.options!==undefined){subscriber.options.calls--;if(subscriber.options.calls<1){this.removeSubscriber(subscriber.id);y--;x--}else{subscriber.update(subscriber.options)}}}if(this._parent){this._parent.publish(data)}this.stopped=false}};function Mediator(){if(!(this instanceof Mediator)){return new Mediator}this._channels=new Channel("")}Mediator.prototype={getChannel:function(namespace){var channel=this._channels,namespaceHierarchy=namespace.split(":"),x=0,y=namespaceHierarchy.length;if(namespace===""){return channel}if(namespaceHierarchy.length>0){for(x,y;x<y;x++){if(!channel.hasChannel(namespaceHierarchy[x])){channel.addChannel(namespaceHierarchy[x])}channel=channel.returnChannel(namespaceHierarchy[x])}}return channel},subscribe:function(channelName,fn,options,context){var channel=this.getChannel(channelName);options=options||{};context=context||{};return channel.addSubscriber(fn,options,context)},once:function(channelName,fn,options,context){options=options||{};options.calls=1;return this.subscribe(channelName,fn,options,context)},getSubscriber:function(identifier,channel){return this.getChannel(channel||"").getSubscriber(identifier)},remove:function(channelName,identifier){this.getChannel(channelName).removeSubscriber(identifier)},publish:function(channelName){var args=Array.prototype.slice.call(arguments,1),channel=this.getChannel(channelName);args.push(channel);this.getChannel(channelName).publish(args)}};Mediator.prototype.on=Mediator.prototype.subscribe;Mediator.prototype.bind=Mediator.prototype.subscribe;Mediator.prototype.emit=Mediator.prototype.publish;Mediator.prototype.trigger=Mediator.prototype.publish;Mediator.prototype.off=Mediator.prototype.remove;Mediator.Channel=Channel;Mediator.Subscriber=Subscriber;Mediator.version="0.9.4";return Mediator});
{
"name": "mediator-js",
"version": "0.9.3",
"version": "0.9.4",
"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.3
Version 0.9.4

@@ -215,2 +215,5 @@ For more information, please see

__Version 0.9.4__
* Fixed issue with auto-removing subscribers after a maximum amount of calls
__Version 0.9.3__

@@ -217,0 +220,0 @@ * Make AMD name match npm package name (`mediator-js`). Used `Mediator.js`

@@ -54,2 +54,16 @@ var Mediator = require("../index").Mediator,

});
it("should remove a subscriber in a list of others that's been called its maximum amount of times", function(){
var spy = sinon.spy(), i;
mediator.subscribe("test", function(){});
mediator.subscribe("test", spy, { calls: 3 });
mediator.subscribe("test", function(){});
for(i = 0; i < 5; i++){
mediator.publish("test");
}
expect(spy).calledThrice;
});
});

@@ -56,0 +70,0 @@

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