-
Notifications
You must be signed in to change notification settings - Fork 77
/
mediator.min.js
13 lines (13 loc) · 4.73 KB
/
mediator.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
* Mediator.js Library v0.11.0
* https://github.com/ajacksified/Mediator.js
*
* Copyright 2018, Jack Lawson
* MIT Licensed (http://www.opensource.org/licenses/mit-license.php)
*
* For more information: http://thejacklawson.com/2011/06/mediators-for-modularized-asynchronous-programming-in-javascript/index.html
* Project on GitHub: https://github.com/ajacksified/Mediator.js
*
* Last update: 22 Mar 2018
*/
!function(t,s){"use strict";"function"==typeof define&&define.amd?define([],function(){return t.Mediator=s(),t.Mediator}):"undefined"!=typeof exports?exports.Mediator=s():t.Mediator=s()}(this,function(){"use strict";function t(s,i,e){if(!(this instanceof t))return new t(s,i,e);var n;this.id=(n=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)})()+n()+"-"+n()+"-"+n()+"-"+n()+"-"+n()+n()+n(),this.fn=s,this.options=i,this.context=e,this.channel=null}function s(t,i){if(!(this instanceof s))return new s(t);this.namespace=t||"",this._subscribers=[],this._channels={},this._parent=i,this.stopped=!1}function i(){if(!(this instanceof i))return new i;this._channels=new s("")}return t.prototype.update=function(t){t&&(this.fn=t.fn||this.fn,this.context=t.context||this.context,this.options=t.options||this.options,this.channel&&this.options&&void 0!==this.options.priority&&this.channel.setPriority(this.id,this.options.priority))},s.prototype.channelPrioritySort=function(t,s){return t.options.priority>s.options.priority?-1:t.options.priority<s.options.priority?1:0},s.prototype.addSubscriber=function(s,i,e){i||(i={priority:0}),i.priority=0|i.priority;var n=new t(s,i,e);return n.channel=this,this._subscribers.push(n),this._subscribers.sort(this.channelPrioritySort),n},s.prototype.stopPropagation=function(){this.stopped=!0},s.prototype.getSubscriber=function(t){for(var s=0,i=this._subscribers.length;s<i;s++)if(this._subscribers[s].id===t||this._subscribers[s].fn===t)return this._subscribers[s]},s.prototype.setPriority=function(t,s){for(var i=0,e=this._subscribers.length;i<e;i++)if(this._subscribers[i].id===t||this._subscribers[i].fn===t){this._subscribers[i].priority=s;break}this._subscribers.sort(this.channelPrioritySort)},s.prototype.addChannel=function(t){this._channels[t]=new s((this.namespace?this.namespace+":":"")+t,this)},s.prototype.hasChannel=function(t){return this._channels.hasOwnProperty(t)},s.prototype.returnChannel=function(t){return this._channels[t]},s.prototype.removeSubscriber=function(t,s){var i=this._subscribers.length-1;if(s=void 0===s||s,t)for(;i>=0;i--)this._subscribers[i].fn!==t&&this._subscribers[i].id!==t||(this._subscribers[i].channel=null,this._subscribers.splice(i,1));else this._subscribers=[];s&&this.autoCleanChannel();i=this._subscribers.length-1;if(t)for(;i>=0;i--)this._subscribers[i].fn!==t&&this._subscribers[i].id!==t||(this._subscribers[i].channel=null,this._subscribers.splice(i,1));else this._subscribers=[]},s.prototype.autoCleanChannel=function(){if(0===this._subscribers.length){for(var t in this._channels)if(this._channels.hasOwnProperty(t))return;if(null!=this._parent){var s=this.namespace.split(":"),i=s[s.length-1];this._parent.removeChannel(i)}}},s.prototype.removeChannel=function(t){this.hasChannel(t)&&(this._channels[t]=null,delete this._channels[t],this.autoCleanChannel())},s.prototype.publish=function(t){for(var s,i,e,n=0,r=this._subscribers.length,o=!1;n<r;n++)o=!1,s=this._subscribers[n],this.stopped||(i=this._subscribers.length,void 0!==s.options&&"function"==typeof s.options.predicate?s.options.predicate.apply(s.context,t)&&(o=!0):o=!0),o&&(s.options&&void 0!==s.options.calls&&(s.options.calls--,s.options.calls<1&&this.removeSubscriber(s.id)),s.fn.apply(s.context,t),r=e=this._subscribers.length,e===i-1&&n--);this._parent&&this._parent.publish(t),this.stopped=!1},i.prototype.getChannel=function(t,s){var i=this._channels,e=t.split(":"),n=0,r=e.length;if(""===t)return i;if(e.length>0)for(;n<r;n++){if(!i.hasChannel(e[n])){if(s)break;i.addChannel(e[n])}i=i.returnChannel(e[n])}return i},i.prototype.subscribe=function(t,s,i,e){return i=i||{},e=e||{},this.getChannel(t||"",!1).addSubscriber(s,i,e)},i.prototype.once=function(t,s,i,e){return(i=i||{}).calls=1,this.subscribe(t,s,i,e)},i.prototype.getSubscriber=function(t,s){var i=this.getChannel(s||"",!0);return i.namespace!==s?null:i.getSubscriber(t)},i.prototype.remove=function(t,s,i){var e=this.getChannel(t||"",!0);if(e.namespace!==t)return!1;e.removeSubscriber(s,i)},i.prototype.publish=function(t){var s=this.getChannel(t||"",!1);if(s.namespace!==t)return null;var i=Array.prototype.slice.call(arguments,1);i.push(s),s.publish(i)},i.prototype.on=i.prototype.subscribe,i.prototype.bind=i.prototype.subscribe,i.prototype.emit=i.prototype.publish,i.prototype.trigger=i.prototype.publish,i.prototype.off=i.prototype.remove,i.Channel=s,i.Subscriber=t,i.version="0.10.1",i});