-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.47 KB
/
index.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
class QueueProcessor {
constructor(queue, name, args) {
this.queue = queue;
this.name = name;
this.requests = [];
this.counts = {
success: 0,
error: 0
};
this.args = args;
}
process(func, onSuccess, onError, onComplete) {
const queue = this.queue;
const self = this;
if(queue.length > 0) {
// splice operates on the array instance, mutating the passed `queue` array. the removed items
// are returned
const item = queue.splice(queue.length -1)[0];
if(!item) {
return;
}
const request = func(item);
this.requests.push(request);
request.then((result) => {
result = JSON.parse(result);
// this line couples the processor to the textrazor schema. error should be caught at a
// different layer so the queue processor can remain generic
if(result.hasOwnProperty('error') || result.ok === false) {
onError(result, item);
} else {
self.counts.success += 1;
onSuccess(result, item, this.args);
}
this.process(func, onSuccess, onError, onComplete);
}).catch((error) => {
onError(error);
this.counts.error += 1;
this.process(func, onSuccess, onError, onComplete);
});
} else {
Promise.all(this.requests)
.then(() => {
onComplete(this);
})
.catch((error) => {
onError(error);
})
}
}
}
module.exports = QueueProcessor;