-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathcommon-api.js
More file actions
525 lines (471 loc) · 15.1 KB
/
common-api.js
File metadata and controls
525 lines (471 loc) · 15.1 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
'use strict';
RMModule.factory('RMCommonApi', ['$http', 'RMFastQ', '$log', 'RMUtils', function($http, $q, $log, Utils) {
var EMPTY_ARRAY = [];
function wrapPromise(_ctx, _fun) {
var dsp = _ctx.$dispatcher();
return function(_last) {
// save and reset promise
var oldPromise = _ctx.$promise;
_ctx.$promise = undefined;
try {
_ctx.$last = _last;
var result = dsp ? _ctx.$decorate(dsp, _fun, [_ctx]) : _fun.call(_ctx, _ctx);
return result === undefined ? _ctx.$promise : result;
} finally {
_ctx.$promise = oldPromise; // restore old promise
}
};
}
/**
* @class CommonApi
*
* @description
*
* Provides a common framework for restmod resources.
*
* This API is included in {@link RecordApi} and {@link CollectionApi}.
* making its methods available in every structure generated by restmod.
*
* TODO: Describe hook mechanism, promise mechanism and send lifecycle.
*
* @property {promise} $promise The last operation promise (undefined if no promise has been created yet)
* @property {array} $pending Pending requests associated to this resource (undefined if no request has been initiated)
* @property {object} $$cb Scope call backs (undefined if no callbacks have been defined, private api)
* @property {function} $$dsp The current event dispatcher (private api)
*/
var CommonApi = {
/**
* @memberof CommonApi#
*
* @description Gets this resource url.
*
* @param {string} _for Intended usage for the url (optional)
* @return {string} The resource url.
*/
$url: function(_for) {
if(_for) {
_for = '$' + _for + 'UrlFor';
if(this.$scope[_for]) return this.$scope[_for](this);
} else if(this.$scope.$canonicalUrlFor) {
return this.$scope.$canonicalUrlFor(this);
}
return this.$scope.$urlFor(this);
},
// Hooks API
/**
* @memberof CommonApi#
*
* @description Executes a given hook callbacks using the current dispatcher context.
*
* This method can be used to provide custom object lifecycle hooks.
*
* Usage:
*
* ```javascript
* var mixin = restmod.mixin({
* triggerDummy: function(_param) {
* this.$dispatch('dummy-hook', _param);
* }
* });
*
* // Then hook can be used at model definition to provide type-level customization:
* var Bike $resmod.model('/api/bikes', mixin, {
* '~dummy-hook': function() {
* alert('This is called for every bike');
* }
* };
*
* // or at instance level:
* var myBike = Bike.$build();
* myBike.$on('dummy-hook', function() {
* alert('This is called for myBike only');
* });
*
* // or event at decorated context level
* myBike.$decorate({
* 'dummy-hook': function() {
* alert('This is called for myBike only inside the decorated context');
* }
* }, fuction() {
* // decorated context
* });
* ```
*
* @param {string} _hook Hook name
* @param {array} _args Hook arguments
* @param {object} _ctx Hook execution context override
*
* @return {CommonApi} self
*/
$dispatch: function(_hook, _args, _ctx) {
var cbs, i, cb, dsp = this.$$dsp;
if(!_ctx) _ctx = this;
// context callbacks
if(dsp) {
this.$$dsp = undefined; // disable dsp for hooks
dsp(_hook, _args, _ctx);
}
// instance callbacks
if(this.$$cb && (cbs = this.$$cb[_hook])) {
for(i = 0; !!(cb = cbs[i]); i++) {
cb.apply(_ctx, _args || EMPTY_ARRAY);
}
}
// bubble up the object scope, bubble to type only if there isnt a viable parent scope.
if(this.$scope && this.$scope.$dispatch) {
this.$scope.$dispatch(_hook, _args, _ctx);
} else if(this.$type) {
this.$type.$dispatch(_hook, _args, _ctx);
}
this.$$dsp = dsp; // reenable dsp.
return this;
},
/**
* @memberof CommonApi#
*
* @description Registers an instance hook.
*
* An instance hook is called only for events generated by the calling object.
*
* ```javascript
* var bike = Model.$build(), bike2 = Model.$build();
* bike.$on('before-save', function() { alert('saved!'); });
*
* bike.$save(); // 'saved!' alert is shown after bike is saved
* bike2.$save(); // no alert is shown after bike2 is saved
* ```
*
* @param {string} _hook Hook name
* @param {function} _fun Callback
* @return {CommonApi} self
*/
$on: function(_hook, _fun) {
var hooks = (this.$$cb || (this.$$cb = {}))[_hook] || (this.$$cb[_hook] = []);
hooks.push(_fun);
return this;
},
/**
* @memberof CommonApi#
*
* @description Unregisters an instance hook registered with `$on`
*
* @param {string} _hook Hook name
* @param {function} _fun Original callback
* @return {CommonApi} self
*/
$off: function(_hook, _fun) {
if(this.$$cb && this.$$cb[_hook]) {
var idx = Utils.indexWhere(this.$$cb[_hook], function(e) { return e === _fun; });
if(idx !== -1) this.$$cb[_hook].splice(idx, 1);
}
return this;
},
/**
* @memberof CommonApi#
*
* @description Registers hooks to be used only inside the given function (decorated context).
*
* ```javascript
* // special fetch method that sends a special token header.
* restmod.mixin({
* $fetchWithToken: function(_token) {
* return this.$decorate({
* 'before-fetch': function(_req) {
* _req.headers = _req.headers || {};
* _req.headers['Token'] = _token;
* }
* ), function() {
* return this.$fetch();
* })
* }
* });
* ```
*
* @param {object|function} _hooks Hook mapping object or hook execution method.
* @param {function} _fun Function to be executed in with decorated context, this function is executed in the callee object context.
* @return {CommonApi} self
*/
$decorate: function(_hooks, _fun, _args) {
var oldDispatcher = this.$$dsp;
// set new dispatcher
this.$$dsp = (typeof _hooks === 'function' || !_hooks) ? _hooks : function(_hook, _args, _ctx) {
if(oldDispatcher) oldDispatcher.apply(null, arguments);
var extraCb = _hooks[_hook];
if(extraCb) extraCb.apply(_ctx, _args || EMPTY_ARRAY);
};
try {
return _fun.apply(this, _args);
} finally {
// reset dispatcher with old value
this.$$dsp = oldDispatcher;
}
},
/**
* @memberof CommonApi#
*
* @description Retrieves the current object's event dispatcher function.
*
* This method can be used in conjuction with `$decorate` to provide a consistent hook context
* during async operations. This is important when building extensions that want to support the
* contextual hook system in asynchronic operations.
*
* For more information aboout contextual hooks, see the {@link CommonApi#decorate} documentation.
*
* Usage:
*
* ```javascript
* restmod.mixin({
* $saveAndTrack: function() {
* var dsp = this.$dispatcher(), // capture the current dispatcher function.
* self = this;
* this.$save().$then(function() {
* this.$send({ path: '/traces', data: 'ble' }, function() {
* this.$decorate(dsp, function() {
* // the event is dispatched using the dispatcher function available when $saveAndTrack was called.
* this.$dispatch('trace-stored');
* });
* });
* });
* }
* })
* ```
*
* @return {function} Dispatcher evaluator
*/
$dispatcher: function() {
return this.$$dsp;
},
// Promise API
/**
* @memberof CommonApi#
*
* @description Returns this object last promise.
*
* If promise does not exist, then a new one is generated that resolves to the object itsef. The
* new promise is not set as the current object promise, for that use `$then`.
*
* Usage:
*
* ```javascript
* col.$fetch().$asPromise();
* ```
*
* @return {promise} $q promise
*/
$asPromise: function() {
var _this = this;
return this.$promise ? this.$promise.then(
function() { return _this; },
function() { return $q.reject(_this); }
) : $q.when(this);
},
/**
* @memberof CommonApi#
*
* @description Promise chaining method, keeps the model instance as the chain context.
*
* Calls `$q.then` on the model's last promise.
*
* Usage:
*
* ```javascript
* col.$fetch().$then(function() { });
* ```
*
* @param {function} _success success callback
* @param {function} _error error callback
* @return {CommonApi} self
*/
$then: function(_success, _error) {
if(!this.$promise) {
this.$promise = $q.when(wrapPromise(this, _success)(this));
} else {
this.$promise = this.$promise.then(
_success ? wrapPromise(this, _success) : _success,
_error ? wrapPromise(this, _error) : _error
);
}
return this;
},
/**
* @memberof CommonApi#
*
* @description Promise chaining method, similar to then but executes same callback in success or error.
*
* Usage:
*
* ```javascript
* col.$fetch().$always(function() { });
* ```
*
* @param {function} _fun success/error callback
* @return {CommonApi} self
*/
$always: function(_fun) {
return this.$then(_fun, _fun);
},
/**
* @memberof CommonApi#
*
* @description Promise chaining, keeps the model instance as the chain context.
*
* Calls ´$q.finally´ on the collection's last promise, updates last promise with finally result.
*
* Usage:
*
* ```javascript
* col.$fetch().$finally(function() { });
* ```
*
* @param {function} _cb callback
* @return {CommonApi} self
*/
$finally: function(_cb) {
this.$promise = this.$promise['finally'](wrapPromise(this, _cb));
return this;
},
// Communication API
/**
* @memberof CommonApi#
*
* @description Low level communication method, wraps the $http api.
*
* * You can access last request promise using the `$asPromise` method.
* * Pending requests will be available at the $pending property (array).
* * Current request execution status can be queried using the $status property (current request, not last).
* * The $status property refers to the current request inside $send `_success` and `_error` callbacks.
*
* @param {object} _options $http options
* @param {function} _success sucess callback (sync)
* @param {function} _error error callback (sync)
* @return {CommonApi} self
*/
$send: function(_options, _success, _error) {
// make sure a style base was selected for the model
if(!this.$type.getProperty('style')) {
$log.warn('No API style base was selected, see the Api Integration FAQ for more information on this warning');
}
var action = this.$$action;
return this.$always(function() {
var defaultOptions = {};
if(action && action.aborter) {
defaultOptions.timeout = action.aborter.promise;
}
_options = angular.extend(defaultOptions, _options);
this.$response = null;
this.$status = 'pending';
this.$dispatch('before-request', [_options]);
return $http(_options).then(wrapPromise(this, function() {
if(action && action.canceled) {
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
return $q.reject(this);
} else {
this.$status = 'ok';
this.$response = this.$last;
this.$dispatch('after-request', [this.$response]);
if(_success) _success.call(this, this.$response);
}
}), wrapPromise(this, function() {
if(action && action.canceled) {
this.$status = 'canceled';
this.$dispatch('after-request-cancel', []);
} else {
this.$status = 'error';
this.$response = this.$last;
// IDEA: Consider flushing pending request in case of an error. Also continue ignoring requests
// until the error flag is reset by user.
this.$dispatch('after-request-error', [this.$response]);
if(_error) _error.call(this, this.$response);
}
return $q.reject(this); // TODO: this will step over any promise generated in _error!!
}));
});
},
// Actions API
/**
* @memberof CommonApi#
*
* @description Registers a new action to be executed in the promise queue.
*
* Registered pending actions can be canceled using `$cancel`
*
* `$cancel` will also cancel any ongoing call to `$send` (will not abort it yet though...)
*
* @return {CommonApi} self
*/
$action: function(_fun) {
var status = {
canceled: false,
aborter: $q.defer()
}, pending = this.$pending || (this.$pending = []);
pending.push(status);
return this.$always(function() {
var oldAction = this.$$action;
try {
if(!status.canceled) {
this.$$action = status;
return _fun.call(this);
} else {
return $q.reject(this);
}
} finally {
// restore object state and pending actions
this.$$action = oldAction;
}
}).$finally(function() {
// after action and related async code finishes, remove status from pending list
pending.splice(pending.indexOf(status), 1);
});
},
/**
* @memberof CommonApi#
*
* @description Cancels all pending actions registered with $action.
*
* @return {CommonApi} self
*/
$cancel: function() {
// cancel every pending request.
if(this.$pending) {
angular.forEach(this.$pending, function(_status) {
_status.canceled = true;
});
}
return this;
},
/**
* @memberof CommonApi#
*
* @description Aborts all pending actions registered with $action.
*
* @return {CommonApi} self
*/
$abort: function() {
// abort every pending request.
if(this.$pending) {
angular.forEach(this.$pending, function(_status) {
_status.aborter.resolve();
});
}
return this;
},
/**
* @memberof CommonApi#
*
* @description Returns true if object has queued actions
*
* @return {Boolean} Object request pending status.
*/
$hasPendingActions: function() {
var pendingCount = 0;
if(this.$pending) {
angular.forEach(this.$pending, function(_status) {
if(!_status.canceled) pendingCount++;
});
}
return pendingCount > 0;
}
};
return CommonApi;
}]);