-
Notifications
You must be signed in to change notification settings - Fork 2
/
gate.js
142 lines (126 loc) · 4.11 KB
/
gate.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
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
/**
* This module allows you to track aggregate activity and
* execute one or more callbacks when your tasks clear.
* It is not necessary to identify individual tasks as
* the only thing this class cares about is the quantity of tasks in the queue.
*
* However for future proofing, tasks are identified and optionally removed by name.
*/
var _ = require('underscore');
var util = require('util');
module.exports = function (callback, name) {
if ((name == 'undefined') || (!name)){
throw new Error('gate name == undefined');
}
this._tasks = 0;
this.name = name || '';
this._start = false;
this.debug = false;
this.multi = false;
this._callback = callback;
this._tasks_to_do = [];
this._tasks_done = [];
this._task_inc = 0;
}
_.extend(module.exports.prototype, {
start:function () {
var self = this;
if (!(this.name)){
throw new Error('NO NAME for gate');
}
process.nextTick(function(){
self._on_start();
});
},
_on_start: function(){
this._start = true;
if (this.debug) {
console.log('GATE %s:: STARTING!!!!! %s with tasks (cb = %s)', this.name, this._tasks, this._callback.toString());
}
this._check_status();
},
task_start:function (task) {
if (!task) {
task = ++ this._task_inc;
}
this._tasks_to_do.push(task);
++ this._tasks;
if (this.debug) {
console.log('gate %s task starting: %s - status: %s', this.name, task, this._status_str());
}
},
task_done:function (task) {
-- this._tasks;
if (!task){
task = '(unknown)';
}
this._tasks_done.push(task);
if (this.debug) {
console.log('gate %s task done; %s - status: %s', this.name, task, this._status_str());
}
this._check_status();
},
_start: false,
_done: false,
task_done_callback:function (start_task, on_done, task) {
var my_gate = this;
if (_.isString(start_task) && (!task)){
task = start_task;
}
if (start_task) {
// acknowledge that a task has been begun.
if (this.debug){
console.log('starting task and returning callback %s', task);
}
this.task_start(task);
} else {
if (this.debug){
console.log('NOT starting task - just returning callback %s', task);
}
}
return function () {
if (on_done){
on_done();
}
my_gate.task_done( task);
}
},
_status_str: function(){
return util.format(': %s started: %s, done: %s, to do: (%s), done: (%s)',
this.name,
this._start ? 'true' : 'false',
this._done ? 'true' : 'false',
this._tasks_to_do.join(','), this._tasks_done.join(','));
},
_check_status:function () {
var self = this;
if (this.debug){
console.log( '_check_status: >>>> %s', this._status_str());
}
if (this._start) {
if (this._tasks < 1) {
if (this._done){
if (this.multi){
this._callback(self);
} else {
throw new Error('gate for ' + this._callback.toString() + ' attempting to finish twice. ' + this._status_str());
}
} else {
this._done = true;
if (this.debug) {
console.log('GATE %s:: _check_status: !!!!! REACHED END !!!!!!', this.name);
}
this._callback();
}
} else {
if (this.debug) {
console.log('GATE %s:: check status task done; %s tasks left', this.name, this._tasks);
}
}
} else {
if (this.debug) {
console.log('GATE %s:: check status, %s left: STILL NOT STARTED; ', this.name, this._tasks);
}
}
}
})