worker-timers-broker 8.0.10 → 8.0.12

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.
@@ -0,0 +1,2 @@
1
+ export declare const createClearIntervalFactory: (scheduledIntervalsState: Map<number, null | symbol>) => (clear: (timerId: number) => Promise<boolean>) => (timerId: number) => void;
2
+ //# sourceMappingURL=clear-interval-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clear-interval-factory.d.ts","sourceRoot":"","sources":["../../../src/factories/clear-interval-factory.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,0BAA0B,GAClC,yBAAyB,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,MAAM,OAAO,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,MAAM,SAAS,MAAM,SAQ1H,CAAC"}
@@ -0,0 +1,9 @@
1
+ export const createClearIntervalFactory = (scheduledIntervalsState) => (clear) => (timerId) => {
2
+ if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
3
+ scheduledIntervalsState.set(timerId, null);
4
+ clear(timerId).then(() => {
5
+ scheduledIntervalsState.delete(timerId);
6
+ });
7
+ }
8
+ };
9
+ //# sourceMappingURL=clear-interval-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clear-interval-factory.js","sourceRoot":"","sources":["../../../src/factories/clear-interval-factory.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,0BAA0B,GACnC,CAAC,uBAAmD,EAAE,EAAE,CAAC,CAAC,KAA4C,EAAE,EAAE,CAAC,CAAC,OAAe,EAAE,EAAE;IAC3H,IAAI,OAAO,uBAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC3D,uBAAuB,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAE3C,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACrB,uBAAuB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const createClearTimeoutFactory: (scheduledTimeoutsState: Map<number, null | symbol>) => (clear: (timerId: number) => Promise<boolean>) => (timerId: number) => void;
2
+ //# sourceMappingURL=clear-timeout-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clear-timeout-factory.d.ts","sourceRoot":"","sources":["../../../src/factories/clear-timeout-factory.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,yBAAyB,GACjC,wBAAwB,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,MAAM,OAAO,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,MAAM,SAAS,MAAM,SAQzH,CAAC"}
@@ -0,0 +1,9 @@
1
+ export const createClearTimeoutFactory = (scheduledTimeoutsState) => (clear) => (timerId) => {
2
+ if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
3
+ scheduledTimeoutsState.set(timerId, null);
4
+ clear(timerId).then(() => {
5
+ scheduledTimeoutsState.delete(timerId);
6
+ });
7
+ }
8
+ };
9
+ //# sourceMappingURL=clear-timeout-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clear-timeout-factory.js","sourceRoot":"","sources":["../../../src/factories/clear-timeout-factory.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,yBAAyB,GAClC,CAAC,sBAAkD,EAAE,EAAE,CAAC,CAAC,KAA4C,EAAE,EAAE,CAAC,CAAC,OAAe,EAAE,EAAE;IAC1H,IAAI,OAAO,sBAAsB,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC1D,sBAAsB,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAE1C,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACrB,sBAAsB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
2
+ export declare const createSetIntervalFactory: (generateUniqueNumber: typeof generateUniqueNumberFunction, scheduledIntervalsState: Map<number, null | symbol>) => (set: (delay: number, timerId: number) => Promise<boolean>) => (func: Function, delay?: number, ...args: any[]) => number;
3
+ //# sourceMappingURL=set-interval-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-interval-factory.d.ts","sourceRoot":"","sources":["../../../src/factories/set-interval-factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,IAAI,4BAA4B,EAAE,MAAM,qBAAqB,CAAC;AAEhG,eAAO,MAAM,wBAAwB,GAChC,sBAAsB,OAAO,4BAA4B,EAAE,yBAAyB,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,MAC9G,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,MACzD,MAAM,QAAQ,EAAE,cAAS,EAAE,GAAG,MAAM,GAAG,EAAE,WA2BzC,CAAC"}
@@ -0,0 +1,21 @@
1
+ export const createSetIntervalFactory = (generateUniqueNumber, scheduledIntervalsState) => (set) => (func, delay = 0, ...args) => {
2
+ const symbol = Symbol();
3
+ const timerId = generateUniqueNumber(scheduledIntervalsState);
4
+ scheduledIntervalsState.set(timerId, symbol);
5
+ const schedule = () => set(delay, timerId).then(() => {
6
+ const state = scheduledIntervalsState.get(timerId);
7
+ if (state === undefined) {
8
+ throw new Error('The timer is in an undefined state.');
9
+ }
10
+ if (state === symbol) {
11
+ func(...args);
12
+ // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
13
+ if (scheduledIntervalsState.get(timerId) === symbol) {
14
+ schedule();
15
+ }
16
+ }
17
+ });
18
+ schedule();
19
+ return timerId;
20
+ };
21
+ //# sourceMappingURL=set-interval-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-interval-factory.js","sourceRoot":"","sources":["../../../src/factories/set-interval-factory.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,wBAAwB,GACjC,CAAC,oBAAyD,EAAE,uBAAmD,EAAE,EAAE,CACnH,CAAC,GAAyD,EAAE,EAAE,CAC9D,CAAC,IAAc,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,IAAW,EAAE,EAAE;IAC1C,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,MAAM,OAAO,GAAG,oBAAoB,CAAC,uBAAuB,CAAC,CAAC;IAE9D,uBAAuB,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAE7C,MAAM,QAAQ,GAAG,GAAG,EAAE,CAClB,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;QAC1B,MAAM,KAAK,GAAG,uBAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAEnD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACnB,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;YAEd,+GAA+G;YAC/G,IAAI,uBAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,MAAM,EAAE,CAAC;gBAClD,QAAQ,EAAE,CAAC;YACf,CAAC;QACL,CAAC;IACL,CAAC,CAAC,CAAC;IAEP,QAAQ,EAAE,CAAC;IAEX,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
2
+ export declare const createSetTimeoutFactory: (generateUniqueNumber: typeof generateUniqueNumberFunction, scheduledTimeoutsState: Map<number, null | symbol>) => (set: (delay: number, timerId: number) => Promise<boolean>) => (func: Function, delay?: number, ...args: any[]) => number;
3
+ //# sourceMappingURL=set-timeout-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-timeout-factory.d.ts","sourceRoot":"","sources":["../../../src/factories/set-timeout-factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,IAAI,4BAA4B,EAAE,MAAM,qBAAqB,CAAC;AAEhG,eAAO,MAAM,uBAAuB,GAC/B,sBAAsB,OAAO,4BAA4B,EAAE,wBAAwB,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,MAC7G,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,MACzD,MAAM,QAAQ,EAAE,cAAS,EAAE,GAAG,MAAM,GAAG,EAAE,WAsBzC,CAAC"}
@@ -0,0 +1,18 @@
1
+ export const createSetTimeoutFactory = (generateUniqueNumber, scheduledTimeoutsState) => (set) => (func, delay = 0, ...args) => {
2
+ const symbol = Symbol();
3
+ const timerId = generateUniqueNumber(scheduledTimeoutsState);
4
+ scheduledTimeoutsState.set(timerId, symbol);
5
+ set(delay, timerId).then(() => {
6
+ const state = scheduledTimeoutsState.get(timerId);
7
+ if (state === undefined) {
8
+ throw new Error('The timer is in an undefined state.');
9
+ }
10
+ if (state === symbol) {
11
+ // A timeout can be savely deleted because it is only called once.
12
+ scheduledTimeoutsState.delete(timerId);
13
+ func(...args);
14
+ }
15
+ });
16
+ return timerId;
17
+ };
18
+ //# sourceMappingURL=set-timeout-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-timeout-factory.js","sourceRoot":"","sources":["../../../src/factories/set-timeout-factory.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,uBAAuB,GAChC,CAAC,oBAAyD,EAAE,sBAAkD,EAAE,EAAE,CAClH,CAAC,GAAyD,EAAE,EAAE,CAC9D,CAAC,IAAc,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,IAAW,EAAE,EAAE;IAC1C,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,MAAM,OAAO,GAAG,oBAAoB,CAAC,sBAAsB,CAAC,CAAC;IAE7D,sBAAsB,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAE5C,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;QAC1B,MAAM,KAAK,GAAG,sBAAsB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAElD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACnB,kEAAkE;YAClE,sBAAsB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAEvC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QAClB,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC"}
@@ -1 +1 @@
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
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,SAAS,CAAC;AAMhF,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAW9B,eAAO,MAAM,IAAI,EAAE,0BAWjB,CAAC;AAEH,eAAO,MAAM,IAAI,EAAE,yBAIlB,CAAC"}
@@ -1,5 +1,9 @@
1
1
  import { createBroker } from 'broker-factory';
2
2
  import { generateUniqueNumber } from 'fast-unique-numbers';
3
+ import { createClearIntervalFactory } from './factories/clear-interval-factory';
4
+ import { createClearTimeoutFactory } from './factories/clear-timeout-factory';
5
+ import { createSetIntervalFactory } from './factories/set-interval-factory';
6
+ import { createSetTimeoutFactory } from './factories/set-timeout-factory';
3
7
  /*
4
8
  * @todo Explicitly referencing the barrel file seems to be necessary when enabling the
5
9
  * isolatedModules compiler option.
@@ -9,78 +13,15 @@ export * from './types/index';
9
13
  // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
10
14
  const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
11
15
  const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
16
+ const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
17
+ const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
18
+ const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
19
+ const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
12
20
  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
- });
20
- }
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);
29
- });
30
- }
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', {
39
- delay,
40
- now: performance.timeOrigin + performance.now(),
41
- timerId,
42
- timerType: 'interval'
43
- }).then(() => {
44
- const state = scheduledIntervalsState.get(timerId);
45
- if (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', {
66
- delay,
67
- now: performance.timeOrigin + performance.now(),
68
- timerId,
69
- timerType: 'timeout'
70
- }).then(() => {
71
- const state = scheduledTimeoutsState.get(timerId);
72
- if (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
- }
21
+ clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
22
+ clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
23
+ setInterval: ({ call }) => createSetInterval((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })),
24
+ setTimeout: ({ call }) => createSetTimeout((delay, timerId) => call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' }))
84
25
  });
85
26
  export const load = (url) => {
86
27
  const worker = new Worker(url);
@@ -1 +1 @@
1
-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AAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;IAC7G,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CACtB,iBAAiB,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CACjC,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC,CAC1G;IACL,UAAU,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CACrB,gBAAgB,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAChC,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,UAAU,GAAG,WAAW,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CACzG;CACR,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"}
@@ -1,58 +1,47 @@
1
1
  (function (global, factory) {
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';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('broker-factory'), require('fast-unique-numbers'), require('@babel/runtime/helpers/typeof')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'broker-factory', 'fast-unique-numbers', '@babel/runtime/helpers/typeof'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.workerTimersBroker = {}, global.brokerFactory, global.fastUniqueNumbers, global._typeof));
5
+ })(this, (function (exports, brokerFactory, fastUniqueNumbers, _typeof) { 'use strict';
6
6
 
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;
7
+ var createClearIntervalFactory = function createClearIntervalFactory(scheduledIntervalsState) {
8
+ return function (clear) {
13
9
  return function (timerId) {
14
10
  if (_typeof(scheduledIntervalsState.get(timerId)) === 'symbol') {
15
11
  scheduledIntervalsState.set(timerId, null);
16
- call('clear', {
17
- timerId: timerId,
18
- timerType: 'interval'
19
- }).then(function () {
12
+ clear(timerId).then(function () {
20
13
  scheduledIntervalsState["delete"](timerId);
21
14
  });
22
15
  }
23
16
  };
24
- },
25
- clearTimeout: function clearTimeout(_ref2) {
26
- var call = _ref2.call;
17
+ };
18
+ };
19
+
20
+ var createClearTimeoutFactory = function createClearTimeoutFactory(scheduledTimeoutsState) {
21
+ return function (clear) {
27
22
  return function (timerId) {
28
23
  if (_typeof(scheduledTimeoutsState.get(timerId)) === 'symbol') {
29
24
  scheduledTimeoutsState.set(timerId, null);
30
- call('clear', {
31
- timerId: timerId,
32
- timerType: 'timeout'
33
- }).then(function () {
25
+ clear(timerId).then(function () {
34
26
  scheduledTimeoutsState["delete"](timerId);
35
27
  });
36
28
  }
37
29
  };
38
- },
39
- setInterval: function setInterval(_ref3) {
40
- var call = _ref3.call;
30
+ };
31
+ };
32
+
33
+ var createSetIntervalFactory = function createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState) {
34
+ return function (set) {
41
35
  return function (func) {
42
36
  var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
43
37
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
44
38
  args[_key - 2] = arguments[_key];
45
39
  }
46
40
  var symbol = Symbol();
47
- var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledIntervalsState);
41
+ var timerId = generateUniqueNumber(scheduledIntervalsState);
48
42
  scheduledIntervalsState.set(timerId, symbol);
49
43
  var _schedule = function schedule() {
50
- return call('set', {
51
- delay: delay,
52
- now: performance.timeOrigin + performance.now(),
53
- timerId: timerId,
54
- timerType: 'interval'
55
- }).then(function () {
44
+ return set(delay, timerId).then(function () {
56
45
  var state = scheduledIntervalsState.get(timerId);
57
46
  if (state === undefined) {
58
47
  throw new Error('The timer is in an undefined state.');
@@ -69,23 +58,20 @@
69
58
  _schedule();
70
59
  return timerId;
71
60
  };
72
- },
73
- setTimeout: function setTimeout(_ref4) {
74
- var call = _ref4.call;
61
+ };
62
+ };
63
+
64
+ var createSetTimeoutFactory = function createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState) {
65
+ return function (set) {
75
66
  return function (func) {
76
67
  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];
68
+ for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
69
+ args[_key - 2] = arguments[_key];
79
70
  }
80
71
  var symbol = Symbol();
81
- var timerId = fastUniqueNumbers.generateUniqueNumber(scheduledTimeoutsState);
72
+ var timerId = generateUniqueNumber(scheduledTimeoutsState);
82
73
  scheduledTimeoutsState.set(timerId, symbol);
83
- call('set', {
84
- delay: delay,
85
- now: performance.timeOrigin + performance.now(),
86
- timerId: timerId,
87
- timerType: 'timeout'
88
- }).then(function () {
74
+ set(delay, timerId).then(function () {
89
75
  var state = scheduledTimeoutsState.get(timerId);
90
76
  if (state === undefined) {
91
77
  throw new Error('The timer is in an undefined state.');
@@ -98,6 +84,56 @@
98
84
  });
99
85
  return timerId;
100
86
  };
87
+ };
88
+ };
89
+
90
+ // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
91
+ var scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
92
+ var scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
93
+ var createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
94
+ var createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
95
+ var createSetInterval = createSetIntervalFactory(fastUniqueNumbers.generateUniqueNumber, scheduledIntervalsState);
96
+ var createSetTimeout = createSetTimeoutFactory(fastUniqueNumbers.generateUniqueNumber, scheduledTimeoutsState);
97
+ var wrap = brokerFactory.createBroker({
98
+ clearInterval: function clearInterval(_ref) {
99
+ var call = _ref.call;
100
+ return createClearInterval(function (timerId) {
101
+ return call('clear', {
102
+ timerId: timerId,
103
+ timerType: 'interval'
104
+ });
105
+ });
106
+ },
107
+ clearTimeout: function clearTimeout(_ref2) {
108
+ var call = _ref2.call;
109
+ return createClearTimeout(function (timerId) {
110
+ return call('clear', {
111
+ timerId: timerId,
112
+ timerType: 'timeout'
113
+ });
114
+ });
115
+ },
116
+ setInterval: function setInterval(_ref3) {
117
+ var call = _ref3.call;
118
+ return createSetInterval(function (delay, timerId) {
119
+ return call('set', {
120
+ delay: delay,
121
+ now: performance.timeOrigin + performance.now(),
122
+ timerId: timerId,
123
+ timerType: 'interval'
124
+ });
125
+ });
126
+ },
127
+ setTimeout: function setTimeout(_ref4) {
128
+ var call = _ref4.call;
129
+ return createSetTimeout(function (delay, timerId) {
130
+ return call('set', {
131
+ delay: delay,
132
+ now: performance.timeOrigin + performance.now(),
133
+ timerId: timerId,
134
+ timerType: 'timeout'
135
+ });
136
+ });
101
137
  }
102
138
  });
103
139
  var load = function load(url) {
package/package.json CHANGED
@@ -9,48 +9,39 @@
9
9
  }
10
10
  },
11
11
  "dependencies": {
12
- "@babel/runtime": "^7.28.3",
13
- "broker-factory": "^3.1.9",
14
- "fast-unique-numbers": "^9.0.23",
12
+ "@babel/runtime": "^7.28.4",
13
+ "broker-factory": "^3.1.10",
14
+ "fast-unique-numbers": "^9.0.24",
15
15
  "tslib": "^2.8.1",
16
- "worker-timers-worker": "^9.0.10"
16
+ "worker-timers-worker": "^9.0.11"
17
17
  },
18
18
  "description": "The broker which is used by the worker-timers package.",
19
19
  "devDependencies": {
20
- "@babel/core": "^7.28.3",
20
+ "@babel/core": "^7.28.5",
21
21
  "@babel/plugin-external-helpers": "^7.27.1",
22
- "@babel/plugin-transform-runtime": "^7.28.3",
23
- "@babel/preset-env": "^7.28.3",
22
+ "@babel/plugin-transform-runtime": "^7.28.5",
23
+ "@babel/preset-env": "^7.28.5",
24
24
  "@commitlint/cli": "^19.8.1",
25
25
  "@commitlint/config-angular": "^19.8.1",
26
- "@rollup/plugin-babel": "^6.0.4",
27
- "chai": "^4.3.10",
26
+ "@rollup/plugin-babel": "^6.1.0",
27
+ "@vitest/browser-webdriverio": "^4.0.15",
28
+ "chai": "^6.2.1",
28
29
  "commitizen": "^4.3.1",
29
30
  "cz-conventional-changelog": "^3.3.0",
30
31
  "eslint": "^8.57.0",
31
- "eslint-config-holy-grail": "^61.0.0",
32
+ "eslint-config-holy-grail": "^61.0.3",
32
33
  "husky": "^9.1.7",
33
- "karma": "^6.4.4",
34
- "karma-chrome-launcher": "^3.2.0",
35
- "karma-firefox-launcher": "^2.1.3",
36
- "karma-mocha": "^2.0.1",
37
- "karma-sauce-launcher": "^4.3.6",
38
- "karma-sinon-chai": "^2.0.2",
39
- "karma-webkit-launcher": "^2.6.0",
40
- "karma-webpack": "^5.0.1",
41
- "lint-staged": "^16.1.5",
42
- "mocha": "^11.7.1",
43
- "prettier": "^3.6.2",
44
- "rimraf": "^6.0.1",
45
- "rollup": "^4.46.2",
46
- "sinon": "^17.0.2",
47
- "sinon-chai": "^3.7.0",
48
- "ts-loader": "^9.5.2",
34
+ "lint-staged": "^16.2.7",
35
+ "prettier": "^3.7.4",
36
+ "rimraf": "^6.1.2",
37
+ "rollup": "^4.53.3",
38
+ "sinon": "^21.0.0",
39
+ "sinon-chai": "^4.0.1",
49
40
  "tsconfig-holy-grail": "^15.0.2",
50
41
  "tslint": "^6.1.3",
51
42
  "tslint-config-holy-grail": "^56.0.6",
52
- "typescript": "^5.9.2",
53
- "webpack": "^5.101.2"
43
+ "typescript": "^5.9.3",
44
+ "vitest": "^4.0.15"
54
45
  },
55
46
  "files": [
56
47
  "build/es2019/",
@@ -75,8 +66,8 @@
75
66
  "prepare": "husky",
76
67
  "prepublishOnly": "npm run build",
77
68
  "test": "npm run lint && npm run build && npm run test:unit",
78
- "test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then karma start config/karma/config-unit.js --single-run; fi"
69
+ "test:unit": "if [ \"$TYPE\" = \"\" -o \"$TYPE\" = \"unit\" ]; then npx vitest --config config/vitest/unit.ts; fi"
79
70
  },
80
71
  "types": "build/es2019/module.d.ts",
81
- "version": "8.0.10"
72
+ "version": "8.0.12"
82
73
  }
@@ -0,0 +1,10 @@
1
+ export const createClearIntervalFactory =
2
+ (scheduledIntervalsState: Map<number, null | symbol>) => (clear: (timerId: number) => Promise<boolean>) => (timerId: number) => {
3
+ if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
4
+ scheduledIntervalsState.set(timerId, null);
5
+
6
+ clear(timerId).then(() => {
7
+ scheduledIntervalsState.delete(timerId);
8
+ });
9
+ }
10
+ };
@@ -0,0 +1,10 @@
1
+ export const createClearTimeoutFactory =
2
+ (scheduledTimeoutsState: Map<number, null | symbol>) => (clear: (timerId: number) => Promise<boolean>) => (timerId: number) => {
3
+ if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
4
+ scheduledTimeoutsState.set(timerId, null);
5
+
6
+ clear(timerId).then(() => {
7
+ scheduledTimeoutsState.delete(timerId);
8
+ });
9
+ }
10
+ };
@@ -0,0 +1,33 @@
1
+ import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
2
+
3
+ export const createSetIntervalFactory =
4
+ (generateUniqueNumber: typeof generateUniqueNumberFunction, scheduledIntervalsState: Map<number, null | symbol>) =>
5
+ (set: (delay: number, timerId: number) => Promise<boolean>) =>
6
+ (func: Function, delay = 0, ...args: any[]) => {
7
+ const symbol = Symbol();
8
+ const timerId = generateUniqueNumber(scheduledIntervalsState);
9
+
10
+ scheduledIntervalsState.set(timerId, symbol);
11
+
12
+ const schedule = () =>
13
+ set(delay, timerId).then(() => {
14
+ const state = scheduledIntervalsState.get(timerId);
15
+
16
+ if (state === undefined) {
17
+ throw new Error('The timer is in an undefined state.');
18
+ }
19
+
20
+ if (state === symbol) {
21
+ func(...args);
22
+
23
+ // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
24
+ if (scheduledIntervalsState.get(timerId) === symbol) {
25
+ schedule();
26
+ }
27
+ }
28
+ });
29
+
30
+ schedule();
31
+
32
+ return timerId;
33
+ };
@@ -0,0 +1,28 @@
1
+ import type { generateUniqueNumber as generateUniqueNumberFunction } from 'fast-unique-numbers';
2
+
3
+ export const createSetTimeoutFactory =
4
+ (generateUniqueNumber: typeof generateUniqueNumberFunction, scheduledTimeoutsState: Map<number, null | symbol>) =>
5
+ (set: (delay: number, timerId: number) => Promise<boolean>) =>
6
+ (func: Function, delay = 0, ...args: any[]) => {
7
+ const symbol = Symbol();
8
+ const timerId = generateUniqueNumber(scheduledTimeoutsState);
9
+
10
+ scheduledTimeoutsState.set(timerId, symbol);
11
+
12
+ set(delay, timerId).then(() => {
13
+ const state = scheduledTimeoutsState.get(timerId);
14
+
15
+ if (state === undefined) {
16
+ throw new Error('The timer is in an undefined state.');
17
+ }
18
+
19
+ if (state === symbol) {
20
+ // A timeout can be savely deleted because it is only called once.
21
+ scheduledTimeoutsState.delete(timerId);
22
+
23
+ func(...args);
24
+ }
25
+ });
26
+
27
+ return timerId;
28
+ };
package/src/module.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import { createBroker } from 'broker-factory';
2
2
  import { generateUniqueNumber } from 'fast-unique-numbers';
3
3
  import { TWorkerTimersWorkerDefinition } from 'worker-timers-worker';
4
+ import { createClearIntervalFactory } from './factories/clear-interval-factory';
5
+ import { createClearTimeoutFactory } from './factories/clear-timeout-factory';
6
+ import { createSetIntervalFactory } from './factories/set-interval-factory';
7
+ import { createSetTimeoutFactory } from './factories/set-timeout-factory';
4
8
  import { IWorkerTimersBrokerDefinition } from './interfaces';
5
9
  import { TWorkerTimersBrokerLoader, TWorkerTimersBrokerWrapper } from './types';
6
10
 
@@ -15,94 +19,22 @@ export * from './types/index';
15
19
  const scheduledIntervalsState: Map<number, null | symbol> = new Map([[0, null]]); // tslint:disable-line no-empty
16
20
  const scheduledTimeoutsState: Map<number, null | symbol> = new Map([[0, null]]); // tslint:disable-line no-empty
17
21
 
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 (state === undefined) {
58
- throw new Error('The timer is in an undefined state.');
59
- }
60
-
61
- if (state === symbol) {
62
- func(...args);
63
-
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
- }
68
- }
69
- });
22
+ const createClearInterval = createClearIntervalFactory(scheduledIntervalsState);
23
+ const createClearTimeout = createClearTimeoutFactory(scheduledTimeoutsState);
24
+ const createSetInterval = createSetIntervalFactory(generateUniqueNumber, scheduledIntervalsState);
25
+ const createSetTimeout = createSetTimeoutFactory(generateUniqueNumber, scheduledTimeoutsState);
70
26
 
71
- schedule();
72
-
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);
80
-
81
- scheduledTimeoutsState.set(timerId, symbol);
82
-
83
- call('set', {
84
- delay,
85
- now: performance.timeOrigin + performance.now(),
86
- timerId,
87
- timerType: 'timeout'
88
- }).then(() => {
89
- const state = scheduledTimeoutsState.get(timerId);
90
-
91
- if (state === undefined) {
92
- throw new Error('The timer is in an undefined state.');
93
- }
94
-
95
- if (state === symbol) {
96
- // A timeout can be savely deleted because it is only called once.
97
- scheduledTimeoutsState.delete(timerId);
98
-
99
- func(...args);
100
- }
101
- });
102
-
103
- return timerId;
104
- };
105
- }
27
+ export const wrap: TWorkerTimersBrokerWrapper = createBroker<IWorkerTimersBrokerDefinition, TWorkerTimersWorkerDefinition>({
28
+ clearInterval: ({ call }) => createClearInterval((timerId) => call('clear', { timerId, timerType: 'interval' })),
29
+ clearTimeout: ({ call }) => createClearTimeout((timerId) => call('clear', { timerId, timerType: 'timeout' })),
30
+ setInterval: ({ call }) =>
31
+ createSetInterval((delay, timerId) =>
32
+ call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'interval' })
33
+ ),
34
+ setTimeout: ({ call }) =>
35
+ createSetTimeout((delay, timerId) =>
36
+ call('set', { delay, now: performance.timeOrigin + performance.now(), timerId, timerType: 'timeout' })
37
+ )
106
38
  });
107
39
 
108
40
  export const load: TWorkerTimersBrokerLoader = (url: string) => {