-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack.js
76 lines (63 loc) · 1.81 KB
/
track.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
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
(['./io', './scaffold'], function (io, scaffold) {
'use strict';
// keep track of I/O requests
function track (options, prep, level) {
if (!io.track.optIn(options)) {
return null;
}
var key = prep.key, deferred = io.track.deferred[key];
// check if in flight
if (deferred) {
return deferred.promise || deferred;
}
// check if required to wait
if (options.wait) {
return flyByKey(key);
}
// register a request
var promise = flyByKey(key);
deferred = io.track.deferred[key];
var newPromise = io.request(options, prep, level - 1);
if (promise !== newPromise) {
newPromise.then(
function (value) { deferred.resolve(value, true); },
function (value) { deferred.reject (value, true); }
);
}
return promise;
}
function flyByKey(key) {
var deferred = io.track.deferred[key], needsCleanUp = false;
if (!deferred) {
deferred = io.track.deferred[key] = new io.Deferred();
needsCleanUp = true;
}
var promise = deferred.promise || deferred;
if (needsCleanUp) {
promise.then(cleanUp, cleanUp);
}
return promise;
function cleanUp () {
delete io.track.deferred[key];
}
}
function fly (options) {
options = io.processOptions(typeof options == 'string' ?
{url: options, method: 'GET'} : options);
return io.track.flyByKey(io.makeKey(options));
}
function isFlying (options) {
options = io.processOptions(typeof options == 'string' ?
{url: options, method: 'GET'} : options);
return io.track.deferred[io.makeKey(options)];
}
// export
io.track = {
flyByKey: flyByKey,
fly: fly,
isFlying: isFlying,
deferred: {}
};
return scaffold(io, 'track', 40, track);
});