forked from gregberge/angular-primus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-primus.js
More file actions
160 lines (129 loc) · 4.11 KB
/
angular-primus.js
File metadata and controls
160 lines (129 loc) · 4.11 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*! Angular primus v1.0.0 | (c) 2013 Greg Bergé | License MIT */
angular
.module('primus', [])
.provider('primus', primusProvider);
function primusProvider() {
var provider = this;
/**
* Expose primus service.
*/
this.$get = ['$rootScope', '$q', primusService];
/**
* Create a new Primus service.
*/
function primusService($rootScope, $q) {
var primus = new Primus(this.endpoint, this.options);
var resourceDefers = {};
/**
* Listen on events of a given type.
* Calls the listener inside in Angular context ($evalAsync)
*
* @param {String} event
* @param {Function} listener
* @returns {Function} Deregistration function for this listener.
*/
primus.$on = function $on(event, listener) {
// run the listener in Angular context
primus.on(event, listenerInAngularContext);
function listenerInAngularContext() {
var args = arguments;
$rootScope.$evalAsync(function () {
listener.apply(null, args);
});
}
// Return the deregistration function
return function $off() {
primus.removeListener(event, listenerInAngularContext);
};
};
/**
* Listen on events of a given type, with a filtering pattern.
* If the pattern matches, calls the listener in Angular context ($evalAsync)
*
* @param {String} event
* @param {Object|Function} matchPattern
* - as a function returning true/false
* - as an object used as lodash _.matches param
* @param {Function} listener
* @returns {Function} Deregistration function for this listener.
*/
primus.$filteredOn = function $filteredOn(event, matchPattern, listener) {
var checkMatch;
if (_.isFunction(matchPattern))
checkMatch = matchPattern;
else if (_.isObject(matchPattern))
checkMatch = _.matches(matchPattern);
else
throw new Error('angular-primus $filteredOn() : matchPattern must be a function or an object !');
// run the listener in Angular context
primus.on(event, filteredListenerInAngularContext);
function filteredListenerInAngularContext() {
var args = arguments;
var isMatching = checkMatch(args[0]);
if (! isMatching) return;
$rootScope.$evalAsync(function () {
listener.apply(null, args);
});
}
// Return the deregistration function
return function $off() {
primus.removeListener(event, filteredListenerInAngularContext);
};
};
/**
* Get a resource with promise.
* Promise is resolved with resource when the resource is ready.
*
* @param {String} name
* @param {Boolean} multiplex
* @returns {Promise}
*/
primus.$resource = function $resource(name, multiplex) {
multiplex = typeof multiplex === 'undefined' ? provider.multiplex : multiplex;
// If already defined, return promise.
if (resourceDefers[name]) return resourceDefers[name].promise;
// Register a new deferred.
resourceDefers[name] = $q.defer();
// Create a new resource.
var resource = primus.resource(name, multiplex);
// Resolve promise when resource is ready.
resource.once('ready', function onReady() {
resourceDefers[name].resolve(resource);
$rootScope.$apply();
});
// Return promise.
return resourceDefers[name].promise;
};
return primus;
}
/**
* Define options.
*
* @param {Object} options
* @returns {primusProvider}
*/
this.setOptions = function setOptions(options) {
this.options = options;
return this;
};
/**
* Define endpoint.
*
* @param {String} endpoint
* @returns {primusProvider}
*/
this.setEndpoint = function setEndpoint(endpoint) {
this.endpoint = endpoint;
return this;
};
/**
* Set the default multiplex option for resource.
*
* @param {Boolean} multiplex
* @returns {primusProvider}
*/
this.setDefaultMultiplex = function setDefaultMultiplex(multiplex) {
this.multiplex = multiplex;
return this;
};
}