-
Notifications
You must be signed in to change notification settings - Fork 1
/
macrotask.js
584 lines (474 loc) · 12.4 KB
/
macrotask.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.macrotask = global.macrotask || {})));
}(this, (function (exports) { 'use strict';
var global$1 = typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};
var browser = true;
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var get = function get(object, property, receiver) {
if (object === null) object = Function.prototype;
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var set = function set(object, property, value, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent !== null) {
set(parent, property, value, receiver);
}
} else if ("value" in desc && desc.writable) {
desc.value = value;
} else {
var setter = desc.set;
if (setter !== undefined) {
setter.call(receiver, value);
}
}
return value;
};
var perf = void 0;
var Deadline = function () {
function Deadline() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
classCallCheck(this, Deadline);
this.limit = limit;
this.performance = Deadline.getPerformance();
this.now = this.getTime();
}
Deadline.prototype.getTime = function getTime() {
return this.performance ? global$1.performance.now() : Date.now();
};
Deadline.prototype.timeRemaining = function timeRemaining() {
var out = this.limit - (this.getTime() - this.now);
if (out > 0) {
return out;
}
return 0;
};
Deadline.getPerformance = function getPerformance() {
if (perf === undefined) {
perf = !!(global$1.performance && global$1.performance.now && typeof global$1.performance.now === 'function');
}
return perf;
};
return Deadline;
}();
var test = function test() {
return !browser && global$1 && typeof global$1.setImmediate === 'function';
};
var install = function install(func) {
return function () {
return global$1.setImmediate(function () {
return func(new Deadline());
});
};
};
var setImmediate = Object.freeze({
test: test,
install: install
});
// The test against `importScripts` prevents this implementation from being installed inside a web worker,
// where `global.postMessage` means something completely different and can't be used for this purpose.
function test$1() {
if (!global$1.postMessage || global$1.importScripts) {
return false;
}
if (global$1.setImmediate) {
// we can only get here in IE10
// which doesn't handel postMessage well
return false;
}
var postMessageIsAsynchronous = true;
var oldOnMessage = global$1.onmessage;
global$1.onmessage = function () {
postMessageIsAsynchronous = false;
};
global$1.postMessage('', '*');
global$1.onmessage = oldOnMessage;
return postMessageIsAsynchronous;
}
function install$1(func) {
var codeWord = 'macrotask' + Math.random();
function globalMessage(event) {
if (event.source === global$1 && event.data === codeWord) {
func(new Deadline());
}
}
if (global$1.addEventListener) {
global$1.addEventListener('message', globalMessage, false);
} else {
global$1.attachEvent('onmessage', globalMessage);
}
return function () {
global$1.postMessage(codeWord, '*');
};
}
var postMessage = Object.freeze({
test: test$1,
install: install$1
});
function test$2() {
if (global$1.setImmediate) {
// we can only get here in IE10
// which doesn't handel postMessage well
return false;
}
return typeof global$1.MessageChannel !== 'undefined';
}
function install$2(func) {
var channel = new global$1.MessageChannel();
channel.port1.onmessage = function () {
return func(new Deadline());
};
return function () {
channel.port2.postMessage(0);
};
}
var messageChannel = Object.freeze({
test: test$2,
install: install$2
});
var test$3 = function test$3() {
return 'document' in global$1 && 'onreadystatechange' in global$1.document.createElement('script');
};
var install$3 = function install$3(handle) {
return function () {
// Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted
// into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.
var scriptEl = global$1.document.createElement('script');
scriptEl.onreadystatechange = function () {
handle(new Deadline());
scriptEl.onreadystatechange = null;
scriptEl.parentNode.removeChild(scriptEl);
scriptEl = null;
};
global$1.document.documentElement.appendChild(scriptEl);
return handle;
};
};
var stateChange = Object.freeze({
test: test$3,
install: install$3
});
var test$4 = function test$4() {
return true;
};
function install$4(t) {
return function () {
setTimeout(function () {
t(new Deadline());
}, 0);
};
}
var timeout = Object.freeze({
test: test$4,
install: install$4
});
var test$5 = function test$5() {
return typeof global$1.requestIdleCallback === 'function';
};
var install$5 = function install$5(func) {
return function () {
return global$1.requestIdleCallback(func);
};
};
var idleCallback = Object.freeze({
test: test$5,
install: install$5
});
var types = [setImmediate, idleCallback, postMessage, messageChannel, stateChange, timeout];
var creatNextTick = function (drainQueue) {
for (var _iterator = types, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var type = _ref;
if (type.test()) {
return type.install(drainQueue);
}
}
};
var CancelToken = function CancelToken() {
classCallCheck(this, CancelToken);
};
// v8 likes predictible objects
var Item = function () {
function Item(fun, array, list) {
classCallCheck(this, Item);
this.fun = fun;
this.array = array;
this.token = new CancelToken();
this.prev = null;
this.next = null;
this.list = list;
}
Item.prototype.run = function run() {
var fun = this.fun;
var array = this.array;
switch (array.length) {
case 0:
fun();
break;
case 1:
fun(array[0]);
break;
case 2:
fun(array[0], array[1]);
break;
case 3:
fun(array[0], array[1], array[2]);
break;
default:
fun.apply(undefined, array);
break;
}
};
Item.prototype.cancel = function cancel() {
var next = this.next;
var prev = this.prev;
if (next === null) {
if (prev === null) {
// only thing on list
if (this.list.head === this && this.list.length === 1) {
// sanity check
this.list.head = this.list.tail = null;
} else {
return;
}
} else {
prev.next = null;
this.list.tail = prev;
// tail of list
}
} else {
if (prev === null) {
// head of list
next.prev = null;
this.list.head = next;
} else {
// middle of list
prev.next = next;
next.prev = prev;
}
}
this.list.length--;
};
return Item;
}();
var List = function () {
function List() {
classCallCheck(this, List);
this.length = 0;
this.head = null;
this.tail = null;
this.cache = new WeakMap();
}
List.prototype.push = function push(func, args) {
var item = new Item(func, args, this);
if (this.length > 0) {
var currentTail = this.tail;
currentTail.next = item;
item.prev = currentTail;
this.tail = item;
} else {
this.head = this.tail = item;
}
this.length++;
this.cache.set(item.token, item);
return item.token;
};
List.prototype.shift = function shift() {
if (this.length < 1) {
return;
}
var item = this.head;
if (this.length === 1) {
this.head = this.tail = null;
} else {
var newHead = item.next;
newHead.prev = null;
this.head = newHead;
}
this.length--;
this.cache.delete(item.token);
return item;
};
List.prototype.cancel = function cancel(token) {
var item = this.cache.get(token);
if (item) {
this.cache.delete(token);
} else {
return false;
}
item.cancel();
return true;
};
return List;
}();
var list = new List();
var inProgress = false;
var nextTick = void 0;
function drainQueue(idleDeadline) {
if (!list.length) {
inProgress = false;
return;
}
var task = list.shift();
if (!list.length) {
inProgress = false;
} else {
nextTick();
}
task.run();
if (!idleDeadline) {
return;
}
while (idleDeadline.timeRemaining() > 0 && list.length) {
var _task = list.shift();
_task.run();
}
}
function run$1(task) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var token = list.push(task, args);
if (inProgress) {
return token;
}
if (!nextTick) {
nextTick = creatNextTick(drainQueue);
}
inProgress = true;
nextTick();
return token;
}
function clear(token) {
return list.cancel(token);
}
exports.run = run$1;
exports.clear = clear;
Object.defineProperty(exports, '__esModule', { value: true });
})));