worker-timers-worker 7.0.71 → 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.
@@ -1,5 +1,5 @@
1
- export declare const clearScheduledInterval: (timerId: number) => void;
2
- export declare const clearScheduledTimeout: (timerId: number) => void;
3
- export declare const scheduleInterval: (delay: number, timerId: number, nowInMainThread: number) => void;
4
- export declare const scheduleTimeout: (delay: number, timerId: number, nowInMainThread: number) => void;
1
+ export declare const clearScheduledInterval: (timerId: number) => boolean;
2
+ export declare const clearScheduledTimeout: (timerId: number) => boolean;
3
+ export declare const scheduleInterval: (delay: number, timerId: number, nowAndTimeOrigin: number) => void;
4
+ export declare const scheduleTimeout: (delay: number, timerId: number, nowAndTimeOrigin: number) => void;
5
5
  //# sourceMappingURL=timer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../../src/helpers/timer.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,sBAAsB,YAAa,MAAM,SASrD,CAAC;AAEF,eAAO,MAAM,qBAAqB,YAAa,MAAM,SASpD,CAAC;AA0BF,eAAO,MAAM,gBAAgB,UAAW,MAAM,WAAW,MAAM,mBAAmB,MAAM,SAOvF,CAAC;AAEF,eAAO,MAAM,eAAe,UAAW,MAAM,WAAW,MAAM,mBAAmB,MAAM,SAOtF,CAAC"}
1
+ {"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../../src/helpers/timer.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,sBAAsB,YAAa,MAAM,YAWrD,CAAC;AAEF,eAAO,MAAM,qBAAqB,YAAa,MAAM,YAWpD,CAAC;AAqBF,eAAO,MAAM,gBAAgB,UAAW,MAAM,WAAW,MAAM,oBAAoB,MAAM,SAOxF,CAAC;AAEF,eAAO,MAAM,eAAe,UAAW,MAAM,WAAW,MAAM,oBAAoB,MAAM,SAOvF,CAAC"}
@@ -2,49 +2,44 @@ const scheduledIntervalIdentifiers = new Map();
2
2
  const scheduledTimeoutIdentifiers = new Map();
3
3
  export const clearScheduledInterval = (timerId) => {
4
4
  const identifier = scheduledIntervalIdentifiers.get(timerId);
5
- if (identifier !== undefined) {
6
- clearTimeout(identifier);
7
- scheduledIntervalIdentifiers.delete(timerId);
8
- }
9
- else {
10
- throw new Error(`There is no interval scheduled with the given id "${timerId}".`);
5
+ if (identifier === undefined) {
6
+ return false;
11
7
  }
8
+ clearTimeout(identifier);
9
+ scheduledIntervalIdentifiers.delete(timerId);
10
+ return true;
12
11
  };
13
12
  export const clearScheduledTimeout = (timerId) => {
14
13
  const identifier = scheduledTimeoutIdentifiers.get(timerId);
15
- if (identifier !== undefined) {
16
- clearTimeout(identifier);
17
- scheduledTimeoutIdentifiers.delete(timerId);
18
- }
19
- else {
20
- throw new Error(`There is no timeout scheduled with the given id "${timerId}".`);
14
+ if (identifier === undefined) {
15
+ return false;
21
16
  }
17
+ clearTimeout(identifier);
18
+ scheduledTimeoutIdentifiers.delete(timerId);
19
+ return true;
22
20
  };
23
- const computeDelayAndExpectedCallbackTime = (delay, nowInMainThread) => {
24
- let now;
25
- let remainingDelay;
26
- const nowInWorker = performance.now();
27
- const elapsed = Math.max(0, nowInWorker - nowInMainThread);
28
- now = nowInWorker;
29
- remainingDelay = delay - elapsed;
21
+ const computeDelayAndExpectedCallbackTime = (delay, nowAndTimeOrigin) => {
22
+ const now = performance.now();
23
+ const remainingDelay = delay + nowAndTimeOrigin - now - performance.timeOrigin;
30
24
  const expected = now + remainingDelay;
31
25
  return { expected, remainingDelay };
32
26
  };
33
27
  const setTimeoutCallback = (identifiers, timerId, expected, timerType) => {
34
- const now = performance.now();
35
- if (now > expected) {
36
- postMessage({ id: null, method: 'call', params: { timerId, timerType } });
28
+ const remainingDelay = expected - performance.now();
29
+ if (remainingDelay > 0) {
30
+ identifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, identifiers, timerId, expected, timerType));
37
31
  }
38
32
  else {
39
- identifiers.set(timerId, setTimeout(setTimeoutCallback, expected - now, identifiers, timerId, expected, timerType));
33
+ identifiers.delete(timerId);
34
+ postMessage({ id: null, method: 'call', params: { timerId, timerType } });
40
35
  }
41
36
  };
42
- export const scheduleInterval = (delay, timerId, nowInMainThread) => {
43
- const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
37
+ export const scheduleInterval = (delay, timerId, nowAndTimeOrigin) => {
38
+ const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin);
44
39
  scheduledIntervalIdentifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, scheduledIntervalIdentifiers, timerId, expected, 'interval'));
45
40
  };
46
- export const scheduleTimeout = (delay, timerId, nowInMainThread) => {
47
- const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
41
+ export const scheduleTimeout = (delay, timerId, nowAndTimeOrigin) => {
42
+ const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin);
48
43
  scheduledTimeoutIdentifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, scheduledTimeoutIdentifiers, timerId, expected, 'timeout'));
49
44
  };
50
45
  //# sourceMappingURL=timer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"timer.js","sourceRoot":"","sources":["../../../src/helpers/timer.ts"],"names":[],"mappings":"AAEA,MAAM,4BAA4B,GAAwB,IAAI,GAAG,EAAE,CAAC;AACpE,MAAM,2BAA2B,GAAwB,IAAI,GAAG,EAAE,CAAC;AAEnE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,OAAe,EAAE,EAAE;IACtD,MAAM,UAAU,GAAG,4BAA4B,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAE7D,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC3B,YAAY,CAAC,UAAU,CAAC,CAAC;QACzB,4BAA4B,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACjD,CAAC;SAAM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,CAAC;QACJ,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC5B,WAAW,CAAoB,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;IACjG,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAE,OAAe,EAAE,gBAAwB,EAAE,EAAE;IACzF,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,mCAAmC,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;IAElG,4BAA4B,CAAC,GAAG,CAC5B,OAAO,EACP,UAAU,CAAC,kBAAkB,EAAE,cAAc,EAAE,4BAA4B,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,CAAC,CAC9G,CAAC;AACN,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,KAAa,EAAE,OAAe,EAAE,gBAAwB,EAAE,EAAE;IACxF,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,mCAAmC,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;IAElG,2BAA2B,CAAC,GAAG,CAC3B,OAAO,EACP,UAAU,CAAC,kBAAkB,EAAE,cAAc,EAAE,2BAA2B,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,CAC5G,CAAC;AACN,CAAC,CAAC"}
@@ -1,5 +1,5 @@
1
1
  export interface IClearResponse {
2
- error: null;
3
2
  id: number;
3
+ result: boolean;
4
4
  }
5
5
  //# sourceMappingURL=clear-response.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"clear-response.d.ts","sourceRoot":"","sources":["../../../src/interfaces/clear-response.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC3B,KAAK,EAAE,IAAI,CAAC;IAEZ,EAAE,EAAE,MAAM,CAAC;CACd"}
1
+ {"version":3,"file":"clear-response.d.ts","sourceRoot":"","sources":["../../../src/interfaces/clear-response.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC3B,EAAE,EAAE,MAAM,CAAC;IAEX,MAAM,EAAE,OAAO,CAAC;CACnB"}
@@ -10,12 +10,10 @@ addEventListener('message', ({ data }) => {
10
10
  if (data.method === 'clear') {
11
11
  const { id, params: { timerId, timerType } } = data;
12
12
  if (timerType === 'interval') {
13
- clearScheduledInterval(timerId);
14
- postMessage({ error: null, id });
13
+ postMessage({ id, result: clearScheduledInterval(timerId) });
15
14
  }
16
15
  else if (timerType === 'timeout') {
17
- clearScheduledTimeout(timerId);
18
- postMessage({ error: null, id });
16
+ postMessage({ id, result: clearScheduledTimeout(timerId) });
19
17
  }
20
18
  else {
21
19
  throw new Error(`The given type "${timerType}" is not supported`);
@@ -1 +1 @@
1
- {"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGnH;;;GAGG;AACH,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAE9B,gBAAgB,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,EAAgB,EAAE,EAAE;IACnD,IAAI,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC;YAC1B,MAAM,EACF,EAAE,EACF,MAAM,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,EACjC,GAAG,IAAI,CAAC;YAET,IAAI,SAAS,KAAK,UAAU,EAAE,CAAC;gBAC3B,sBAAsB,CAAC,OAAO,CAAC,CAAC;gBAEhC,WAAW,CAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;YACrD,CAAC;iBAAM,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;gBACjC,qBAAqB,CAAC,OAAO,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M,oBAAoB,CAAC,CAAC;QACjF,CAAC;IACL,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACX,WAAW,CAAsC;YAC7C,KAAK,EAAE;gBACH,OAAO,EAAE,GAAG,CAAC,OAAO;aACvB;YACD,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,MAAM,EAAE,IAAI;SACf,CAAC,CAAC;IACP,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -7,29 +7,25 @@
7
7
  var scheduledTimeoutIdentifiers = new Map();
8
8
  var clearScheduledInterval = function clearScheduledInterval(timerId) {
9
9
  var identifier = scheduledIntervalIdentifiers.get(timerId);
10
- if (identifier !== undefined) {
11
- clearTimeout(identifier);
12
- scheduledIntervalIdentifiers["delete"](timerId);
13
- } else {
14
- throw new Error("There is no interval scheduled with the given id \"".concat(timerId, "\"."));
10
+ if (identifier === undefined) {
11
+ return false;
15
12
  }
13
+ clearTimeout(identifier);
14
+ scheduledIntervalIdentifiers["delete"](timerId);
15
+ return true;
16
16
  };
17
17
  var clearScheduledTimeout = function clearScheduledTimeout(timerId) {
18
18
  var identifier = scheduledTimeoutIdentifiers.get(timerId);
19
- if (identifier !== undefined) {
20
- clearTimeout(identifier);
21
- scheduledTimeoutIdentifiers["delete"](timerId);
22
- } else {
23
- throw new Error("There is no timeout scheduled with the given id \"".concat(timerId, "\"."));
19
+ if (identifier === undefined) {
20
+ return false;
24
21
  }
22
+ clearTimeout(identifier);
23
+ scheduledTimeoutIdentifiers["delete"](timerId);
24
+ return true;
25
25
  };
26
- var computeDelayAndExpectedCallbackTime = function computeDelayAndExpectedCallbackTime(delay, nowInMainThread) {
27
- var now;
28
- var remainingDelay;
29
- var nowInWorker = performance.now();
30
- var elapsed = Math.max(0, nowInWorker - nowInMainThread);
31
- now = nowInWorker;
32
- remainingDelay = delay - elapsed;
26
+ var computeDelayAndExpectedCallbackTime = function computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin) {
27
+ var now = performance.now();
28
+ var remainingDelay = delay + nowAndTimeOrigin - now - performance.timeOrigin;
33
29
  var expected = now + remainingDelay;
34
30
  return {
35
31
  expected: expected,
@@ -37,8 +33,11 @@
37
33
  };
38
34
  };
39
35
  var setTimeoutCallback = function setTimeoutCallback(identifiers, timerId, expected, timerType) {
40
- var now = performance.now();
41
- if (now > expected) {
36
+ var remainingDelay = expected - performance.now();
37
+ if (remainingDelay > 0) {
38
+ identifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, identifiers, timerId, expected, timerType));
39
+ } else {
40
+ identifiers["delete"](timerId);
42
41
  postMessage({
43
42
  id: null,
44
43
  method: 'call',
@@ -47,18 +46,16 @@
47
46
  timerType: timerType
48
47
  }
49
48
  });
50
- } else {
51
- identifiers.set(timerId, setTimeout(setTimeoutCallback, expected - now, identifiers, timerId, expected, timerType));
52
49
  }
53
50
  };
54
- var scheduleInterval = function scheduleInterval(delay, timerId, nowInMainThread) {
55
- var _computeDelayAndExpec = computeDelayAndExpectedCallbackTime(delay, nowInMainThread),
51
+ var scheduleInterval = function scheduleInterval(delay, timerId, nowAndTimeOrigin) {
52
+ var _computeDelayAndExpec = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin),
56
53
  expected = _computeDelayAndExpec.expected,
57
54
  remainingDelay = _computeDelayAndExpec.remainingDelay;
58
55
  scheduledIntervalIdentifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, scheduledIntervalIdentifiers, timerId, expected, 'interval'));
59
56
  };
60
- var scheduleTimeout = function scheduleTimeout(delay, timerId, nowInMainThread) {
61
- var _computeDelayAndExpec2 = computeDelayAndExpectedCallbackTime(delay, nowInMainThread),
57
+ var scheduleTimeout = function scheduleTimeout(delay, timerId, nowAndTimeOrigin) {
58
+ var _computeDelayAndExpec2 = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin),
62
59
  expected = _computeDelayAndExpec2.expected,
63
60
  remainingDelay = _computeDelayAndExpec2.remainingDelay;
64
61
  scheduledTimeoutIdentifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, scheduledTimeoutIdentifiers, timerId, expected, 'timeout'));
@@ -73,16 +70,14 @@
73
70
  timerId = _data$params.timerId,
74
71
  timerType = _data$params.timerType;
75
72
  if (timerType === 'interval') {
76
- clearScheduledInterval(timerId);
77
73
  postMessage({
78
- error: null,
79
- id: id
74
+ id: id,
75
+ result: clearScheduledInterval(timerId)
80
76
  });
81
77
  } else if (timerType === 'timeout') {
82
- clearScheduledTimeout(timerId);
83
78
  postMessage({
84
- error: null,
85
- id: id
79
+ id: id,
80
+ result: clearScheduledTimeout(timerId)
86
81
  });
87
82
  } else {
88
83
  throw new Error("The given type \"".concat(timerType, "\" is not supported"));
package/package.json CHANGED
@@ -31,7 +31,7 @@
31
31
  "commitizen": "^4.3.0",
32
32
  "cz-conventional-changelog": "^3.3.0",
33
33
  "eslint": "^8.57.0",
34
- "eslint-config-holy-grail": "^59.0.8",
34
+ "eslint-config-holy-grail": "^59.0.10",
35
35
  "grunt": "^1.6.1",
36
36
  "grunt-cli": "^1.4.3",
37
37
  "grunt-sh": "^0.2.1",
@@ -46,14 +46,14 @@
46
46
  "karma-sinon-chai": "^2.0.2",
47
47
  "karma-webkit-launcher": "^2.4.0",
48
48
  "karma-webpack": "^5.0.1",
49
- "lint-staged": "^15.2.2",
49
+ "lint-staged": "^15.2.4",
50
50
  "load-grunt-config": "^4.0.1",
51
51
  "memory-fs": "^0.5.0",
52
52
  "mocha": "^10.4.0",
53
53
  "prettier": "^3.2.5",
54
- "rimraf": "^5.0.5",
54
+ "rimraf": "^5.0.7",
55
55
  "rollup": "^4.17.2",
56
- "sinon": "^17.0.1",
56
+ "sinon": "^17.0.2",
57
57
  "sinon-chai": "^3.7.0",
58
58
  "ts-loader": "^9.5.1",
59
59
  "tsconfig-holy-grail": "^15.0.1",
@@ -87,5 +87,5 @@
87
87
  "test": "grunt lint && grunt test"
88
88
  },
89
89
  "types": "build/es2019/module.d.ts",
90
- "version": "7.0.71"
90
+ "version": "8.0.0"
91
91
  }
@@ -6,51 +6,50 @@ const scheduledTimeoutIdentifiers: Map<number, number> = new Map();
6
6
  export const clearScheduledInterval = (timerId: number) => {
7
7
  const identifier = scheduledIntervalIdentifiers.get(timerId);
8
8
 
9
- if (identifier !== undefined) {
10
- clearTimeout(identifier);
11
- scheduledIntervalIdentifiers.delete(timerId);
12
- } else {
13
- throw new Error(`There is no interval scheduled with the given id "${timerId}".`);
9
+ if (identifier === undefined) {
10
+ return false;
14
11
  }
12
+
13
+ clearTimeout(identifier);
14
+ scheduledIntervalIdentifiers.delete(timerId);
15
+
16
+ return true;
15
17
  };
16
18
 
17
19
  export const clearScheduledTimeout = (timerId: number) => {
18
20
  const identifier = scheduledTimeoutIdentifiers.get(timerId);
19
21
 
20
- if (identifier !== undefined) {
21
- clearTimeout(identifier);
22
- scheduledTimeoutIdentifiers.delete(timerId);
23
- } else {
24
- throw new Error(`There is no timeout scheduled with the given id "${timerId}".`);
22
+ if (identifier === undefined) {
23
+ return false;
25
24
  }
26
- };
27
25
 
28
- const computeDelayAndExpectedCallbackTime = (delay: number, nowInMainThread: number) => {
29
- let now: number;
30
- let remainingDelay: number;
31
- const nowInWorker = performance.now();
32
- const elapsed = Math.max(0, nowInWorker - nowInMainThread);
26
+ clearTimeout(identifier);
27
+ scheduledTimeoutIdentifiers.delete(timerId);
33
28
 
34
- now = nowInWorker;
35
- remainingDelay = delay - elapsed;
29
+ return true;
30
+ };
36
31
 
32
+ const computeDelayAndExpectedCallbackTime = (delay: number, nowAndTimeOrigin: number) => {
33
+ const now = performance.now();
34
+ const remainingDelay = delay + nowAndTimeOrigin - now - performance.timeOrigin;
37
35
  const expected = now + remainingDelay;
38
36
 
39
37
  return { expected, remainingDelay };
40
38
  };
41
39
 
42
40
  const setTimeoutCallback = (identifiers: Map<number, number>, timerId: number, expected: number, timerType: string) => {
43
- const now = performance.now();
41
+ const remainingDelay = expected - performance.now();
44
42
 
45
- if (now > expected) {
46
- postMessage(<ICallNotification>{ id: null, method: 'call', params: { timerId, timerType } });
43
+ if (remainingDelay > 0) {
44
+ identifiers.set(timerId, setTimeout(setTimeoutCallback, remainingDelay, identifiers, timerId, expected, timerType));
47
45
  } else {
48
- identifiers.set(timerId, setTimeout(setTimeoutCallback, expected - now, identifiers, timerId, expected, timerType));
46
+ identifiers.delete(timerId);
47
+ postMessage(<ICallNotification>{ id: null, method: 'call', params: { timerId, timerType } });
49
48
  }
50
49
  };
51
50
 
52
- export const scheduleInterval = (delay: number, timerId: number, nowInMainThread: number) => {
53
- const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
51
+ export const scheduleInterval = (delay: number, timerId: number, nowAndTimeOrigin: number) => {
52
+ const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin);
54
53
 
55
54
  scheduledIntervalIdentifiers.set(
56
55
  timerId,
@@ -58,8 +57,8 @@ export const scheduleInterval = (delay: number, timerId: number, nowInMainThread
58
57
  );
59
58
  };
60
59
 
61
- export const scheduleTimeout = (delay: number, timerId: number, nowInMainThread: number) => {
62
- const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowInMainThread);
60
+ export const scheduleTimeout = (delay: number, timerId: number, nowAndTimeOrigin: number) => {
61
+ const { expected, remainingDelay } = computeDelayAndExpectedCallbackTime(delay, nowAndTimeOrigin);
63
62
 
64
63
  scheduledTimeoutIdentifiers.set(
65
64
  timerId,
@@ -1,5 +1,5 @@
1
1
  export interface IClearResponse {
2
- error: null;
3
-
4
2
  id: number;
3
+
4
+ result: boolean;
5
5
  }
package/src/module.ts CHANGED
@@ -17,13 +17,9 @@ addEventListener('message', ({ data }: IBrokerEvent) => {
17
17
  } = data;
18
18
 
19
19
  if (timerType === 'interval') {
20
- clearScheduledInterval(timerId);
21
-
22
- postMessage(<IClearResponse>{ error: null, id });
20
+ postMessage(<IClearResponse>{ id, result: clearScheduledInterval(timerId) });
23
21
  } else if (timerType === 'timeout') {
24
- clearScheduledTimeout(timerId);
25
-
26
- postMessage(<IClearResponse>{ error: null, id });
22
+ postMessage(<IClearResponse>{ id, result: clearScheduledTimeout(timerId) });
27
23
  } else {
28
24
  throw new Error(`The given type "${timerType}" is not supported`);
29
25
  }