-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanipulations-status.js
48 lines (40 loc) · 1.25 KB
/
manipulations-status.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
var STATUS_QUEUED = 'queued',
STATUS_PROCESSING = 'processing';
function ActiveManipulations() {
this.manipulations = {};
}
ActiveManipulations.prototype = {
queue: function(key) {
this.manipulations[key] = {
status: STATUS_QUEUED,
callbacks: []
}
},
start: function(key) {
if (typeof(this.manipulations[key]) === 'undefined') {
throw Error("Must call queue before calling start for a given key");
}
this.manipulations[key].status = STATUS_PROCESSING;
},
finish: function(key, err) {
this.manipulations[key].callbacks.forEach(function(cb) {
cb(err);
});
delete this.manipulations[key];
},
wait: function(key, cb) {
if (typeof(this.manipulations[key]) === 'undefined') {
throw Error("Must call queue before calling wait for a given key");
}
this.manipulations[key].callbacks.push(cb);
},
isActive: function(key) {
return typeof(this.manipulations[key]) !== 'undefined';
}
};
module.exports = {
// these are just exposed for testing
ActiveManipulations: ActiveManipulations,
// this is what we really use
activeManipulations: new ActiveManipulations()
};