This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (65 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
(function(){
var psst = function(){
if (!(this instanceof psst)) {
return new psst();
}
this.subscriptions = {};
};
var bind = function(context, func){
return function(){
return func.apply(context, arguments);
}
};
psst.prototype.on = psst.prototype.subscribe = function(channel, callback){
if (channel && Object.prototype.toString.call(callback) === '[object Function]') {
if (!Object.prototype.hasOwnProperty.call(this.subscriptions, channel)) {
this.subscriptions[channel] = [];
}
this.subscriptions[channel].push(callback);
return {
channel: channel,
callback: callback
};
}
};
psst.prototype.once = function(channel, callback){
var once = this.on(channel, function(){
this.off(once);
callback.apply(this, arguments);
});
return once;
};
psst.prototype.off = psst.prototype.unsubscribe = function(subscription){
if (subscription && Object.prototype.hasOwnProperty.call(subscription, 'channel')) {
for (var i = 0, len = this.subscriptions[subscription.channel].length; i < len; i++) {
if (this.subscriptions[subscription.channel][i] === subscription.callback) {
this.subscriptions[subscription.channel].splice(i, 1);
}
}
}
};
psst.prototype.emit = psst.prototype.trigger = psst.prototype.publish = function(channel){
if (channel && Object.prototype.hasOwnProperty.call(this.subscriptions, channel)) {
for (var i = 0, len = this.subscriptions[channel].length; i < len; i++) {
this.subscriptions[channel][i].apply(this, Array.prototype.slice.call(arguments, 1));
}
}
};
// Support the v1 way of using psst.js
var __psst__ = new psst();
psst.on = psst.subscribe = bind(__psst__, psst.prototype.on);
psst.once = bind(__psst__, psst.prototype.once);
psst.off = psst.unsubscribe = bind(__psst__, psst.prototype.off);
psst.emit = psst.trigger = psst.publish = bind(__psst__, psst.prototype.emit);
// Export psst for CommonJS, AMD and the browser.
if (typeof exports === 'object') {
module.exports = psst;
} else if (typeof define === 'function' && define.amd) {
define(function() {
return psst;
});
} else {
window.psst = psst;
}
})();