-
Notifications
You must be signed in to change notification settings - Fork 6
/
PubSub.js
42 lines (38 loc) · 1.17 KB
/
PubSub.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
define([
], function(
) {
"use strict";
/**
* Simple module for signaling.
*
* On a call to `pubSub.publish("channel-name"[, optional, arg1, ... args])`
* the callback of all subscriptions of "channel-name" will be invoked
* with the optional arguments given to `publish`:
* `callback(optional, arg1, ... args)`
*
* The subscriptions are always invoked in the order of subscription.
*
* There's no way to cancel subscription yet.
*/
function PubSub() {
this._callbacks = Object.create(null);
}
var _p = PubSub.prototype;
_p.subscribe = function(channel, callback) {
var callbacks = this._callbacks[channel];
if(!callbacks)
this._callbacks[channel] = callbacks = [];
callbacks.push(callback);
};
_p.publish = function(channel /* , args, ... */) {
var i, l
, args = []
, callbacks = this._callbacks[channel] || []
;
for(i=1,l=arguments.length;i<l;i++)
args.push(arguments[i]);
for(i=0,l=callbacks.length;i<l;i++)
callbacks[i].apply(null, args);
};
return PubSub;
});