This repository was archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScheduler.js
More file actions
180 lines (143 loc) · 3.8 KB
/
Scheduler.js
File metadata and controls
180 lines (143 loc) · 3.8 KB
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
/** @license MIT License (c) copyright 2016 original author or authors */
import ScheduledTask from './ScheduledTask'
import binarySearch from './binarySearch'
import { findIndex, removeAll } from '@most/prelude'
export default class Scheduler {
constructor (timer) {
this.timer = timer
this._timer = null
this._nextArrival = 0
this._tasks = []
var self = this
this._runReadyTasksBound = function () {
self._runReadyTasks(self.now())
}
}
now () {
return this.timer.now()
}
asap (task) {
return this.schedule(0, -1, task)
}
delay (delay, task) {
return this.schedule(delay, -1, task)
}
periodic (period, task) {
return this.schedule(0, period, task)
}
schedule (delay, period, task) {
var now = this.now()
var st = new ScheduledTask(now + Math.max(0, delay), period, task, this)
insertByTime(st, this._tasks)
this._scheduleNextRun(now)
return st
}
cancel (task) {
task.active = false
var i = binarySearch(task.time, this._tasks)
if (i >= 0 && i < this._tasks.length) {
var at = findIndex(task, this._tasks[i].events)
if (at >= 0) {
this._tasks[i].events.splice(at, 1)
this._reschedule()
}
}
}
cancelAll (f) {
for (var i = 0; i < this._tasks.length; ++i) {
removeAllFrom(f, this._tasks[i])
}
this._reschedule()
}
_reschedule () {
if (this._tasks.length === 0) {
this._unschedule()
} else {
this._scheduleNextRun(this.now())
}
}
_unschedule () {
this.timer.clearTimer(this._timer)
this._timer = null
}
_scheduleNextRun (now) { // eslint-disable-line complexity
if (this._tasks.length === 0) {
return
}
var nextArrival = this._tasks[0].time
if (this._timer === null) {
this._scheduleNextArrival(nextArrival, now)
} else if (nextArrival < this._nextArrival) {
this._unschedule()
this._scheduleNextArrival(nextArrival, now)
}
}
_scheduleNextArrival (nextArrival, now) {
this._nextArrival = nextArrival
var delay = Math.max(0, nextArrival - now)
this._timer = this.timer.setTimer(this._runReadyTasksBound, delay)
}
_runReadyTasks (now) {
this._timer = null
this._tasks = this._findAndRunTasks(now)
this._scheduleNextRun(this.now())
}
_findAndRunTasks (now) {
var tasks = this._tasks
var l = tasks.length
var i = 0
while (i < l && tasks[i].time <= now) {
++i
}
this._tasks = tasks.slice(i)
// Run all ready tasks
for (var j = 0; j < i; ++j) {
this._tasks = runTasks(tasks[j], this._tasks)
}
return this._tasks
}
}
function removeAllFrom (f, timeslot) {
timeslot.events = removeAll(f, timeslot.events)
}
function runTasks (timeslot, tasks) { // eslint-disable-line complexity
var events = timeslot.events
for (var i = 0; i < events.length; ++i) {
var task = events[i]
if (task.active) {
runTask(task)
// Reschedule periodic repeating tasks
// Check active again, since a task may have canceled itself
if (task.period >= 0) {
task.time = task.time + task.period
insertByTime(task, tasks)
}
}
}
return tasks
}
function runTask (task) {
try {
return task.run()
} catch (e) {
return task.error(e)
}
}
function insertByTime (task, timeslots) { // eslint-disable-line complexity
var l = timeslots.length
if (l === 0) {
timeslots.push(newTimeslot(task.time, [task]))
return
}
var i = binarySearch(task.time, timeslots)
if (i >= l) {
timeslots.push(newTimeslot(task.time, [task]))
} else if (task.time === timeslots[i].time) {
timeslots[i].events.push(task)
} else {
timeslots.splice(i, 0, newTimeslot(task.time, [task]))
}
}
function newTimeslot (t, events) {
return { time: t, events: events }
}