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.
Files changed (42) hide show
  1. package/build/es2019/interfaces/index.d.ts +2 -0
  2. package/build/es2019/interfaces/index.d.ts.map +1 -0
  3. package/build/es2019/interfaces/index.js +2 -0
  4. package/build/es2019/interfaces/index.js.map +1 -0
  5. package/build/es2019/interfaces/worker-timers-broker-definition.d.ts +8 -0
  6. package/build/es2019/interfaces/worker-timers-broker-definition.d.ts.map +1 -0
  7. package/build/es2019/interfaces/worker-timers-broker-definition.js +2 -0
  8. package/build/es2019/interfaces/worker-timers-broker-definition.js.map +1 -0
  9. package/build/es2019/module.d.ts +5 -6
  10. package/build/es2019/module.d.ts.map +1 -1
  11. package/build/es2019/module.js +75 -132
  12. package/build/es2019/module.js.map +1 -1
  13. package/build/es2019/types/index.d.ts +3 -0
  14. package/build/es2019/types/index.d.ts.map +1 -0
  15. package/build/es2019/types/index.js +3 -0
  16. package/build/es2019/types/index.js.map +1 -0
  17. package/build/es2019/types/worker-timers-broker-loader.d.ts +4 -0
  18. package/build/es2019/types/worker-timers-broker-loader.d.ts.map +1 -0
  19. package/build/es2019/types/worker-timers-broker-loader.js +2 -0
  20. package/build/es2019/types/worker-timers-broker-loader.js.map +1 -0
  21. package/build/es2019/types/worker-timers-broker-wrapper.d.ts +4 -0
  22. package/build/es2019/types/worker-timers-broker-wrapper.d.ts.map +1 -0
  23. package/build/es2019/types/worker-timers-broker-wrapper.js +2 -0
  24. package/build/es2019/types/worker-timers-broker-wrapper.js.map +1 -0
  25. package/build/es5/bundle.js +92 -157
  26. package/package.json +14 -13
  27. package/src/interfaces/index.ts +1 -0
  28. package/src/interfaces/worker-timers-broker-definition.ts +11 -0
  29. package/src/module.ts +92 -161
  30. package/src/types/index.ts +2 -0
  31. package/src/types/worker-timers-broker-loader.ts +4 -0
  32. package/src/types/worker-timers-broker-wrapper.ts +4 -0
  33. package/build/es2019/guards/call-notification.d.ts +0 -3
  34. package/build/es2019/guards/call-notification.d.ts.map +0 -1
  35. package/build/es2019/guards/call-notification.js +0 -4
  36. package/build/es2019/guards/call-notification.js.map +0 -1
  37. package/build/es2019/guards/clear-response.d.ts +0 -3
  38. package/build/es2019/guards/clear-response.d.ts.map +0 -1
  39. package/build/es2019/guards/clear-response.js +0 -4
  40. package/build/es2019/guards/clear-response.js.map +0 -1
  41. package/src/guards/call-notification.ts +0 -5
  42. package/src/guards/clear-response.ts +0 -5
@@ -0,0 +1,2 @@
1
+ export * from './worker-timers-broker-definition';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -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,2 @@
1
+ export * from './worker-timers-broker-definition';
2
+ //# sourceMappingURL=index.js.map
@@ -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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=worker-timers-broker-definition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-timers-broker-definition.js","sourceRoot":"","sources":["../../../src/interfaces/worker-timers-broker-definition.ts"],"names":[],"mappings":""}
@@ -1,7 +1,6 @@
1
- export declare const load: (url: string) => {
2
- clearInterval: (timerId: number) => void;
3
- clearTimeout: (timerId: number) => void;
4
- setInterval: (func: Function, delay?: number, ...args: any[]) => number;
5
- setTimeout: (func: Function, delay?: number, ...args: any[]) => number;
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":"AAKA,eAAO,MAAM,IAAI,QAAS,MAAM;6BAqFI,MAAM;4BAeP,MAAM;wBAeV,QAAQ,2BAAsB,GAAG,EAAE;uBAmCpC,QAAQ,2BAAsB,GAAG,EAAE;CAyBhE,CAAC"}
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"}
@@ -1,146 +1,89 @@
1
+ import { createBroker } from 'broker-factory';
1
2
  import { generateUniqueNumber } from 'fast-unique-numbers';
2
- import { isCallNotification } from './guards/call-notification';
3
- import { isClearResponse } from './guards/clear-response';
4
- export const load = (url) => {
5
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
6
- const scheduledIntervalFunctions = new Map([[0, () => { }]]); // tslint:disable-line no-empty
7
- const scheduledTimeoutFunctions = new Map([[0, () => { }]]); // tslint:disable-line no-empty
8
- const unrespondedRequests = new Map();
9
- const worker = new Worker(url);
10
- worker.addEventListener('message', ({ data }) => {
11
- if (isCallNotification(data)) {
12
- const { params: { timerId, timerType } } = data;
13
- if (timerType === 'interval') {
14
- const idOrFunc = scheduledIntervalFunctions.get(timerId);
15
- if (typeof idOrFunc === undefined) {
16
- throw new Error('The timer is in an undefined state.');
17
- }
18
- if (typeof idOrFunc === 'number') {
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
- else {
66
- const { error: { message } } = data;
67
- throw new Error(message);
68
- }
69
- });
70
- const clearInterval = (timerId) => {
71
- if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
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
- worker.postMessage({
113
- id: null,
114
- method: 'set',
115
- params: {
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
- return timerId;
123
- };
124
- const setTimeout = (func, delay = 0, ...args) => {
125
- const timerId = generateUniqueNumber(scheduledTimeoutFunctions);
126
- scheduledTimeoutFunctions.set(timerId, () => func(...args));
127
- worker.postMessage({
128
- id: null,
129
- method: 'set',
130
- params: {
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
- return timerId;
138
- };
139
- return {
140
- clearInterval,
141
- clearTimeout,
142
- setInterval,
143
- setTimeout
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,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAE3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,GAAW,EAAE,EAAE;IAChC,2GAA2G;IAC3G,MAAM,0BAA0B,GAAmC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,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CAAC;IACN,CAAC;CACJ,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,IAAI,GAA8B,CAAC,GAAW,EAAE,EAAE;IAC3D,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;IAE/B,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './worker-timers-broker-loader';
2
+ export * from './worker-timers-broker-wrapper';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -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,3 @@
1
+ export * from './worker-timers-broker-loader';
2
+ export * from './worker-timers-broker-wrapper';
3
+ //# sourceMappingURL=index.js.map
@@ -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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=worker-timers-broker-loader.js.map
@@ -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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=worker-timers-broker-wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-timers-broker-wrapper.js","sourceRoot":"","sources":["../../../src/types/worker-timers-broker-wrapper.ts"],"names":[],"mappings":""}
@@ -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
- var isCallNotification = function isCallNotification(message) {
8
- return message.method !== undefined && message.method === 'call';
9
- };
10
-
11
- var isClearResponse = function isClearResponse(message) {
12
- return typeof message.id === 'number' && typeof message.result === 'boolean';
13
- };
14
-
15
- var load = function load(url) {
16
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
17
- var scheduledIntervalFunctions = new Map([[0, function () {}]]); // tslint:disable-line no-empty
18
- var scheduledTimeoutFunctions = new Map([[0, function () {}]]); // tslint:disable-line no-empty
19
- var unrespondedRequests = new Map();
20
- var worker = new Worker(url);
21
- worker.addEventListener('message', function (_ref) {
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
- } else if (isClearResponse(data)) {
57
- var id = data.id;
58
- var _timerIdAndTimerType2 = unrespondedRequests.get(id);
59
- if (_timerIdAndTimerType2 === undefined) {
60
- throw new Error('The timer is in an undefined state.');
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
- var _timerId = _timerIdAndTimerType2.timerId,
63
- _timerType = _timerIdAndTimerType2.timerType;
64
- unrespondedRequests["delete"](id);
65
- if (_timerType === 'interval') {
66
- scheduledIntervalFunctions["delete"](_timerId);
67
- } else {
68
- scheduledTimeoutFunctions["delete"](_timerId);
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
- } else {
71
- var message = data.error.message;
72
- throw new Error(message);
73
- }
74
- });
75
- var clearInterval = function clearInterval(timerId) {
76
- if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
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
- var clearTimeout = function clearTimeout(timerId) {
94
- if (typeof scheduledTimeoutFunctions.get(timerId) === 'function') {
95
- var id = fastUniqueNumbers.generateUniqueNumber(unrespondedRequests);
96
- unrespondedRequests.set(id, {
97
- timerId: timerId,
98
- timerType: 'timeout'
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
- worker.postMessage({
134
- id: null,
135
- method: 'set',
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
- return timerId;
165
- };
166
- return {
167
- clearInterval: clearInterval,
168
- clearTimeout: clearTimeout,
169
- setInterval: setInterval,
170
- setTimeout: setTimeout
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.0",
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": "^8.0.10"
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.0",
20
+ "@babel/core": "^7.26.8",
20
21
  "@babel/plugin-external-helpers": "^7.25.9",
21
- "@babel/plugin-transform-runtime": "^7.25.9",
22
- "@babel/preset-env": "^7.26.0",
23
- "@commitlint/cli": "^19.6.1",
24
- "@commitlint/config-angular": "^19.7.0",
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.0",
44
+ "lint-staged": "^15.4.3",
44
45
  "load-grunt-config": "^4.0.1",
45
46
  "mocha": "^10.8.2",
46
- "prettier": "^3.4.2",
47
+ "prettier": "^3.5.0",
47
48
  "rimraf": "^6.0.1",
48
- "rollup": "^4.30.0",
49
+ "rollup": "^4.34.6",
49
50
  "sinon": "^17.0.2",
50
51
  "sinon-chai": "^3.7.0",
51
- "ts-loader": "^9.5.1",
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.2",
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": "7.1.9"
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 { IClearRequest, ISetNotification, IWorkerEvent, TTimerType } from 'worker-timers-worker';
3
- import { isCallNotification } from './guards/call-notification';
4
- import { isClearResponse } from './guards/clear-response';
5
-
6
- export const load = (url: string) => {
7
- // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
8
- const scheduledIntervalFunctions: Map<number, number | Function> = new Map([[0, () => {}]]); // tslint:disable-line no-empty
9
- const scheduledTimeoutFunctions: Map<number, number | Function> = new Map([[0, () => {}]]); // tslint:disable-line no-empty
10
- const unrespondedRequests: Map<number, { timerId: number; timerType: TTimerType }> = new Map();
11
-
12
- const worker = new Worker(url);
13
-
14
- worker.addEventListener('message', ({ data }: IWorkerEvent) => {
15
- if (isCallNotification(data)) {
16
- const {
17
- params: { timerId, timerType }
18
- } = data;
19
-
20
- if (timerType === 'interval') {
21
- const idOrFunc = scheduledIntervalFunctions.get(timerId);
22
-
23
- if (typeof idOrFunc === undefined) {
24
- throw new Error('The timer is in an undefined state.');
25
- }
26
-
27
- if (typeof idOrFunc === 'number') {
28
- const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
29
-
30
- if (
31
- timerIdAndTimerType === undefined ||
32
- timerIdAndTimerType.timerId !== timerId ||
33
- timerIdAndTimerType.timerType !== timerType
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
- if (typeof idOrFunc === 'number') {
48
- const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
61
+ if (state === symbol) {
62
+ func(...args);
49
63
 
50
- if (
51
- timerIdAndTimerType === undefined ||
52
- timerIdAndTimerType.timerId !== timerId ||
53
- timerIdAndTimerType.timerType !== timerType
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
- } else if (typeof idOrFunc === 'function') {
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
- const clearTimeout = (timerId: number) => {
107
- if (typeof scheduledTimeoutFunctions.get(timerId) === 'function') {
108
- const id = generateUniqueNumber(unrespondedRequests);
71
+ schedule();
109
72
 
110
- unrespondedRequests.set(id, { timerId, timerType: 'timeout' });
111
- scheduledTimeoutFunctions.set(timerId, id);
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
- worker.postMessage(<IClearRequest>{
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
- worker.postMessage(<ISetNotification>{
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: 'interval'
150
- }
151
- });
87
+ timerType: 'timeout'
88
+ }).then(() => {
89
+ const state = scheduledTimeoutsState.get(timerId);
152
90
 
153
- return timerId;
154
- };
91
+ if (typeof state === 'undefined') {
92
+ throw new Error('The timer is in an undefined state.');
93
+ }
155
94
 
156
- const setTimeout = (func: Function, delay = 0, ...args: any[]) => {
157
- const timerId = generateUniqueNumber(scheduledTimeoutFunctions);
95
+ if (state === symbol) {
96
+ // A timeout can be savely deleted because it is only called once.
97
+ scheduledTimeoutsState.delete(timerId);
158
98
 
159
- scheduledTimeoutFunctions.set(timerId, () => func(...args));
99
+ func(...args);
100
+ }
101
+ });
160
102
 
161
- worker.postMessage(<ISetNotification>{
162
- id: null,
163
- method: 'set',
164
- params: {
165
- delay,
166
- now: performance.timeOrigin + performance.now(),
167
- timerId,
168
- timerType: 'timeout'
169
- }
170
- });
103
+ return timerId;
104
+ };
105
+ }
106
+ });
171
107
 
172
- return timerId;
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
  };
@@ -0,0 +1,2 @@
1
+ export * from './worker-timers-broker-loader';
2
+ export * from './worker-timers-broker-wrapper';
@@ -0,0 +1,4 @@
1
+ import { IDefaultBrokerDefinition } from 'broker-factory';
2
+ import { IWorkerTimersBrokerDefinition } from '../interfaces';
3
+
4
+ export type TWorkerTimersBrokerLoader = (url: string) => IWorkerTimersBrokerDefinition & IDefaultBrokerDefinition;
@@ -0,0 +1,4 @@
1
+ import { IDefaultBrokerDefinition } from 'broker-factory';
2
+ import { IWorkerTimersBrokerDefinition } from '../interfaces';
3
+
4
+ export type TWorkerTimersBrokerWrapper = (sender: MessagePort | Worker) => IWorkerTimersBrokerDefinition & IDefaultBrokerDefinition;
@@ -1,3 +0,0 @@
1
- import { ICallNotification, TWorkerMessage } from 'worker-timers-worker';
2
- export declare const isCallNotification: (message: TWorkerMessage) => message is ICallNotification;
3
- //# sourceMappingURL=call-notification.d.ts.map
@@ -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,4 +0,0 @@
1
- export const isCallNotification = (message) => {
2
- return message.method !== undefined && message.method === 'call';
3
- };
4
- //# sourceMappingURL=call-notification.js.map
@@ -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,3 +0,0 @@
1
- import { IClearResponse, TWorkerMessage } from 'worker-timers-worker';
2
- export declare const isClearResponse: (message: TWorkerMessage) => message is IClearResponse;
3
- //# sourceMappingURL=clear-response.d.ts.map
@@ -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,4 +0,0 @@
1
- export const isClearResponse = (message) => {
2
- return typeof message.id === 'number' && typeof message.result === 'boolean';
3
- };
4
- //# sourceMappingURL=clear-response.js.map
@@ -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
- };
@@ -1,5 +0,0 @@
1
- import { IClearResponse, TWorkerMessage } from 'worker-timers-worker';
2
-
3
- export const isClearResponse = (message: TWorkerMessage): message is IClearResponse => {
4
- return typeof message.id === 'number' && typeof (<IClearResponse>message).result === 'boolean';
5
- };