@util.js/node-timers 0.41.5 → 0.41.7

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/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "author": "Chris Topher <c@creemama.com>",
3
3
  "dependencies": {
4
- "@util.js/privates": "0.41.5",
4
+ "@util.js/privates": "0.41.7",
5
5
  "cron": "4.4.0"
6
6
  },
7
7
  "description": "An object to hold JavaScript timers: https://nodejs.org/api/timers.html",
8
+ "files": [
9
+ "index.js",
10
+ "lib"
11
+ ],
8
12
  "keywords": [
9
13
  "throttle",
10
14
  "timer",
@@ -26,5 +30,5 @@
26
30
  "type": "git",
27
31
  "url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-node-timers"
28
32
  },
29
- "version": "0.41.5"
33
+ "version": "0.41.7"
30
34
  }
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- if (process.argv.length < 3) {
4
- for (let i = 0; i < 1; i++) runExample(i);
5
- } else {
6
- runExample(parseInt(process.argv[2]));
7
- }
8
-
9
- function runExample(i) {
10
- switch (i) {
11
- case 0:
12
- runExample0();
13
- break;
14
- }
15
- }
16
-
17
- function runExample0() {
18
- const timers = require("..");
19
- let lastTime = Date.now();
20
- function call(str) {
21
- console.log(str + ": " + (Date.now() - lastTime));
22
- lastTime = Date.now();
23
- }
24
- const throttledCall = timers.throttle(call, 2000);
25
- throttledCall("a"); // should output "a: ~1".
26
- throttledCall("b"); // should output "b: ~2000".
27
- throttledCall("c"); // should output "c: ~2000".
28
- }
@@ -1,228 +0,0 @@
1
- "use strict";
2
-
3
- const { assert, expect } = require("chai");
4
- const timers = require("..");
5
-
6
- describe("Timers#setImmediate(callback[, ...args])", () => {
7
- it("should notify a callback", (done) => {
8
- timers.setImmediate((value) => {
9
- try {
10
- expect(value).to.eql("foobar");
11
- done();
12
- } catch (error) {
13
- done(error);
14
- }
15
- }, "foobar");
16
- });
17
- });
18
-
19
- describe("Timers#setImmediatePromise([...args])", () => {
20
- it("should resolve a Promise", async function () {
21
- const value = await timers.setImmediatePromise("foobar");
22
- expect(value).to.eql("foobar");
23
- });
24
- });
25
-
26
- describe("Timers#setInterval(callback, delay[, ...args]) and Timers#clearInterval(timeout)", () => {
27
- it("should notify a callback several times and clear the timeout", (done) => {
28
- let numCalls = 0;
29
- const timeout = timers.setInterval(
30
- (value) => {
31
- if (++numCalls === 3) {
32
- timers.clearInterval(timeout);
33
- timers.setTimeout(() => {
34
- try {
35
- expect(numCalls).to.eql(3);
36
- done();
37
- } catch (error) {
38
- done(error);
39
- }
40
- }, 75);
41
- }
42
- try {
43
- expect(value).to.eql("foobar");
44
- } catch (error) {
45
- timers.clearInterval(timeout);
46
- done(error);
47
- }
48
- },
49
- 25,
50
- "foobar",
51
- );
52
- });
53
- });
54
-
55
- describe("Timers#setTimeout(callback, delay[, ...args])", () => {
56
- it("should notify a callback", (done) => {
57
- timers.setTimeout(
58
- (value) => {
59
- try {
60
- expect(value).to.eql("foobar");
61
- done();
62
- } catch (error) {
63
- done(error);
64
- }
65
- },
66
- 25,
67
- "foobar",
68
- );
69
- });
70
- });
71
-
72
- describe("Timers#setTimeoutPromise(delay[, ...args])", () => {
73
- it("should notify a callback", async function () {
74
- const value = await timers.setTimeoutPromise(25, "foobar");
75
- expect(value).to.eql("foobar");
76
- });
77
- });
78
-
79
- describe("Timers#clearImmediate(immediate)", () => {
80
- it("should clear an immediate", (done) => {
81
- let numCalls = 0;
82
- const immediate = timers.setImmediate(() => {
83
- ++numCalls;
84
- done(
85
- new Error(
86
- "We did not expect execution to notify the immediate callback.",
87
- ),
88
- );
89
- });
90
- require("fs").readFile(__filename, "utf8", (string) => {
91
- try {
92
- expect(numCalls).to.eql(0);
93
- if (numCalls === 0) done();
94
- } catch (e) {
95
- if (numCalls === 0)
96
- // We should never get here.
97
- // done should have already been called.
98
- done(e);
99
- }
100
- });
101
- timers.clearImmediate(immediate);
102
- });
103
- });
104
-
105
- describe("Timers#clearTimeout(timeout)", () => {
106
- it("should clear a timeout", (done) => {
107
- let numCalls = 0;
108
- timers.setTimeout(() => {
109
- try {
110
- expect(numCalls).to.eql(0);
111
- if (numCalls === 0) done();
112
- } catch (e) {
113
- if (numCalls === 0)
114
- // We should never get here.
115
- // done should have already been called.
116
- done(e);
117
- }
118
- }, 30);
119
- const timeout = timers.setTimeout(
120
- (value) => {
121
- ++numCalls;
122
- done(
123
- new Error(
124
- "We did not expect execution to notify the timeout callback.",
125
- ),
126
- );
127
- },
128
- 10,
129
- "foobar",
130
- );
131
- timers.clearTimeout(timeout);
132
- });
133
- });
134
-
135
- describe("Timers#schedule", () => {
136
- it("should schedule a job at a certain time", function (done) {
137
- this.timeout(5000);
138
- timers.schedule(new Date(Date.now() + 1000), done).start();
139
- });
140
- it("should schedule a regularly recurring job", function (done) {
141
- this.timeout(5000);
142
- let count = 0;
143
- timers
144
- .schedule("* * * * * *", function () {
145
- count++;
146
- if (count > 3) {
147
- this.stop();
148
- done();
149
- }
150
- })
151
- .start();
152
- });
153
- });
154
-
155
- describe("Times#throttle(func, limit)", () => {
156
- async function testThrottle(limitInSeconds, n) {
157
- const internalPromises = [];
158
- const expectedInternalResults = [];
159
-
160
- function func(number) {
161
- if (number <= n) {
162
- for (let i = 1; i <= n - number; i++) {
163
- const derivedNum = number * 10 + i;
164
- internalPromises.push(throttledFunction(derivedNum));
165
- expectedInternalResults.push("result " + derivedNum);
166
- }
167
- }
168
- if (number === 2) throw new Error("error " + 2);
169
- return "result " + number;
170
- }
171
-
172
- const throttledFunction = timers.throttle(func, limitInSeconds);
173
-
174
- const initialPromises = [];
175
- const expectedResult = [];
176
- let errorPromise;
177
- for (let i = 1; i <= n; i++) {
178
- if (i !== 2) {
179
- initialPromises.push(throttledFunction(i));
180
- expectedResult.push("result " + i);
181
- } else {
182
- errorPromise = throttledFunction(i);
183
- }
184
- }
185
-
186
- let errorPassed = false;
187
- try {
188
- await errorPromise;
189
- errorPassed = true;
190
- } catch (e) {
191
- expect(e.message).to.eql("error 2");
192
- }
193
- if (errorPassed) assert.fail("We expected error 2.");
194
-
195
- const results = await Promise.all(initialPromises);
196
- expect(results).to.eql(expectedResult);
197
-
198
- const internalResults = await Promise.all(internalPromises);
199
- expect(internalResults).to.eql(expectedInternalResults);
200
- }
201
-
202
- it("should throttle a function", async function () {
203
- this.timeout(7000);
204
- await testThrottle(0, 10);
205
- await testThrottle(400, 4);
206
- });
207
-
208
- it("should correctly handle bad input", async function () {
209
- expect(() => timers.throttle(true, 5)).to.throw(TypeError);
210
- expect(() => timers.throttle(null, 5)).to.throw(TypeError);
211
- expect(() => timers.throttle(undefined, 5)).to.throw(TypeError);
212
- expect(() => timers.throttle(5, 5)).to.throw(TypeError);
213
- expect(() => timers.throttle("foo", 5)).to.throw(TypeError);
214
- expect(() => timers.throttle(Symbol("foo"), 5)).to.throw(TypeError);
215
-
216
- // The limitInMilliseconds is 1 in this case.
217
- expect(await timers.throttle(() => "foo", true)()).to.eql("foo");
218
-
219
- // The limitInMilliseconds is 0 in these cases.
220
- expect(await timers.throttle(() => "foo", null)()).to.eql("foo");
221
- expect(await timers.throttle(() => "foo")()).to.eql("foo");
222
- expect(await timers.throttle(() => "foo", "foo")()).to.eql("foo");
223
-
224
- expect(() => timers.throttle(() => "foo", Symbol("foo"))).to.throw(
225
- TypeError,
226
- );
227
- });
228
- });