@webex/common-timers 3.0.0-beta.21 → 3.0.0-beta.211
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +100 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +116 -0
- package/test/unit/spec/index.ts +136 -0
- package/src/index.js +0 -37
package/dist/index.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
|
4
5
|
_Object$defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
8
|
+
exports.Timer = void 0;
|
|
7
9
|
exports.safeSetInterval = safeSetInterval;
|
|
8
10
|
exports.safeSetTimeout = safeSetTimeout;
|
|
11
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));
|
|
12
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));
|
|
13
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
|
|
9
14
|
/*!
|
|
10
15
|
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
11
16
|
*/
|
|
@@ -15,7 +20,7 @@ exports.safeSetTimeout = safeSetTimeout;
|
|
|
15
20
|
* wedging the process open unexpectedly.
|
|
16
21
|
* @param {Mixed} args
|
|
17
22
|
* @protected
|
|
18
|
-
* @returns {
|
|
23
|
+
* @returns {NodeJS.Timeout|Number}
|
|
19
24
|
*/
|
|
20
25
|
function safeSetTimeout() {
|
|
21
26
|
var timer = setTimeout.apply(void 0, arguments);
|
|
@@ -26,11 +31,11 @@ function safeSetTimeout() {
|
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
/**
|
|
29
|
-
* Wrapper around
|
|
34
|
+
* Wrapper around setInterval which (in node) unrefs the returned timer to avoid
|
|
30
35
|
* wedging the process open unexpectedly.
|
|
31
36
|
* @param {Mixed} args
|
|
32
37
|
* @protected
|
|
33
|
-
* @returns {
|
|
38
|
+
* @returns {NodeJS.Timeout|Number}
|
|
34
39
|
*/
|
|
35
40
|
function safeSetInterval() {
|
|
36
41
|
var interval = setInterval.apply(void 0, arguments);
|
|
@@ -39,4 +44,96 @@ function safeSetInterval() {
|
|
|
39
44
|
}
|
|
40
45
|
return interval;
|
|
41
46
|
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a restartable timer
|
|
50
|
+
*/
|
|
51
|
+
var Timer = /*#__PURE__*/function () {
|
|
52
|
+
/**
|
|
53
|
+
* Construct timer
|
|
54
|
+
* @param {Function} callback Function called when the timer expired
|
|
55
|
+
* @param {number} timeout duration of the timeout in milliseconds
|
|
56
|
+
*/
|
|
57
|
+
function Timer(callback, timeout) {
|
|
58
|
+
var _this = this;
|
|
59
|
+
(0, _classCallCheck2.default)(this, Timer);
|
|
60
|
+
(0, _defineProperty2.default)(this, "state", void 0);
|
|
61
|
+
(0, _defineProperty2.default)(this, "timeout", void 0);
|
|
62
|
+
(0, _defineProperty2.default)(this, "callback", void 0);
|
|
63
|
+
(0, _defineProperty2.default)(this, "currentTimer", void 0);
|
|
64
|
+
this.state = 'init';
|
|
65
|
+
this.timeout = timeout;
|
|
66
|
+
this.callback = function () {
|
|
67
|
+
_this.state = 'done';
|
|
68
|
+
callback();
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Start timer
|
|
74
|
+
* @returns {void}
|
|
75
|
+
*/
|
|
76
|
+
(0, _createClass2.default)(Timer, [{
|
|
77
|
+
key: "start",
|
|
78
|
+
value: function start() {
|
|
79
|
+
if (this.state !== 'init') {
|
|
80
|
+
throw new Error("Can't start the timer when it's in ".concat(this.state, " state"));
|
|
81
|
+
}
|
|
82
|
+
this.startTimer();
|
|
83
|
+
this.state = 'running';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Clear the current timer and start a new one
|
|
88
|
+
* @returns {void}
|
|
89
|
+
*/
|
|
90
|
+
}, {
|
|
91
|
+
key: "reset",
|
|
92
|
+
value: function reset() {
|
|
93
|
+
if (this.state !== 'running') {
|
|
94
|
+
throw new Error("Can't reset the timer when it's in ".concat(this.state, " state"));
|
|
95
|
+
}
|
|
96
|
+
this.clearTimer();
|
|
97
|
+
this.startTimer();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Clear the timer
|
|
102
|
+
* @returns {void}
|
|
103
|
+
*/
|
|
104
|
+
}, {
|
|
105
|
+
key: "cancel",
|
|
106
|
+
value: function cancel() {
|
|
107
|
+
if (this.state !== 'running') {
|
|
108
|
+
throw new Error("Can't cancel the timer when it's in ".concat(this.state, " state"));
|
|
109
|
+
}
|
|
110
|
+
this.clearTimer();
|
|
111
|
+
this.state = 'done';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Create the actual timer
|
|
116
|
+
* @private
|
|
117
|
+
* @returns {undefined}
|
|
118
|
+
*/
|
|
119
|
+
}, {
|
|
120
|
+
key: "startTimer",
|
|
121
|
+
value: function startTimer() {
|
|
122
|
+
this.currentTimer = safeSetTimeout(this.callback, this.timeout);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Clear the actual timer
|
|
127
|
+
* @private
|
|
128
|
+
* @returns {undefined}
|
|
129
|
+
*/
|
|
130
|
+
}, {
|
|
131
|
+
key: "clearTimer",
|
|
132
|
+
value: function clearTimer() {
|
|
133
|
+
clearTimeout(this.currentTimer);
|
|
134
|
+
}
|
|
135
|
+
}]);
|
|
136
|
+
return Timer;
|
|
137
|
+
}();
|
|
138
|
+
exports.Timer = Timer;
|
|
42
139
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["safeSetTimeout","timer","setTimeout","unref","safeSetInterval","interval","setInterval"],"sources":["index.
|
|
1
|
+
{"version":3,"names":["safeSetTimeout","timer","setTimeout","unref","safeSetInterval","interval","setInterval","Timer","callback","timeout","state","Error","startTimer","clearTimer","currentTimer","clearTimeout"],"sources":["index.ts"],"sourcesContent":["/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\n/**\n * Wrapper around setTimout which (in node) unrefs the returned timer to avoid\n * wedging the process open unexpectedly.\n * @param {Mixed} args\n * @protected\n * @returns {NodeJS.Timeout|Number}\n */\nexport function safeSetTimeout(...args: Parameters<typeof setTimeout>): number | NodeJS.Timeout {\n const timer = setTimeout(...args);\n\n if (timer.unref) {\n timer.unref();\n }\n\n return timer;\n}\n\n/**\n * Wrapper around setInterval which (in node) unrefs the returned timer to avoid\n * wedging the process open unexpectedly.\n * @param {Mixed} args\n * @protected\n * @returns {NodeJS.Timeout|Number}\n */\nexport function safeSetInterval(...args: Parameters<typeof setInterval>): number | NodeJS.Timeout {\n const interval = setInterval(...args);\n\n if (interval.unref) {\n interval.unref();\n }\n\n return interval;\n}\n\n/**\n * Create a restartable timer\n */\nexport class Timer {\n private state: 'init' | 'running' | 'done';\n private readonly timeout: number;\n private readonly callback: () => void;\n private currentTimer: number | NodeJS.Timeout;\n\n /**\n * Construct timer\n * @param {Function} callback Function called when the timer expired\n * @param {number} timeout duration of the timeout in milliseconds\n */\n constructor(callback: () => void, timeout: number) {\n this.state = 'init';\n this.timeout = timeout;\n this.callback = () => {\n this.state = 'done';\n callback();\n };\n }\n\n /**\n * Start timer\n * @returns {void}\n */\n start() {\n if (this.state !== 'init') {\n throw new Error(`Can't start the timer when it's in ${this.state} state`);\n }\n\n this.startTimer();\n this.state = 'running';\n }\n\n /**\n * Clear the current timer and start a new one\n * @returns {void}\n */\n reset() {\n if (this.state !== 'running') {\n throw new Error(`Can't reset the timer when it's in ${this.state} state`);\n }\n this.clearTimer();\n this.startTimer();\n }\n\n /**\n * Clear the timer\n * @returns {void}\n */\n cancel() {\n if (this.state !== 'running') {\n throw new Error(`Can't cancel the timer when it's in ${this.state} state`);\n }\n this.clearTimer();\n this.state = 'done';\n }\n\n /**\n * Create the actual timer\n * @private\n * @returns {undefined}\n */\n private startTimer() {\n this.currentTimer = safeSetTimeout(this.callback, this.timeout);\n }\n\n /**\n * Clear the actual timer\n * @private\n * @returns {undefined}\n */\n private clearTimer() {\n clearTimeout(this.currentTimer);\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAAA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,cAAc,GAAkE;EAC9F,IAAMC,KAAK,GAAGC,UAAU,yBAAS;EAEjC,IAAID,KAAK,CAACE,KAAK,EAAE;IACfF,KAAK,CAACE,KAAK,EAAE;EACf;EAEA,OAAOF,KAAK;AACd;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASG,eAAe,GAAmE;EAChG,IAAMC,QAAQ,GAAGC,WAAW,yBAAS;EAErC,IAAID,QAAQ,CAACF,KAAK,EAAE;IAClBE,QAAQ,CAACF,KAAK,EAAE;EAClB;EAEA,OAAOE,QAAQ;AACjB;;AAEA;AACA;AACA;AAFA,IAGaE,KAAK;EAMhB;AACF;AACA;AACA;AACA;EACE,eAAYC,QAAoB,EAAEC,OAAe,EAAE;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IACjD,IAAI,CAACC,KAAK,GAAG,MAAM;IACnB,IAAI,CAACD,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACD,QAAQ,GAAG,YAAM;MACpB,KAAI,CAACE,KAAK,GAAG,MAAM;MACnBF,QAAQ,EAAE;IACZ,CAAC;EACH;;EAEA;AACF;AACA;AACA;EAHE;IAAA;IAAA,OAIA,iBAAQ;MACN,IAAI,IAAI,CAACE,KAAK,KAAK,MAAM,EAAE;QACzB,MAAM,IAAIC,KAAK,8CAAuC,IAAI,CAACD,KAAK,YAAS;MAC3E;MAEA,IAAI,CAACE,UAAU,EAAE;MACjB,IAAI,CAACF,KAAK,GAAG,SAAS;IACxB;;IAEA;AACF;AACA;AACA;EAHE;IAAA;IAAA,OAIA,iBAAQ;MACN,IAAI,IAAI,CAACA,KAAK,KAAK,SAAS,EAAE;QAC5B,MAAM,IAAIC,KAAK,8CAAuC,IAAI,CAACD,KAAK,YAAS;MAC3E;MACA,IAAI,CAACG,UAAU,EAAE;MACjB,IAAI,CAACD,UAAU,EAAE;IACnB;;IAEA;AACF;AACA;AACA;EAHE;IAAA;IAAA,OAIA,kBAAS;MACP,IAAI,IAAI,CAACF,KAAK,KAAK,SAAS,EAAE;QAC5B,MAAM,IAAIC,KAAK,+CAAwC,IAAI,CAACD,KAAK,YAAS;MAC5E;MACA,IAAI,CAACG,UAAU,EAAE;MACjB,IAAI,CAACH,KAAK,GAAG,MAAM;IACrB;;IAEA;AACF;AACA;AACA;AACA;EAJE;IAAA;IAAA,OAKA,sBAAqB;MACnB,IAAI,CAACI,YAAY,GAAGd,cAAc,CAAC,IAAI,CAACQ,QAAQ,EAAE,IAAI,CAACC,OAAO,CAAC;IACjE;;IAEA;AACF;AACA;AACA;AACA;EAJE;IAAA;IAAA,OAKA,sBAAqB;MACnBM,YAAY,CAAC,IAAI,CAACD,YAAY,CAAC;IACjC;EAAC;EAAA;AAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webex/common-timers",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.211",
|
|
4
4
|
"description": "Timer wrappers to prevent wedging a process open",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
|
-
"devMain": "src/index.
|
|
7
|
+
"devMain": "src/index.ts",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/webex/webex-js-sdk.git",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wrapper around setTimout which (in node) unrefs the returned timer to avoid
|
|
7
|
+
* wedging the process open unexpectedly.
|
|
8
|
+
* @param {Mixed} args
|
|
9
|
+
* @protected
|
|
10
|
+
* @returns {NodeJS.Timeout|Number}
|
|
11
|
+
*/
|
|
12
|
+
export function safeSetTimeout(...args: Parameters<typeof setTimeout>): number | NodeJS.Timeout {
|
|
13
|
+
const timer = setTimeout(...args);
|
|
14
|
+
|
|
15
|
+
if (timer.unref) {
|
|
16
|
+
timer.unref();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return timer;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Wrapper around setInterval which (in node) unrefs the returned timer to avoid
|
|
24
|
+
* wedging the process open unexpectedly.
|
|
25
|
+
* @param {Mixed} args
|
|
26
|
+
* @protected
|
|
27
|
+
* @returns {NodeJS.Timeout|Number}
|
|
28
|
+
*/
|
|
29
|
+
export function safeSetInterval(...args: Parameters<typeof setInterval>): number | NodeJS.Timeout {
|
|
30
|
+
const interval = setInterval(...args);
|
|
31
|
+
|
|
32
|
+
if (interval.unref) {
|
|
33
|
+
interval.unref();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return interval;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a restartable timer
|
|
41
|
+
*/
|
|
42
|
+
export class Timer {
|
|
43
|
+
private state: 'init' | 'running' | 'done';
|
|
44
|
+
private readonly timeout: number;
|
|
45
|
+
private readonly callback: () => void;
|
|
46
|
+
private currentTimer: number | NodeJS.Timeout;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Construct timer
|
|
50
|
+
* @param {Function} callback Function called when the timer expired
|
|
51
|
+
* @param {number} timeout duration of the timeout in milliseconds
|
|
52
|
+
*/
|
|
53
|
+
constructor(callback: () => void, timeout: number) {
|
|
54
|
+
this.state = 'init';
|
|
55
|
+
this.timeout = timeout;
|
|
56
|
+
this.callback = () => {
|
|
57
|
+
this.state = 'done';
|
|
58
|
+
callback();
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Start timer
|
|
64
|
+
* @returns {void}
|
|
65
|
+
*/
|
|
66
|
+
start() {
|
|
67
|
+
if (this.state !== 'init') {
|
|
68
|
+
throw new Error(`Can't start the timer when it's in ${this.state} state`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.startTimer();
|
|
72
|
+
this.state = 'running';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Clear the current timer and start a new one
|
|
77
|
+
* @returns {void}
|
|
78
|
+
*/
|
|
79
|
+
reset() {
|
|
80
|
+
if (this.state !== 'running') {
|
|
81
|
+
throw new Error(`Can't reset the timer when it's in ${this.state} state`);
|
|
82
|
+
}
|
|
83
|
+
this.clearTimer();
|
|
84
|
+
this.startTimer();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Clear the timer
|
|
89
|
+
* @returns {void}
|
|
90
|
+
*/
|
|
91
|
+
cancel() {
|
|
92
|
+
if (this.state !== 'running') {
|
|
93
|
+
throw new Error(`Can't cancel the timer when it's in ${this.state} state`);
|
|
94
|
+
}
|
|
95
|
+
this.clearTimer();
|
|
96
|
+
this.state = 'done';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create the actual timer
|
|
101
|
+
* @private
|
|
102
|
+
* @returns {undefined}
|
|
103
|
+
*/
|
|
104
|
+
private startTimer() {
|
|
105
|
+
this.currentTimer = safeSetTimeout(this.callback, this.timeout);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Clear the actual timer
|
|
110
|
+
* @private
|
|
111
|
+
* @returns {undefined}
|
|
112
|
+
*/
|
|
113
|
+
private clearTimer() {
|
|
114
|
+
clearTimeout(this.currentTimer);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {assert} from '@webex/test-helper-chai';
|
|
2
|
+
import { Timer, safeSetTimeout, safeSetInterval } from "@webex/common-timers";
|
|
3
|
+
import sinon from 'sinon';
|
|
4
|
+
|
|
5
|
+
describe("commonn timers", () => {
|
|
6
|
+
let clock
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
clock = sinon.useFakeTimers();
|
|
10
|
+
})
|
|
11
|
+
after(() => {
|
|
12
|
+
clock.restore();
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe("safeSetTimeout", () => {
|
|
16
|
+
it("should call the callback when the timer expired", () => {
|
|
17
|
+
const callback = sinon.fake();
|
|
18
|
+
const timer = safeSetTimeout(callback, 1000)
|
|
19
|
+
clock.runAll()
|
|
20
|
+
assert.calledOnce(callback)
|
|
21
|
+
|
|
22
|
+
clearTimeout(timer)
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("safeSetInterval", () => {
|
|
27
|
+
it("should start in an interval", () => {
|
|
28
|
+
const callback = sinon.fake();
|
|
29
|
+
const timer = safeSetInterval(callback, 1000)
|
|
30
|
+
clock.tick(2000)
|
|
31
|
+
assert.calledTwice(callback)
|
|
32
|
+
|
|
33
|
+
clearInterval(timer)
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("Timer", () => {
|
|
38
|
+
|
|
39
|
+
describe("start method", () => {
|
|
40
|
+
it("should call the callback function when the timer expired", () => {
|
|
41
|
+
const callback = sinon.fake();
|
|
42
|
+
const timer = new Timer(callback, 1)
|
|
43
|
+
timer.start();
|
|
44
|
+
clock.runAll()
|
|
45
|
+
assert.calledOnce(callback)
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should throw error when start called more than once", () => {
|
|
49
|
+
const timer = new Timer(() => {}, 1000)
|
|
50
|
+
timer.start();
|
|
51
|
+
|
|
52
|
+
assert.throws(() => timer.start(), /Can't start the timer when it's in running state/i);
|
|
53
|
+
timer.cancel();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should throw error when start called after reset", () => {
|
|
57
|
+
const timer = new Timer(() => {}, 1000)
|
|
58
|
+
timer.start();
|
|
59
|
+
timer.reset();
|
|
60
|
+
|
|
61
|
+
assert.throws(() => timer.start(), /Can't start the timer when it's in running state/i);
|
|
62
|
+
timer.cancel();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should throw error when start called after timer canceled", () => {
|
|
66
|
+
const timer = new Timer(() => {}, 1000)
|
|
67
|
+
timer.start();
|
|
68
|
+
timer.cancel();
|
|
69
|
+
|
|
70
|
+
assert.throws(() => timer.start(), /Can't start the timer when it's in done state/i);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should throw error when start called after timer finished", () => {
|
|
74
|
+
const timer = new Timer(() => {}, 1000)
|
|
75
|
+
timer.start();
|
|
76
|
+
clock.runAll()
|
|
77
|
+
|
|
78
|
+
assert.throws(() => timer.start(), /Can't start the timer when it's in done state/i);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("reset method", () => {
|
|
83
|
+
it("should reset the timer", () => {
|
|
84
|
+
const callback = sinon.fake();
|
|
85
|
+
const timer = new Timer(callback, 1000)
|
|
86
|
+
timer.start();
|
|
87
|
+
clock.tick(500)
|
|
88
|
+
timer.reset();
|
|
89
|
+
clock.tick(500)
|
|
90
|
+
assert.notCalled(callback)
|
|
91
|
+
clock.tick(500)
|
|
92
|
+
assert.calledOnce(callback)
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should throw error when reset called before start", () => {
|
|
96
|
+
const timer = new Timer(() => {}, 1000)
|
|
97
|
+
|
|
98
|
+
assert.throws(() => timer.reset(), /Can't reset the timer when it's in init state/i);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should throw error when reset called after cancel", () => {
|
|
102
|
+
const timer = new Timer(() => {}, 1000)
|
|
103
|
+
timer.start();
|
|
104
|
+
timer.cancel();
|
|
105
|
+
|
|
106
|
+
assert.throws(() => timer.reset(), /Can't reset the timer when it's in done state/i);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe("cancel method", () => {
|
|
111
|
+
it("should stop the timer", () => {
|
|
112
|
+
const callback = sinon.fake();
|
|
113
|
+
const timer = new Timer(callback, 1)
|
|
114
|
+
timer.start();
|
|
115
|
+
timer.cancel();
|
|
116
|
+
clock.runAll()
|
|
117
|
+
assert.notCalled(callback)
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should throw error when cancel called before start", () => {
|
|
121
|
+
const timer = new Timer(() => {}, 1000)
|
|
122
|
+
|
|
123
|
+
assert.throws(() => timer.cancel(), /Can't cancel the timer when it's in init state/i);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("should throw error when cancel called more than once", () => {
|
|
127
|
+
const timer = new Timer(() => {}, 1000)
|
|
128
|
+
timer.start();
|
|
129
|
+
timer.cancel();
|
|
130
|
+
|
|
131
|
+
assert.throws(() => timer.cancel(), /Can't cancel the timer when it's in done state/i);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
package/src/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Wrapper around setTimout which (in node) unrefs the returned timer to avoid
|
|
7
|
-
* wedging the process open unexpectedly.
|
|
8
|
-
* @param {Mixed} args
|
|
9
|
-
* @protected
|
|
10
|
-
* @returns {Timer|Number}
|
|
11
|
-
*/
|
|
12
|
-
export function safeSetTimeout(...args) {
|
|
13
|
-
const timer = setTimeout(...args);
|
|
14
|
-
|
|
15
|
-
if (timer.unref) {
|
|
16
|
-
timer.unref();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return timer;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Wrapper around setTimout which (in node) unrefs the returned timer to avoid
|
|
24
|
-
* wedging the process open unexpectedly.
|
|
25
|
-
* @param {Mixed} args
|
|
26
|
-
* @protected
|
|
27
|
-
* @returns {Timer|Number}
|
|
28
|
-
*/
|
|
29
|
-
export function safeSetInterval(...args) {
|
|
30
|
-
const interval = setInterval(...args);
|
|
31
|
-
|
|
32
|
-
if (interval.unref) {
|
|
33
|
-
interval.unref();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return interval;
|
|
37
|
-
}
|