-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
182 lines (156 loc) · 3.93 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
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
'use strict'
//--------------------------------------------------------------------
// Task
//--------------------------------------------------------------------
const TASK_STATE_INIT = 'init'
const TASK_STATE_RUN = 'run'
const TASK_STATE_WAIT = 'wait'
const TASK_STATE_WAIT_PROMISE = 'wait_promise'
const TASK_STATE_SLEEP = 'sleep'
const TASK_STATE_DONE = 'done'
let s_idleTaskPool = []
function Task(id, func, owner) {
this.init(id, func, owner)
}
Task.prototype.init = function(id, func, owner) {
this.id = id
this.func = func
this.owner = owner
this.state = TASK_STATE_INIT
this.gen = this._initGenerator()
}
Task.prototype._initGenerator = function() {
let func = this.func
if (this.owner != undefined) {
func = this.func.bind(this.owner)
}
return func()
}
Task.getTask = function(id, func, owner) {
if (s_idleTaskPool.length == 0) {
return new Task(id, func, owner)
}
else {
let reuseTask = s_idleTaskPool.pop()
reuseTask.init(id, func, owner)
return reuseTask
}
}
Task.addToIdlePool = function(task) {
task.state = TASK_STATE_DONE
s_idleTaskPool.push(task)
}
//--------------------------------------------------------------------
// Scheduler
//--------------------------------------------------------------------
function Scheduler() {
this.tasks = {}
this.lastTaskId = 1
this.errorHandler = null
}
/**
* func is only generator function (function*)
* owner is the func's owner that will be bind as this in func.
*/
Scheduler.prototype.add = function(func, owner) {
let taskId = '' + this.lastTaskId
this.lastTaskId++
let task = Task.getTask(taskId, func, owner)
this.tasks[taskId] = task
this._runNextTick(taskId)
return taskId;
}
Scheduler.prototype._runNextTick = function(taskId, param) {
process.nextTick(() => {
this._run(taskId, param)
})
}
Scheduler.prototype._run = function(taskId, param) {
let task = this.tasks[taskId]
task.state = TASK_STATE_RUN
let result
try {
result = task.gen.next(param)
}
catch (e) {
this._handleError(task, e)
delete this.tasks[taskId]
Task.addToIdlePool(task)
return
}
if (result.done) {
delete this.tasks[taskId]
Task.addToIdlePool(task)
return
}
// Assume that result.value is an object that has properties
// state
// promise
let taskFeedback = result.value
if (taskFeedback) {
task.state = taskFeedback.state
if (taskFeedback.promise) {
taskFeedback.promise.then(() => {
this._runNextTick(taskId)
}).catch((reason) => {
this._handleError(task, reason)
delete this.tasks[taskId]
Task.addToIdlePool(task)
})
}
}
}
Scheduler.prototype._handleError = function(task, error) {
if (this.errorHandler) {
this.errorHandler(task.id, task.func, task.owner, error)
}
else {
throw error
}
}
Scheduler.prototype.notify = function(taskId, param) {
process.nextTick(() => {
const task = this.tasks[taskId]
if (task.state == TASK_STATE_WAIT) {
this._run(taskId, param)
}
else {
const error = new Error('Notify task that is not in waiting state')
this._handleError(task, error)
}
})
}
Scheduler.prototype.sleep = function(time) {
const taskFeedback = {
state: TASK_STATE_SLEEP,
promise: new Promise(function(resolve, reject) {
setTimeout(resolve, time)
})
}
return taskFeedback
}
Scheduler.prototype.wait = function() {
const taskFeedback = {
state: TASK_STATE_WAIT
}
return taskFeedback
}
Scheduler.prototype.waitPromise = function(promise) {
const taskFeedback = {
state: TASK_STATE_WAIT_PROMISE,
promise: promise
}
return taskFeedback
}
/**
* errorHandler = function(taskId, func, owner, error)
* Error handler will be replaced!
*/
Scheduler.prototype.onError = function(errorHandler) {
this.errorHandler = errorHandler
}
let scheduler = (function () {
return new Scheduler()
})()
// Return Singleton Scheduler Object
module.exports = scheduler