-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathangular-pubsub.js
91 lines (80 loc) · 2.24 KB
/
angular-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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* UMD (Universal Module Definition) pattern
* https://github.com/umdjs/umd
* returnExports snippet
* https://github.com/umdjs/umd/blob/master/returnExports.js
*/
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define('pubsub-core',[], factory);
}
else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
}
else {
// Browser globals (root is window)
root.pubsubCore = factory();
}
}(this, function() {
'use strict';
return function() {
var channels = {};
return {
publish: function(topic) {
var args = Array.prototype.slice.call(arguments, 1);
if (!channels[topic]) {
return;
}
channels[topic].forEach(function(callback) {
callback.apply(null, args);
});
},
subscribe: function(topic, callback) {
if (!(callback instanceof Function)) {
throw new Error('callback must be a function');
}
if (!channels[topic]) {
channels[topic] = [];
}
channels[topic].push(callback);
},
unsubscribe: function(topic, callback) {
if (!channels[topic]) {
return;
}
channels[topic].forEach(function(value, index) {
if (value === callback) {
channels[topic].splice(index, 1);
}
});
}
};
};
}));
/*
* UMD (Universal Module Definition) pattern
* https://github.com/umdjs/umd
* amdWeb snippet
* https://github.com/umdjs/umd/blob/master/amdWeb.js
*/
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define('angular-pubsub',['angular', 'pubsub-core'], factory);
}
else {
// Browser globals
root.angularPubsub = factory(root.angular,
root.pubsubCore);
}
}(this, function(angular, pubsubCore) {
'use strict';
var angularPubsub = angular.module('angularPubsub', []);
return angularPubsub.factory('PubSub', pubsubCore);
}));