Skip to content

Commit 6dccb3e

Browse files
authored
Merge pull request #5 from parzh/chore/add-test-for-refresh
Improve tests
2 parents 35954d3 + 7540b06 commit 6dccb3e

8 files changed

Lines changed: 49 additions & 16 deletions

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "recursive-timeout",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"author": "Dmytro Parzhytskyi <parzhitsky@gmail.com> (https://parzh.com)",
55
"description": "A simple solution to a classic problem: setInterval implemented as recursive setTimeout calls.",
66
"license": "MIT",

src/with-callback/clear-recursive-timeout.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ describe(clearRecursiveTimeout, () => {
1010
})
1111

1212
it('should clear the scheduled timeout', async () => {
13-
const timeout = createRecursiveTimeout(callback, 100)
13+
const recursive = createRecursiveTimeout(callback, 100)
1414

15-
clearRecursiveTimeout(timeout)
15+
clearRecursiveTimeout(recursive)
1616

1717
await delay(150)
1818

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RecursiveTimeout, ArgsShape } from './recursive-timeout'
22

3-
export function clearRecursiveTimeout(timeout: RecursiveTimeout<ArgsShape>): void {
4-
timeout.clear()
3+
export function clearRecursiveTimeout(recursive: RecursiveTimeout<ArgsShape>): void {
4+
recursive.clear()
55
}

src/with-callback/create-recursive-timeout.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { RecursiveTimeout } from './recursive-timeout'
44

55
describe(createRecursiveTimeout, () => {
66
it('should create an instance of RecursiveTimeout class', () => {
7-
const timeout = createRecursiveTimeout(() => {}, 10_000)
7+
const recursive = createRecursiveTimeout(() => {}, 10_000)
88

9-
expect(timeout).toBeInstanceOf(RecursiveTimeout)
9+
expect(recursive).toBeInstanceOf(RecursiveTimeout)
1010

11-
timeout.clear()
11+
recursive.clear()
1212
})
1313

1414
it('should schedule next call only after the current call is finished', async () => {
@@ -21,11 +21,11 @@ describe(createRecursiveTimeout, () => {
2121
return Date.now()
2222
})
2323

24-
const timeout = createRecursiveTimeout(longJob, 50)
24+
const recursive = createRecursiveTimeout(longJob, 50)
2525

2626
await delay(300) // I can't use fake timers, unfortunately
2727

28-
timeout.clear()
28+
recursive.clear()
2929

3030
expect(longJob).toHaveBeenCalledTimes(2)
3131

src/with-callback/recursive-timeout.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,44 @@ describe(RecursiveTimeout, () => {
1313
['clearInterval', clearInterval],
1414
['clearTimeout', clearTimeout],
1515
])('should be cancellable with %s', async (clearName, clear) => {
16-
const timeout = createRecursiveTimeout(callback, 100)
16+
const recursive = createRecursiveTimeout(callback, 100)
1717

18-
clear(timeout)
18+
clear(recursive)
1919

2020
await delay(150)
2121

2222
expect(callback).not.toHaveBeenCalled()
2323
})
24+
25+
it('should allow delaying the call by refreshing the timeout instance', async () => {
26+
const recursive = createRecursiveTimeout(callback, 200)
27+
28+
for (let i = 0; i < 3; i += 1) {
29+
await delay(100)
30+
31+
recursive.refresh()
32+
}
33+
34+
expect(callback).not.toHaveBeenCalled()
35+
36+
await delay(200 + 10)
37+
38+
clearTimeout(recursive)
39+
40+
expect(callback).toHaveBeenCalledTimes(1)
41+
})
42+
43+
it('should show whether it is ref-ed or not', () => {
44+
const recursive = createRecursiveTimeout(callback, 100)
45+
46+
expect(recursive.hasRef()).toBe(true)
47+
48+
for (const [methodName, hasRefExpected] of [['unref', false], ['ref', true]] as const) {
49+
recursive[methodName]()
50+
51+
expect(recursive.hasRef()).toBe(hasRefExpected)
52+
}
53+
54+
recursive.unref() // let it all go
55+
})
2456
})

src/with-callback/recursive-timeout.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class RecursiveTimeout<Args extends ArgsShape> implements NodeJS.Timeout
2929
// we're naming the tick function the same as the original callback
3030
[this.callback.name]: () => {
3131
this.callback(...this.args)
32+
this.clear()
3233
this.setTimer(tick)
3334
},
3435
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RecursiveTimeout, ArgsShape } from './recursive-timeout'
22

3-
export function clearRecursiveTimeout(timeout: RecursiveTimeout<ArgsShape>): void {
4-
timeout.clear()
3+
export function clearRecursiveTimeout(recursive: RecursiveTimeout<ArgsShape>): void {
4+
recursive.clear()
55
}

0 commit comments

Comments
 (0)