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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// META: global=window,worker
test((t) => {
const signal = TaskSignal.any([]);
assert_true(signal instanceof TaskSignal);
assert_equals(signal.priority, 'user-visible');
}, "TaskSignal.any() returns a user-visible TaskSignal when no priority is specified");
test((t) => {
let signal = TaskSignal.any([], {priority: 'user-blocking'});
assert_equals(signal.priority, 'user-blocking');
signal = TaskSignal.any([], {priority: 'user-visible'});
assert_equals(signal.priority, 'user-visible');
signal = TaskSignal.any([], {priority: 'background'});
assert_equals(signal.priority, 'background');
}, "TaskSignal.any() returns a signal with the correct priority when intialized with a string");
test((t) => {
let controller = new TaskController({priority: 'user-blocking'});
let signal = TaskSignal.any([], {priority: controller.signal});
assert_equals(signal.priority, 'user-blocking');
controller = new TaskController({priority: 'user-visible'});
signal = TaskSignal.any([], {priority: controller.signal});
assert_equals(signal.priority, 'user-visible');
controller = new TaskController({priority: 'background'});
signal = TaskSignal.any([], {priority: controller.signal});
assert_equals(signal.priority, 'background');
}, "TaskSignal.any() returns a signal with the correct priority when intialized with a TaskSignal");
test((t) => {
let controller = new TaskController({priority: 'user-blocking'});
let signal = TaskSignal.any([], {priority: controller.signal});
assert_equals(signal.priority, 'user-blocking');
controller.setPriority('user-visible');
assert_equals(signal.priority, 'user-visible');
controller.setPriority('background');
assert_equals(signal.priority, 'background');
controller.setPriority('user-blocking');
assert_equals(signal.priority, 'user-blocking');
}, "TaskSignal.any() returns a signal with dynamic priority");
test((t) => {
const controller = new TaskController();
const signal = TaskSignal.any([], {priority: controller.signal});
let eventFiredCount = 0;
signal.onprioritychange = t.step_func((e) => {
assert_equals(e.target, signal,
`The event target is the signal returned by TaskSignal.any()`);
++eventFiredCount;
});
controller.setPriority('background');
assert_equals(eventFiredCount, 1);
controller.setPriority('user-visible');
assert_equals(eventFiredCount, 2);
controller.setPriority('user-blocking');
assert_equals(eventFiredCount, 3);
}, "Priority change events fire for composite signals");
test((t) => {
const controller = new TaskController();
let signal = TaskSignal.any([], {priority: controller.signal});
signal = TaskSignal.any([], {priority: signal});
signal = TaskSignal.any([], {priority: signal});
signal = TaskSignal.any([], {priority: signal});
signal = TaskSignal.any([], {priority: signal});
assert_equals(signal.priority, 'user-visible');
let eventFiredCount = 0;
signal.onprioritychange = t.step_func((e) => {
assert_equals(e.target, signal,
"The event target is the signal returned by TaskSignal.any()");
++eventFiredCount;
});
controller.setPriority('background');
assert_equals(eventFiredCount, 1);
assert_equals(signal.priority, 'background');
controller.setPriority('user-visible');
assert_equals(eventFiredCount, 2);
assert_equals(signal.priority, 'user-visible');
controller.setPriority('user-blocking');
assert_equals(eventFiredCount, 3);
assert_equals(signal.priority, 'user-blocking');
}, "Priority change events fire for composite signals with intermediate sources");
test((t) => {
const controller = new TaskController();
const signals = [];
const results = [];
let id = 0;
for (let i = 0; i < 3; i++) {
const signal = TaskSignal.any([], {priority: controller.signal});
const eventId = id++;
signal.addEventListener('prioritychange', () => {
results.push(eventId);
});
signals.push(signal);
}
for (let i = 0; i < 3; i++) {
const signal = TaskSignal.any([], {priority: signals[i]});
const eventId = id++;
signal.addEventListener('prioritychange', () => {
results.push(eventId);
});
}
controller.setPriority('background');
assert_equals(results.toString(), '0,1,2,3,4,5')
controller.setPriority('user-blocking');
assert_equals(results.toString(), '0,1,2,3,4,5,0,1,2,3,4,5')
}, "Priority change propagates to multiple dependent signals in the right order");
test((t) => {
const controller = new TaskController();
const signal = TaskSignal.any([], {priority: controller.signal});
let fired = false;
signal.onabort = t.step_func(() => {
assert_unreached("The signal should not abort");
fired = true;
});
controller.abort();
assert_false(fired);
}, "TaskSignal.any() does not propagate abort when not given dependent abort signals");
test((t) => {
const taskController = new TaskController();
const abortController = new AbortController();
const signal = TaskSignal.any([abortController.signal], {priority: taskController.signal});
let priorityFireCount = 0;
signal.onprioritychange = t.step_func(() => {
++priorityFireCount;
});
let abortFired = false;
signal.onabort = t.step_func(() => {
abortFired = true;
});
taskController.setPriority('background');
assert_equals(signal.priority, 'background');
assert_equals(priorityFireCount, 1);
taskController.abort();
assert_false(abortFired, "The signal should use abortController for abort");
abortController.abort();
assert_true(abortFired);
taskController.setPriority('user-visible');
assert_equals(signal.priority, 'user-visible');
assert_equals(priorityFireCount, 2);
}, "TaskSignal.any() propagates abort and priority");
test((t) => {
const controller = new TaskController();
const signal = TaskSignal.any([AbortSignal.abort()], {priority: controller.signal});
let fired = false;
signal.onprioritychange = t.step_func(() => {
fired = true;
});
controller.setPriority('background');
assert_true(fired);
}, "TaskSignal.any() propagates priority after returning an aborted signal");
test((t) => {
// Add a dependent in the initial event dispatch stage.
let controller = new TaskController();
let fired = false;
controller.signal.onprioritychange = t.step_func(() => {
fired = true;
const newSignal = TaskSignal.any([], {priority: controller.signal});
assert_equals(newSignal.priority, 'background');
newSignal.onprioritychange = t.unreached_func('onprioritychange called');
});
controller.setPriority('background');
assert_true(fired);
// Add a dependent while signaling prioritychange on dependents.
fired = false;
controller = new TaskController();
const signal = TaskSignal.any([], {priority: controller.signal});
signal.onprioritychange = t.step_func(() => {
fired = true;
const newSignal = TaskSignal.any([], {priority: signal});
assert_equals(newSignal.priority, 'background');
newSignal.onprioritychange = t.unreached_func('onprioritychange called');
});
controller.setPriority('background');
assert_true(fired);
}, "TaskSignal.any() does not fire prioritychange for dependents added during prioritychange");
|