worker-timers-broker 7.1.9 → 8.0.0
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/build/es2019/interfaces/index.d.ts +2 -0
- package/build/es2019/interfaces/index.d.ts.map +1 -0
- package/build/es2019/interfaces/index.js +2 -0
- package/build/es2019/interfaces/index.js.map +1 -0
- package/build/es2019/interfaces/worker-timers-broker-definition.d.ts +8 -0
- package/build/es2019/interfaces/worker-timers-broker-definition.d.ts.map +1 -0
- package/build/es2019/interfaces/worker-timers-broker-definition.js +2 -0
- package/build/es2019/interfaces/worker-timers-broker-definition.js.map +1 -0
- package/build/es2019/module.d.ts +5 -6
- package/build/es2019/module.d.ts.map +1 -1
- package/build/es2019/module.js +75 -132
- package/build/es2019/module.js.map +1 -1
- package/build/es2019/types/index.d.ts +3 -0
- package/build/es2019/types/index.d.ts.map +1 -0
- package/build/es2019/types/index.js +3 -0
- package/build/es2019/types/index.js.map +1 -0
- package/build/es2019/types/worker-timers-broker-loader.d.ts +4 -0
- package/build/es2019/types/worker-timers-broker-loader.d.ts.map +1 -0
- package/build/es2019/types/worker-timers-broker-loader.js +2 -0
- package/build/es2019/types/worker-timers-broker-loader.js.map +1 -0
- package/build/es2019/types/worker-timers-broker-wrapper.d.ts +4 -0
- package/build/es2019/types/worker-timers-broker-wrapper.d.ts.map +1 -0
- package/build/es2019/types/worker-timers-broker-wrapper.js +2 -0
- package/build/es2019/types/worker-timers-broker-wrapper.js.map +1 -0
- package/build/es5/bundle.js +92 -157
- package/package.json +14 -13
- package/src/interfaces/index.ts +1 -0
- package/src/interfaces/worker-timers-broker-definition.ts +11 -0
- package/src/module.ts +92 -161
- package/src/types/index.ts +2 -0
- package/src/types/worker-timers-broker-loader.ts +4 -0
- package/src/types/worker-timers-broker-wrapper.ts +4 -0
- package/build/es2019/guards/call-notification.d.ts +0 -3
- package/build/es2019/guards/call-notification.d.ts.map +0 -1
- package/build/es2019/guards/call-notification.js +0 -4
- package/build/es2019/guards/call-notification.js.map +0 -1
- package/build/es2019/guards/clear-response.d.ts +0 -3
- package/build/es2019/guards/clear-response.d.ts.map +0 -1
- package/build/es2019/guards/clear-response.js +0 -4
- package/build/es2019/guards/clear-response.js.map +0 -1
- package/src/guards/call-notification.ts +0 -5
- package/src/guards/clear-response.ts +0 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IBrokerDefinition } from 'broker-factory';
|
|
2
|
+
export interface IWorkerTimersBrokerDefinition extends IBrokerDefinition {
|
|
3
|
+
clearInterval(timerId: number): void;
|
|
4
|
+
clearTimeout(timerId: number): void;
|
|
5
|
+
setInterval(func: Function, delay?: number, ...args: any[]): number;
|
|
6
|
+
setTimeout(func: Function, delay?: number, ...args: any[]): number;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=worker-timers-broker-definition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-definition.d.ts","sourceRoot":"","sources":["../../../src/interfaces/worker-timers-broker-definition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD,MAAM,WAAW,6BAA8B,SAAQ,iBAAiB;IACpE,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAErC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAEpC,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;IAEpE,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;CACtE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-definition.js","sourceRoot":"","sources":["../../../src/interfaces/worker-timers-broker-definition.ts"],"names":[],"mappings":""}
|
package/build/es2019/module.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
1
|
+
import { TWorkerTimersBrokerLoader, TWorkerTimersBrokerWrapper } from './types';
|
|
2
|
+
export * from './interfaces/index';
|
|
3
|
+
export * from './types/index';
|
|
4
|
+
export declare const wrap: TWorkerTimersBrokerWrapper;
|
|
5
|
+
export declare const load: TWorkerTimersBrokerLoader;
|
|
7
6
|
//# sourceMappingURL=module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAMhF,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAM9B,eAAO,MAAM,IAAI,EAAE,0BAwFjB,CAAC;AAEH,eAAO,MAAM,IAAI,EAAE,yBAIlB,CAAC"}
|
package/build/es2019/module.js
CHANGED
|
@@ -1,146 +1,89 @@
|
|
|
1
|
+
import { createBroker } from 'broker-factory';
|
|
1
2
|
import { generateUniqueNumber } from 'fast-unique-numbers';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
|
|
20
|
-
if (timerIdAndTimerType === undefined ||
|
|
21
|
-
timerIdAndTimerType.timerId !== timerId ||
|
|
22
|
-
timerIdAndTimerType.timerType !== timerType) {
|
|
23
|
-
throw new Error('The timer is in an undefined state.');
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else if (typeof idOrFunc === 'function') {
|
|
27
|
-
idOrFunc();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
else if (timerType === 'timeout') {
|
|
31
|
-
const idOrFunc = scheduledTimeoutFunctions.get(timerId);
|
|
32
|
-
if (typeof idOrFunc === undefined) {
|
|
33
|
-
throw new Error('The timer is in an undefined state.');
|
|
34
|
-
}
|
|
35
|
-
if (typeof idOrFunc === 'number') {
|
|
36
|
-
const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
|
|
37
|
-
if (timerIdAndTimerType === undefined ||
|
|
38
|
-
timerIdAndTimerType.timerId !== timerId ||
|
|
39
|
-
timerIdAndTimerType.timerType !== timerType) {
|
|
40
|
-
throw new Error('The timer is in an undefined state.');
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else if (typeof idOrFunc === 'function') {
|
|
44
|
-
idOrFunc();
|
|
45
|
-
// A timeout can be savely deleted because it is only called once.
|
|
46
|
-
scheduledTimeoutFunctions.delete(timerId);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else if (isClearResponse(data)) {
|
|
51
|
-
const { id } = data;
|
|
52
|
-
const timerIdAndTimerType = unrespondedRequests.get(id);
|
|
53
|
-
if (timerIdAndTimerType === undefined) {
|
|
54
|
-
throw new Error('The timer is in an undefined state.');
|
|
55
|
-
}
|
|
56
|
-
const { timerId, timerType } = timerIdAndTimerType;
|
|
57
|
-
unrespondedRequests.delete(id);
|
|
58
|
-
if (timerType === 'interval') {
|
|
59
|
-
scheduledIntervalFunctions.delete(timerId);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
scheduledTimeoutFunctions.delete(timerId);
|
|
3
|
+
/*
|
|
4
|
+
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
|
|
5
|
+
* isolatedModules compiler option.
|
|
6
|
+
*/
|
|
7
|
+
export * from './interfaces/index';
|
|
8
|
+
export * from './types/index';
|
|
9
|
+
// Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
|
|
10
|
+
const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
11
|
+
const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
12
|
+
export const wrap = createBroker({
|
|
13
|
+
clearInterval: ({ call }) => {
|
|
14
|
+
return (timerId) => {
|
|
15
|
+
if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
|
|
16
|
+
scheduledIntervalsState.set(timerId, null);
|
|
17
|
+
call('clear', { timerId, timerType: 'interval' }).then(() => {
|
|
18
|
+
scheduledIntervalsState.delete(timerId);
|
|
19
|
+
});
|
|
63
20
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const id = generateUniqueNumber(unrespondedRequests);
|
|
73
|
-
unrespondedRequests.set(id, { timerId, timerType: 'interval' });
|
|
74
|
-
scheduledIntervalFunctions.set(timerId, id);
|
|
75
|
-
worker.postMessage({
|
|
76
|
-
id,
|
|
77
|
-
method: 'clear',
|
|
78
|
-
params: { timerId, timerType: 'interval' }
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
const clearTimeout = (timerId) => {
|
|
83
|
-
if (typeof scheduledTimeoutFunctions.get(timerId) === 'function') {
|
|
84
|
-
const id = generateUniqueNumber(unrespondedRequests);
|
|
85
|
-
unrespondedRequests.set(id, { timerId, timerType: 'timeout' });
|
|
86
|
-
scheduledTimeoutFunctions.set(timerId, id);
|
|
87
|
-
worker.postMessage({
|
|
88
|
-
id,
|
|
89
|
-
method: 'clear',
|
|
90
|
-
params: { timerId, timerType: 'timeout' }
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
const setInterval = (func, delay = 0, ...args) => {
|
|
95
|
-
const timerId = generateUniqueNumber(scheduledIntervalFunctions);
|
|
96
|
-
scheduledIntervalFunctions.set(timerId, () => {
|
|
97
|
-
func(...args);
|
|
98
|
-
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
99
|
-
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
100
|
-
worker.postMessage({
|
|
101
|
-
id: null,
|
|
102
|
-
method: 'set',
|
|
103
|
-
params: {
|
|
104
|
-
delay,
|
|
105
|
-
now: performance.timeOrigin + performance.now(),
|
|
106
|
-
timerId,
|
|
107
|
-
timerType: 'interval'
|
|
108
|
-
}
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
clearTimeout: ({ call }) => {
|
|
24
|
+
return (timerId) => {
|
|
25
|
+
if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
|
|
26
|
+
scheduledTimeoutsState.set(timerId, null);
|
|
27
|
+
call('clear', { timerId, timerType: 'timeout' }).then(() => {
|
|
28
|
+
scheduledTimeoutsState.delete(timerId);
|
|
109
29
|
});
|
|
110
30
|
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
setInterval: ({ call }) => {
|
|
34
|
+
return (func, delay = 0, ...args) => {
|
|
35
|
+
const symbol = Symbol();
|
|
36
|
+
const timerId = generateUniqueNumber(scheduledIntervalsState);
|
|
37
|
+
scheduledIntervalsState.set(timerId, symbol);
|
|
38
|
+
const schedule = () => call('set', {
|
|
116
39
|
delay,
|
|
117
40
|
now: performance.timeOrigin + performance.now(),
|
|
118
41
|
timerId,
|
|
119
42
|
timerType: 'interval'
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
43
|
+
}).then(() => {
|
|
44
|
+
const state = scheduledIntervalsState.get(timerId);
|
|
45
|
+
if (typeof state === undefined) {
|
|
46
|
+
throw new Error('The timer is in an undefined state.');
|
|
47
|
+
}
|
|
48
|
+
if (state === symbol) {
|
|
49
|
+
func(...args);
|
|
50
|
+
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
51
|
+
if (scheduledIntervalsState.get(timerId) === symbol) {
|
|
52
|
+
schedule();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
schedule();
|
|
57
|
+
return timerId;
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
setTimeout: ({ call }) => {
|
|
61
|
+
return (func, delay = 0, ...args) => {
|
|
62
|
+
const symbol = Symbol();
|
|
63
|
+
const timerId = generateUniqueNumber(scheduledTimeoutsState);
|
|
64
|
+
scheduledTimeoutsState.set(timerId, symbol);
|
|
65
|
+
call('set', {
|
|
131
66
|
delay,
|
|
132
67
|
now: performance.timeOrigin + performance.now(),
|
|
133
68
|
timerId,
|
|
134
69
|
timerType: 'timeout'
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
70
|
+
}).then(() => {
|
|
71
|
+
const state = scheduledTimeoutsState.get(timerId);
|
|
72
|
+
if (typeof state === 'undefined') {
|
|
73
|
+
throw new Error('The timer is in an undefined state.');
|
|
74
|
+
}
|
|
75
|
+
if (state === symbol) {
|
|
76
|
+
// A timeout can be savely deleted because it is only called once.
|
|
77
|
+
scheduledTimeoutsState.delete(timerId);
|
|
78
|
+
func(...args);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return timerId;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
export const load = (url) => {
|
|
86
|
+
const worker = new Worker(url);
|
|
87
|
+
return wrap(worker);
|
|
145
88
|
};
|
|
146
89
|
//# sourceMappingURL=module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { IDefaultBrokerDefinition } from 'broker-factory';
|
|
2
|
+
import { IWorkerTimersBrokerDefinition } from '../interfaces';
|
|
3
|
+
export type TWorkerTimersBrokerLoader = (url: string) => IWorkerTimersBrokerDefinition & IDefaultBrokerDefinition;
|
|
4
|
+
//# sourceMappingURL=worker-timers-broker-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-loader.d.ts","sourceRoot":"","sources":["../../../src/types/worker-timers-broker-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAE9D,MAAM,MAAM,yBAAyB,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,6BAA6B,GAAG,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-loader.js","sourceRoot":"","sources":["../../../src/types/worker-timers-broker-loader.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { IDefaultBrokerDefinition } from 'broker-factory';
|
|
2
|
+
import { IWorkerTimersBrokerDefinition } from '../interfaces';
|
|
3
|
+
export type TWorkerTimersBrokerWrapper = (sender: MessagePort | Worker) => IWorkerTimersBrokerDefinition & IDefaultBrokerDefinition;
|
|
4
|
+
//# sourceMappingURL=worker-timers-broker-wrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-wrapper.d.ts","sourceRoot":"","sources":["../../../src/types/worker-timers-broker-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAE9D,MAAM,MAAM,0BAA0B,GAAG,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,KAAK,6BAA6B,GAAG,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-timers-broker-wrapper.js","sourceRoot":"","sources":["../../../src/types/worker-timers-broker-wrapper.ts"],"names":[],"mappings":""}
|
package/build/es5/bundle.js
CHANGED
|
@@ -1,176 +1,111 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/typeof'), require('fast-unique-numbers')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/typeof', 'fast-unique-numbers'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.workerTimersBroker = {}, global._typeof, global.fastUniqueNumbers));
|
|
5
|
-
})(this, (function (exports, _typeof, fastUniqueNumbers) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/typeof'), require('broker-factory'), require('fast-unique-numbers')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/typeof', 'broker-factory', 'fast-unique-numbers'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.workerTimersBroker = {}, global._typeof, global.brokerFactory, global.fastUniqueNumbers));
|
|
5
|
+
})(this, (function (exports, _typeof, brokerFactory, fastUniqueNumbers) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var data = _ref.data;
|
|
23
|
-
if (isCallNotification(data)) {
|
|
24
|
-
var _data$params = data.params,
|
|
25
|
-
timerId = _data$params.timerId,
|
|
26
|
-
timerType = _data$params.timerType;
|
|
27
|
-
if (timerType === 'interval') {
|
|
28
|
-
var idOrFunc = scheduledIntervalFunctions.get(timerId);
|
|
29
|
-
if (_typeof(idOrFunc) === undefined) {
|
|
30
|
-
throw new Error('The timer is in an undefined state.');
|
|
31
|
-
}
|
|
32
|
-
if (typeof idOrFunc === 'number') {
|
|
33
|
-
var timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
|
|
34
|
-
if (timerIdAndTimerType === undefined || timerIdAndTimerType.timerId !== timerId || timerIdAndTimerType.timerType !== timerType) {
|
|
35
|
-
throw new Error('The timer is in an undefined state.');
|
|
36
|
-
}
|
|
37
|
-
} else if (typeof idOrFunc === 'function') {
|
|
38
|
-
idOrFunc();
|
|
39
|
-
}
|
|
40
|
-
} else if (timerType === 'timeout') {
|
|
41
|
-
var _idOrFunc = scheduledTimeoutFunctions.get(timerId);
|
|
42
|
-
if (_typeof(_idOrFunc) === undefined) {
|
|
43
|
-
throw new Error('The timer is in an undefined state.');
|
|
44
|
-
}
|
|
45
|
-
if (typeof _idOrFunc === 'number') {
|
|
46
|
-
var _timerIdAndTimerType = unrespondedRequests.get(_idOrFunc);
|
|
47
|
-
if (_timerIdAndTimerType === undefined || _timerIdAndTimerType.timerId !== timerId || _timerIdAndTimerType.timerType !== timerType) {
|
|
48
|
-
throw new Error('The timer is in an undefined state.');
|
|
49
|
-
}
|
|
50
|
-
} else if (typeof _idOrFunc === 'function') {
|
|
51
|
-
_idOrFunc();
|
|
52
|
-
// A timeout can be savely deleted because it is only called once.
|
|
53
|
-
scheduledTimeoutFunctions["delete"](timerId);
|
|
54
|
-
}
|
|
7
|
+
// Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
|
|
8
|
+
var scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
9
|
+
var scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
10
|
+
var wrap = brokerFactory.createBroker({
|
|
11
|
+
clearInterval: function clearInterval(_ref) {
|
|
12
|
+
var call = _ref.call;
|
|
13
|
+
return function (timerId) {
|
|
14
|
+
if (_typeof(scheduledIntervalsState.get(timerId)) === 'symbol') {
|
|
15
|
+
scheduledIntervalsState.set(timerId, null);
|
|
16
|
+
call('clear', {
|
|
17
|
+
timerId: timerId,
|
|
18
|
+
timerType: 'interval'
|
|
19
|
+
}).then(function () {
|
|
20
|
+
scheduledIntervalsState["delete"](timerId);
|
|
21
|
+
});
|
|
55
22
|
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
clearTimeout: function clearTimeout(_ref2) {
|
|
26
|
+
var call = _ref2.call;
|
|
27
|
+
return function (timerId) {
|
|
28
|
+
if (_typeof(scheduledTimeoutsState.get(timerId)) === 'symbol') {
|
|
29
|
+
scheduledTimeoutsState.set(timerId, null);
|
|
30
|
+
call('clear', {
|
|
31
|
+
timerId: timerId,
|
|
32
|
+
timerType: 'timeout'
|
|
33
|
+
}).then(function () {
|
|
34
|
+
scheduledTimeoutsState["delete"](timerId);
|
|
35
|
+
});
|
|
61
36
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
setInterval: function setInterval(_ref3) {
|
|
40
|
+
var call = _ref3.call;
|
|
41
|
+
return function (func) {
|
|
42
|
+
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
43
|
+
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
44
|
+
args[_key - 2] = arguments[_key];
|
|
69
45
|
}
|
|
70
|
-
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
var id = fastUniqueNumbers.generateUniqueNumber(unrespondedRequests);
|
|
78
|
-
unrespondedRequests.set(id, {
|
|
79
|
-
timerId: timerId,
|
|
80
|
-
timerType: 'interval'
|
|
81
|
-
});
|
|
82
|
-
scheduledIntervalFunctions.set(timerId, id);
|
|
83
|
-
worker.postMessage({
|
|
84
|
-
id: id,
|
|
85
|
-
method: 'clear',
|
|
86
|
-
params: {
|
|
46
|
+
var symbol = Symbol();
|
|
47
|
+
var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledIntervalsState);
|
|
48
|
+
scheduledIntervalsState.set(timerId, symbol);
|
|
49
|
+
var _schedule = function schedule() {
|
|
50
|
+
return call('set', {
|
|
51
|
+
delay: delay,
|
|
52
|
+
now: performance.timeOrigin + performance.now(),
|
|
87
53
|
timerId: timerId,
|
|
88
54
|
timerType: 'interval'
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
scheduledTimeoutFunctions.set(timerId, id);
|
|
101
|
-
worker.postMessage({
|
|
102
|
-
id: id,
|
|
103
|
-
method: 'clear',
|
|
104
|
-
params: {
|
|
105
|
-
timerId: timerId,
|
|
106
|
-
timerType: 'timeout'
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
var setInterval = function setInterval(func) {
|
|
112
|
-
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
113
|
-
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
114
|
-
args[_key - 2] = arguments[_key];
|
|
115
|
-
}
|
|
116
|
-
var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledIntervalFunctions);
|
|
117
|
-
scheduledIntervalFunctions.set(timerId, function () {
|
|
118
|
-
func.apply(undefined, args);
|
|
119
|
-
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
120
|
-
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
121
|
-
worker.postMessage({
|
|
122
|
-
id: null,
|
|
123
|
-
method: 'set',
|
|
124
|
-
params: {
|
|
125
|
-
delay: delay,
|
|
126
|
-
now: performance.timeOrigin + performance.now(),
|
|
127
|
-
timerId: timerId,
|
|
128
|
-
timerType: 'interval'
|
|
55
|
+
}).then(function () {
|
|
56
|
+
var state = scheduledIntervalsState.get(timerId);
|
|
57
|
+
if (_typeof(state) === undefined) {
|
|
58
|
+
throw new Error('The timer is in an undefined state.');
|
|
59
|
+
}
|
|
60
|
+
if (state === symbol) {
|
|
61
|
+
func.apply(void 0, args);
|
|
62
|
+
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
63
|
+
if (scheduledIntervalsState.get(timerId) === symbol) {
|
|
64
|
+
_schedule();
|
|
65
|
+
}
|
|
129
66
|
}
|
|
130
67
|
});
|
|
68
|
+
};
|
|
69
|
+
_schedule();
|
|
70
|
+
return timerId;
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
setTimeout: function setTimeout(_ref4) {
|
|
74
|
+
var call = _ref4.call;
|
|
75
|
+
return function (func) {
|
|
76
|
+
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
77
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
78
|
+
args[_key2 - 2] = arguments[_key2];
|
|
131
79
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
params: {
|
|
137
|
-
delay: delay,
|
|
138
|
-
now: performance.timeOrigin + performance.now(),
|
|
139
|
-
timerId: timerId,
|
|
140
|
-
timerType: 'interval'
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
return timerId;
|
|
144
|
-
};
|
|
145
|
-
var setTimeout = function setTimeout(func) {
|
|
146
|
-
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
147
|
-
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
148
|
-
args[_key2 - 2] = arguments[_key2];
|
|
149
|
-
}
|
|
150
|
-
var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledTimeoutFunctions);
|
|
151
|
-
scheduledTimeoutFunctions.set(timerId, function () {
|
|
152
|
-
return func.apply(undefined, args);
|
|
153
|
-
});
|
|
154
|
-
worker.postMessage({
|
|
155
|
-
id: null,
|
|
156
|
-
method: 'set',
|
|
157
|
-
params: {
|
|
80
|
+
var symbol = Symbol();
|
|
81
|
+
var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledTimeoutsState);
|
|
82
|
+
scheduledTimeoutsState.set(timerId, symbol);
|
|
83
|
+
call('set', {
|
|
158
84
|
delay: delay,
|
|
159
85
|
now: performance.timeOrigin + performance.now(),
|
|
160
86
|
timerId: timerId,
|
|
161
87
|
timerType: 'timeout'
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
88
|
+
}).then(function () {
|
|
89
|
+
var state = scheduledTimeoutsState.get(timerId);
|
|
90
|
+
if (typeof state === 'undefined') {
|
|
91
|
+
throw new Error('The timer is in an undefined state.');
|
|
92
|
+
}
|
|
93
|
+
if (state === symbol) {
|
|
94
|
+
// A timeout can be savely deleted because it is only called once.
|
|
95
|
+
scheduledTimeoutsState["delete"](timerId);
|
|
96
|
+
func.apply(void 0, args);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return timerId;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
var load = function load(url) {
|
|
104
|
+
var worker = new Worker(url);
|
|
105
|
+
return wrap(worker);
|
|
172
106
|
};
|
|
173
107
|
|
|
174
108
|
exports.load = load;
|
|
109
|
+
exports.wrap = wrap;
|
|
175
110
|
|
|
176
111
|
}));
|
package/package.json
CHANGED
|
@@ -9,19 +9,20 @@
|
|
|
9
9
|
}
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@babel/runtime": "^7.26.
|
|
12
|
+
"@babel/runtime": "^7.26.7",
|
|
13
|
+
"broker-factory": "^3.1.0",
|
|
13
14
|
"fast-unique-numbers": "^9.0.15",
|
|
14
15
|
"tslib": "^2.8.1",
|
|
15
|
-
"worker-timers-worker": "^
|
|
16
|
+
"worker-timers-worker": "^9.0.0"
|
|
16
17
|
},
|
|
17
18
|
"description": "The broker which is used by the worker-timers package.",
|
|
18
19
|
"devDependencies": {
|
|
19
|
-
"@babel/core": "^7.26.
|
|
20
|
+
"@babel/core": "^7.26.8",
|
|
20
21
|
"@babel/plugin-external-helpers": "^7.25.9",
|
|
21
|
-
"@babel/plugin-transform-runtime": "^7.
|
|
22
|
-
"@babel/preset-env": "^7.26.
|
|
23
|
-
"@commitlint/cli": "^19.
|
|
24
|
-
"@commitlint/config-angular": "^19.7.
|
|
22
|
+
"@babel/plugin-transform-runtime": "^7.26.8",
|
|
23
|
+
"@babel/preset-env": "^7.26.8",
|
|
24
|
+
"@commitlint/cli": "^19.7.1",
|
|
25
|
+
"@commitlint/config-angular": "^19.7.1",
|
|
25
26
|
"@rollup/plugin-babel": "^6.0.4",
|
|
26
27
|
"chai": "^4.3.10",
|
|
27
28
|
"commitizen": "^4.3.1",
|
|
@@ -40,19 +41,19 @@
|
|
|
40
41
|
"karma-sinon-chai": "^2.0.2",
|
|
41
42
|
"karma-webkit-launcher": "^2.6.0",
|
|
42
43
|
"karma-webpack": "^5.0.1",
|
|
43
|
-
"lint-staged": "^15.3
|
|
44
|
+
"lint-staged": "^15.4.3",
|
|
44
45
|
"load-grunt-config": "^4.0.1",
|
|
45
46
|
"mocha": "^10.8.2",
|
|
46
|
-
"prettier": "^3.
|
|
47
|
+
"prettier": "^3.5.0",
|
|
47
48
|
"rimraf": "^6.0.1",
|
|
48
|
-
"rollup": "^4.
|
|
49
|
+
"rollup": "^4.34.6",
|
|
49
50
|
"sinon": "^17.0.2",
|
|
50
51
|
"sinon-chai": "^3.7.0",
|
|
51
|
-
"ts-loader": "^9.5.
|
|
52
|
+
"ts-loader": "^9.5.2",
|
|
52
53
|
"tsconfig-holy-grail": "^15.0.2",
|
|
53
54
|
"tslint": "^6.1.3",
|
|
54
55
|
"tslint-config-holy-grail": "^56.0.6",
|
|
55
|
-
"typescript": "^5.7.
|
|
56
|
+
"typescript": "^5.7.3",
|
|
56
57
|
"webpack": "^5.97.1"
|
|
57
58
|
},
|
|
58
59
|
"files": [
|
|
@@ -80,5 +81,5 @@
|
|
|
80
81
|
"test": "grunt lint && grunt test"
|
|
81
82
|
},
|
|
82
83
|
"types": "build/es2019/module.d.ts",
|
|
83
|
-
"version": "
|
|
84
|
+
"version": "8.0.0"
|
|
84
85
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './worker-timers-broker-definition';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IBrokerDefinition } from 'broker-factory';
|
|
2
|
+
|
|
3
|
+
export interface IWorkerTimersBrokerDefinition extends IBrokerDefinition {
|
|
4
|
+
clearInterval(timerId: number): void;
|
|
5
|
+
|
|
6
|
+
clearTimeout(timerId: number): void;
|
|
7
|
+
|
|
8
|
+
setInterval(func: Function, delay?: number, ...args: any[]): number;
|
|
9
|
+
|
|
10
|
+
setTimeout(func: Function, delay?: number, ...args: any[]): number;
|
|
11
|
+
}
|
package/src/module.ts
CHANGED
|
@@ -1,181 +1,112 @@
|
|
|
1
|
+
import { createBroker } from 'broker-factory';
|
|
1
2
|
import { generateUniqueNumber } from 'fast-unique-numbers';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
3
|
+
import { TWorkerTimersWorkerDefinition } from 'worker-timers-worker';
|
|
4
|
+
import { IWorkerTimersBrokerDefinition } from './interfaces';
|
|
5
|
+
import { TWorkerTimersBrokerLoader, TWorkerTimersBrokerWrapper } from './types';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* @todo Explicitly referencing the barrel file seems to be necessary when enabling the
|
|
9
|
+
* isolatedModules compiler option.
|
|
10
|
+
*/
|
|
11
|
+
export * from './interfaces/index';
|
|
12
|
+
export * from './types/index';
|
|
13
|
+
|
|
14
|
+
// Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
|
|
15
|
+
const scheduledIntervalsState: Map<number, null | symbol> = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
16
|
+
const scheduledTimeoutsState: Map<number, null | symbol> = new Map([[0, null]]); // tslint:disable-line no-empty
|
|
17
|
+
|
|
18
|
+
export const wrap: TWorkerTimersBrokerWrapper = createBroker<IWorkerTimersBrokerDefinition, TWorkerTimersWorkerDefinition>({
|
|
19
|
+
clearInterval: ({ call }) => {
|
|
20
|
+
return (timerId) => {
|
|
21
|
+
if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
|
|
22
|
+
scheduledIntervalsState.set(timerId, null);
|
|
23
|
+
|
|
24
|
+
call('clear', { timerId, timerType: 'interval' }).then(() => {
|
|
25
|
+
scheduledIntervalsState.delete(timerId);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
clearTimeout: ({ call }) => {
|
|
31
|
+
return (timerId) => {
|
|
32
|
+
if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
|
|
33
|
+
scheduledTimeoutsState.set(timerId, null);
|
|
34
|
+
|
|
35
|
+
call('clear', { timerId, timerType: 'timeout' }).then(() => {
|
|
36
|
+
scheduledTimeoutsState.delete(timerId);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
setInterval: ({ call }) => {
|
|
42
|
+
return (func: Function, delay = 0, ...args: any[]) => {
|
|
43
|
+
const symbol = Symbol();
|
|
44
|
+
const timerId = generateUniqueNumber(scheduledIntervalsState);
|
|
45
|
+
|
|
46
|
+
scheduledIntervalsState.set(timerId, symbol);
|
|
47
|
+
|
|
48
|
+
const schedule = () =>
|
|
49
|
+
call('set', {
|
|
50
|
+
delay,
|
|
51
|
+
now: performance.timeOrigin + performance.now(),
|
|
52
|
+
timerId,
|
|
53
|
+
timerType: 'interval'
|
|
54
|
+
}).then(() => {
|
|
55
|
+
const state = scheduledIntervalsState.get(timerId);
|
|
56
|
+
|
|
57
|
+
if (typeof state === undefined) {
|
|
35
58
|
throw new Error('The timer is in an undefined state.');
|
|
36
59
|
}
|
|
37
|
-
} else if (typeof idOrFunc === 'function') {
|
|
38
|
-
idOrFunc();
|
|
39
|
-
}
|
|
40
|
-
} else if (timerType === 'timeout') {
|
|
41
|
-
const idOrFunc = scheduledTimeoutFunctions.get(timerId);
|
|
42
|
-
|
|
43
|
-
if (typeof idOrFunc === undefined) {
|
|
44
|
-
throw new Error('The timer is in an undefined state.');
|
|
45
|
-
}
|
|
46
60
|
|
|
47
|
-
|
|
48
|
-
|
|
61
|
+
if (state === symbol) {
|
|
62
|
+
func(...args);
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
) {
|
|
55
|
-
throw new Error('The timer is in an undefined state.');
|
|
64
|
+
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
65
|
+
if (scheduledIntervalsState.get(timerId) === symbol) {
|
|
66
|
+
schedule();
|
|
67
|
+
}
|
|
56
68
|
}
|
|
57
|
-
}
|
|
58
|
-
idOrFunc();
|
|
59
|
-
|
|
60
|
-
// A timeout can be savely deleted because it is only called once.
|
|
61
|
-
scheduledTimeoutFunctions.delete(timerId);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
} else if (isClearResponse(data)) {
|
|
65
|
-
const { id } = data;
|
|
66
|
-
|
|
67
|
-
const timerIdAndTimerType = unrespondedRequests.get(id);
|
|
68
|
-
|
|
69
|
-
if (timerIdAndTimerType === undefined) {
|
|
70
|
-
throw new Error('The timer is in an undefined state.');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const { timerId, timerType } = timerIdAndTimerType;
|
|
74
|
-
|
|
75
|
-
unrespondedRequests.delete(id);
|
|
76
|
-
|
|
77
|
-
if (timerType === 'interval') {
|
|
78
|
-
scheduledIntervalFunctions.delete(timerId);
|
|
79
|
-
} else {
|
|
80
|
-
scheduledTimeoutFunctions.delete(timerId);
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
const {
|
|
84
|
-
error: { message }
|
|
85
|
-
} = data;
|
|
86
|
-
|
|
87
|
-
throw new Error(message);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
const clearInterval = (timerId: number) => {
|
|
92
|
-
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
93
|
-
const id = generateUniqueNumber(unrespondedRequests);
|
|
94
|
-
|
|
95
|
-
unrespondedRequests.set(id, { timerId, timerType: 'interval' });
|
|
96
|
-
scheduledIntervalFunctions.set(timerId, id);
|
|
97
|
-
|
|
98
|
-
worker.postMessage(<IClearRequest>{
|
|
99
|
-
id,
|
|
100
|
-
method: 'clear',
|
|
101
|
-
params: { timerId, timerType: 'interval' }
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
};
|
|
69
|
+
});
|
|
105
70
|
|
|
106
|
-
|
|
107
|
-
if (typeof scheduledTimeoutFunctions.get(timerId) === 'function') {
|
|
108
|
-
const id = generateUniqueNumber(unrespondedRequests);
|
|
71
|
+
schedule();
|
|
109
72
|
|
|
110
|
-
|
|
111
|
-
|
|
73
|
+
return timerId;
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
setTimeout: ({ call }) => {
|
|
77
|
+
return (func: Function, delay = 0, ...args: any[]) => {
|
|
78
|
+
const symbol = Symbol();
|
|
79
|
+
const timerId = generateUniqueNumber(scheduledTimeoutsState);
|
|
112
80
|
|
|
113
|
-
|
|
114
|
-
id,
|
|
115
|
-
method: 'clear',
|
|
116
|
-
params: { timerId, timerType: 'timeout' }
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const setInterval = (func: Function, delay = 0, ...args: any[]) => {
|
|
122
|
-
const timerId = generateUniqueNumber(scheduledIntervalFunctions);
|
|
123
|
-
|
|
124
|
-
scheduledIntervalFunctions.set(timerId, () => {
|
|
125
|
-
func(...args);
|
|
126
|
-
|
|
127
|
-
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
128
|
-
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
129
|
-
worker.postMessage(<ISetNotification>{
|
|
130
|
-
id: null,
|
|
131
|
-
method: 'set',
|
|
132
|
-
params: {
|
|
133
|
-
delay,
|
|
134
|
-
now: performance.timeOrigin + performance.now(),
|
|
135
|
-
timerId,
|
|
136
|
-
timerType: 'interval'
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
});
|
|
81
|
+
scheduledTimeoutsState.set(timerId, symbol);
|
|
141
82
|
|
|
142
|
-
|
|
143
|
-
id: null,
|
|
144
|
-
method: 'set',
|
|
145
|
-
params: {
|
|
83
|
+
call('set', {
|
|
146
84
|
delay,
|
|
147
85
|
now: performance.timeOrigin + performance.now(),
|
|
148
86
|
timerId,
|
|
149
|
-
timerType: '
|
|
150
|
-
}
|
|
151
|
-
|
|
87
|
+
timerType: 'timeout'
|
|
88
|
+
}).then(() => {
|
|
89
|
+
const state = scheduledTimeoutsState.get(timerId);
|
|
152
90
|
|
|
153
|
-
|
|
154
|
-
|
|
91
|
+
if (typeof state === 'undefined') {
|
|
92
|
+
throw new Error('The timer is in an undefined state.');
|
|
93
|
+
}
|
|
155
94
|
|
|
156
|
-
|
|
157
|
-
|
|
95
|
+
if (state === symbol) {
|
|
96
|
+
// A timeout can be savely deleted because it is only called once.
|
|
97
|
+
scheduledTimeoutsState.delete(timerId);
|
|
158
98
|
|
|
159
|
-
|
|
99
|
+
func(...args);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
160
102
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
delay,
|
|
166
|
-
now: performance.timeOrigin + performance.now(),
|
|
167
|
-
timerId,
|
|
168
|
-
timerType: 'timeout'
|
|
169
|
-
}
|
|
170
|
-
});
|
|
103
|
+
return timerId;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
});
|
|
171
107
|
|
|
172
|
-
|
|
173
|
-
|
|
108
|
+
export const load: TWorkerTimersBrokerLoader = (url: string) => {
|
|
109
|
+
const worker = new Worker(url);
|
|
174
110
|
|
|
175
|
-
return
|
|
176
|
-
clearInterval,
|
|
177
|
-
clearTimeout,
|
|
178
|
-
setInterval,
|
|
179
|
-
setTimeout
|
|
180
|
-
};
|
|
111
|
+
return wrap(worker);
|
|
181
112
|
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"call-notification.d.ts","sourceRoot":"","sources":["../../../src/guards/call-notification.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEzE,eAAO,MAAM,kBAAkB,YAAa,cAAc,KAAG,OAAO,IAAI,iBAEvE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"call-notification.js","sourceRoot":"","sources":["../../../src/guards/call-notification.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,OAAuB,EAAgC,EAAE;IACxF,OAA2B,OAAQ,CAAC,MAAM,KAAK,SAAS,IAAwB,OAAQ,CAAC,MAAM,KAAK,MAAM,CAAC;AAC/G,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clear-response.d.ts","sourceRoot":"","sources":["../../../src/guards/clear-response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtE,eAAO,MAAM,eAAe,YAAa,cAAc,KAAG,OAAO,IAAI,cAEpE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clear-response.js","sourceRoot":"","sources":["../../../src/guards/clear-response.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,OAAuB,EAA6B,EAAE;IAClF,OAAO,OAAO,OAAO,CAAC,EAAE,KAAK,QAAQ,IAAI,OAAwB,OAAQ,CAAC,MAAM,KAAK,SAAS,CAAC;AACnG,CAAC,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { ICallNotification, TWorkerMessage } from 'worker-timers-worker';
|
|
2
|
-
|
|
3
|
-
export const isCallNotification = (message: TWorkerMessage): message is ICallNotification => {
|
|
4
|
-
return (<ICallNotification>message).method !== undefined && (<ICallNotification>message).method === 'call';
|
|
5
|
-
};
|