@shgysk8zer0/polyfills 0.4.9 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/polyfills",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",
package/scheduler.js CHANGED
@@ -7,3 +7,80 @@ if (! ('scheduler' in globalThis)) {
7
7
  await Scheduler.prototype.yield.call(this, opts);
8
8
  };
9
9
  }
10
+
11
+ if (! ('TaskController' in globalThis)) {
12
+ const PRIORITIES = ['user-visible', 'user-blocking', 'background'];
13
+ const PRIORITY_SYMBOL = Symbol('task:priority');
14
+
15
+ globalThis.TaskPriorityChangeEvent = class TaskPriorityChangeEvent extends Event {
16
+ #previousPriority;
17
+
18
+ constructor(type, { previousPriority }) {
19
+ super(type);
20
+
21
+ if (typeof previousPriority !== 'string') {
22
+ throw new TypeError('TaskPriorityChangeEvent constructor: Missing required \'previousPriority\' member of TaskPriorityChangeEventInit.');
23
+ } else if (! PRIORITIES.includes(previousPriority)) {
24
+ throw new TypeError(`TaskPriorityChangeEvent constructor: '${previousPriority}' (value of 'previousPriority' member of TaskPriorityChangeEventInit) is not a valid value for enumeration TaskPriority.`);
25
+ } else {
26
+ this.#previousPriority = previousPriority;
27
+ }
28
+ }
29
+
30
+ get previousPriority() {
31
+ return this.#previousPriority;
32
+ }
33
+ };
34
+
35
+ globalThis.TaskSignal = class TaskSignal extends AbortSignal {
36
+ get priority() {
37
+ return this[PRIORITY_SYMBOL];
38
+ }
39
+
40
+ static any(signals, { priority = 'user-visible' } = {}) {
41
+ if (! PRIORITIES.includes(priority)) {
42
+ throw new TypeError(`TaskSignal.any: '${priority}' is not a valid value for enumeration TaskPriority.`);
43
+ } else {
44
+ const signal = super.any(signals);
45
+
46
+ Object.setPrototypeOf(signal, globalThis.TaskSignal.prototype);
47
+ Object.defineProperty(signal, PRIORITY_SYMBOL, {
48
+ enumerable: false,
49
+ writable: true,
50
+ configurable: false,
51
+ value: priority,
52
+ });
53
+
54
+ return signal;
55
+ }
56
+ }
57
+ };
58
+
59
+ globalThis.TaskController = class TaskController extends AbortController {
60
+ constructor({ priority = 'user-visible' } = {}) {
61
+ super();
62
+
63
+ if (! PRIORITIES.includes(priority)) {
64
+ throw new TypeError(`TaskController constructor: '${priority}' (value of 'priority' member of TaskControllerInit) is not a valid value for enumeration TaskPriority.`);
65
+ } else {
66
+ Object.setPrototypeOf(this.signal, globalThis.TaskSignal.prototype);
67
+ Object.defineProperty(this.signal, PRIORITY_SYMBOL, {
68
+ enumerable: false,
69
+ writable: true,
70
+ configurable: false,
71
+ value: priority,
72
+ });
73
+ }
74
+ }
75
+
76
+ setPriority(value) {
77
+ if (! PRIORITIES.includes(value)) {
78
+ throw new TypeError(`TaskController.setPriority: '${value}' (value of argument 1) is not a valid value for enumeration TaskPriority.`);
79
+ } else if (value !== this.signal.priority) {
80
+ const event = new globalThis.TaskPriorityChangeEvent('prioritychange', { previousPriority: this.signal.priority });
81
+ this.signal[PRIORITY_SYMBOL] = value;
82
+ this.signal.dispatchEvent(event);
83
+ }
84
+ }
85
+ };
86
+ }