-
Notifications
You must be signed in to change notification settings - Fork 1
/
fx-task-queue.html
158 lines (142 loc) · 4.27 KB
/
fx-task-queue.html
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
<!--
Schedules work to execute, will run the passed function, and replace it
with the newly returned function, repeatedly until the work runs out or
the workLimit is reached in ms. If the limit is reached and there are still
functions to run then it will schedule a completion with setTimeout.
Usage:
// asyncForEach is similar to:
var i = 0;
var array = [ ... ];
function nextElement() {
if (i >= array.length)
return;
process(array[i++]);
return nextElement;
}
this.$.taskQueue.scheduleWork(nextElement);
-->
<link href="../polymer/polymer.html" rel="import">
<polymer-element name="fx-task-queue" constructor="FxTaskQueue">
<script>
(function() {
var tasks = [];
function findTasks(element) {
return tasks.filter(function(task) {
return task.element = element;
});
}
function suspendTasks(priority) {
if (priority == undefined)
priority = -1;
tasks.forEach(function(task) {
if (priority < 0 || task.element.priority <= priority)
task.suspend();
});
}
function resumeTasks(priority) {
if (priority == undefined)
priority = -1;
tasks.forEach(function(task) {
if (priority < 0 || task.element.priority <= priority)
task.resume();
});
}
function ScheduledTask(element, fn) {
this.callback = fn;
this.suspendCount = 0;
this.element = element;
this.scheduled = false;
this.canceled = false;
this.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
}.bind(this));
}
ScheduledTask.prototype.cancel = function() {
this.canceled = true;
};
ScheduledTask.prototype.suspend = function() {
this.suspendCount++;
};
ScheduledTask.prototype.resume = function() {
this.suspendCount--;
if (!this.suspendCount)
this.schedule();
};
ScheduledTask.prototype.schedule = function() {
if (this.scheduled || this.canceled)
return;
this.scheduled = true;
setTimeout(function() {
this.execute();
}.bind(this), this.element.delayTime);
};
ScheduledTask.prototype.execute = function() {
try {
this.executeInternal();
} catch (e) {
this.reject(e);
}
};
ScheduledTask.prototype.executeInternal = function() {
if (this.suspendCount || this.canceled)
return;
var endTime = Date.now() + this.element.sliceTime;
var result = this.callback();
while (result instanceof Function && Date.now() < endTime)
result = result();
if (result instanceof Function) {
this.callback = result;
this.schedule();
} else {
this.resolve();
}
};
Polymer({
created: function() {
this.sliceTime = 100; // ms
this.delayTime = 0; // ms
this.priority = 100;
},
detach: function() {
this.cancel();
},
scheduleWork: function(fn) {
var task = new ScheduledTask(this, fn);
tasks.push(task);
return task.schedule();
},
asyncForEach: function(array, fn) {
var i = 0;
function next() {
if (i >= array.length)
return;
fn(array[i], i);
i++;
return next;
}
return this.scheduleWork(next);
},
cancel: function() {
findTasks(this).forEach(function(task) {
task.cancel();
});
},
suspend: function() {
findTasks(this).forEach(function(task) {
task.suspend();
});
},
resume: function() {
findTasks(this).forEach(function(task) {
task.resume();
});
},
});
document.addEventListener("polymer-ready", function() {
// Static API
FxTaskQueue.suspendTasks = suspendTasks;
FxTaskQueue.resumeTasks = resumeTasks;
});
</script>
</polymer-element>