-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtask-queue.js
175 lines (159 loc) · 4.18 KB
/
task-queue.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
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {devAssert} from '../log';
/**
* The internal structure for the task.
* @typedef {{
* id: string,
* resource: !./resource.Resource,
* priority: number,
* forceOutsideViewport: boolean,
* callback: function(),
* scheduleTime: time,
* startTime: time,
* promise: (?Promise|undefined)
* }}
*/
export let TaskDef;
/**
* A scheduling queue for Resources.
*
* @package
*/
export class TaskQueue {
/**
* Creates an instance of TaskQueue.
*/
constructor() {
/** @private @const {!Array<!TaskDef>} */
this.tasks_ = [];
/** @private @const {!Object<string, !TaskDef>} */
this.taskIdMap_ = {};
/** @private {!time} */
this.lastEnqueueTime_ = 0;
/** @private {!time} */
this.lastDequeueTime_ = 0;
}
/**
* Size of the queue.
* @return {number}
*/
getSize() {
return this.tasks_.length;
}
/**
* Last time a task was enqueued.
* @return {!time}
*/
getLastEnqueueTime() {
return this.lastEnqueueTime_;
}
/**
* Last time a task was dequeued.
* @return {!time}
*/
getLastDequeueTime() {
return this.lastDequeueTime_;
}
/**
* Returns the task with the specified ID or null.
* @param {string} taskId
* @return {?TaskDef}
*/
getTaskById(taskId) {
return this.taskIdMap_[taskId] || null;
}
/**
* Enqueues the task. If the task is already in the queue, the error is
* thrown.
* @param {!TaskDef} task
*/
enqueue(task) {
devAssert(!this.taskIdMap_[task.id], 'Task already enqueued: %s', task.id);
this.tasks_.push(task);
this.taskIdMap_[task.id] = task;
this.lastEnqueueTime_ = Date.now();
}
/**
* Dequeues the task and returns "true" if dequeueing is successful. Otherwise
* returns "false", e.g. when this task is not currently enqueued.
* @param {!TaskDef} task
* @return {boolean}
*/
dequeue(task) {
const existing = this.taskIdMap_[task.id];
const dequeued = this.removeAtIndex(task, this.tasks_.indexOf(existing));
if (!dequeued) {
return false;
}
this.lastDequeueTime_ = Date.now();
return true;
}
/**
* Returns the task with the minimal score based on the provided scoring
* callback.
* @param {function(!TaskDef):number} scorer
* @return {?TaskDef}
*/
peek(scorer) {
let minScore = 1e6;
let minTask = null;
for (let i = 0; i < this.tasks_.length; i++) {
const task = this.tasks_[i];
const score = scorer(task);
if (score < minScore) {
minScore = score;
minTask = task;
}
}
return minTask;
}
/**
* Iterates over all tasks in queue in the insertion order.
* @param {function(!TaskDef)} callback
*/
forEach(callback) {
this.tasks_.forEach(callback);
}
/**
* Removes the task and returns "true" if dequeueing is successful. Otherwise
* returns "false", e.g. when this task is not currently enqueued.
* @param {!TaskDef} task
* @param {number} index of the task to remove.
* @return {boolean}
*/
removeAtIndex(task, index) {
const existing = this.taskIdMap_[task.id];
if (!existing || this.tasks_[index] != existing) {
return false;
}
this.tasks_.splice(index, 1);
delete this.taskIdMap_[task.id];
return true;
}
/**
* Removes tasks in queue that pass the callback test.
* @param {function(!TaskDef):boolean} callback Return true to remove the task.
*/
purge(callback) {
let index = this.tasks_.length;
while (index--) {
if (callback(this.tasks_[index])) {
this.removeAtIndex(this.tasks_[index], index);
}
}
}
}