Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recursive-timeout",
"version": "0.0.6",
"version": "0.0.7",
"author": "Dmytro Parzhytskyi <parzhitsky@gmail.com> (https://parzh.com)",
"description": "A simple solution to a classic problem: setInterval implemented as recursive setTimeout calls.",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/with-callback/clear-recursive-timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe(clearRecursiveTimeout, () => {
})

it('should clear the scheduled timeout', async () => {
const timeout = createRecursiveTimeout(callback, 100)
const recursive = createRecursiveTimeout(callback, 100)

clearRecursiveTimeout(timeout)
clearRecursiveTimeout(recursive)

await delay(150)

Expand Down
4 changes: 2 additions & 2 deletions src/with-callback/clear-recursive-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RecursiveTimeout, ArgsShape } from './recursive-timeout'

export function clearRecursiveTimeout(timeout: RecursiveTimeout<ArgsShape>): void {
timeout.clear()
export function clearRecursiveTimeout(recursive: RecursiveTimeout<ArgsShape>): void {
recursive.clear()
}
10 changes: 5 additions & 5 deletions src/with-callback/create-recursive-timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { RecursiveTimeout } from './recursive-timeout'

describe(createRecursiveTimeout, () => {
it('should create an instance of RecursiveTimeout class', () => {
const timeout = createRecursiveTimeout(() => {}, 10_000)
const recursive = createRecursiveTimeout(() => {}, 10_000)

expect(timeout).toBeInstanceOf(RecursiveTimeout)
expect(recursive).toBeInstanceOf(RecursiveTimeout)

timeout.clear()
recursive.clear()
})

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

const timeout = createRecursiveTimeout(longJob, 50)
const recursive = createRecursiveTimeout(longJob, 50)

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

timeout.clear()
recursive.clear()

expect(longJob).toHaveBeenCalledTimes(2)

Expand Down
36 changes: 34 additions & 2 deletions src/with-callback/recursive-timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,44 @@ describe(RecursiveTimeout, () => {
['clearInterval', clearInterval],
['clearTimeout', clearTimeout],
])('should be cancellable with %s', async (clearName, clear) => {
const timeout = createRecursiveTimeout(callback, 100)
const recursive = createRecursiveTimeout(callback, 100)

clear(timeout)
clear(recursive)

await delay(150)

expect(callback).not.toHaveBeenCalled()
})

it('should allow delaying the call by refreshing the timeout instance', async () => {
const recursive = createRecursiveTimeout(callback, 200)

for (let i = 0; i < 3; i += 1) {
await delay(100)

recursive.refresh()
}

expect(callback).not.toHaveBeenCalled()

await delay(200 + 10)

clearTimeout(recursive)

expect(callback).toHaveBeenCalledTimes(1)
})

it('should show whether it is ref-ed or not', () => {
const recursive = createRecursiveTimeout(callback, 100)

expect(recursive.hasRef()).toBe(true)

for (const [methodName, hasRefExpected] of [['unref', false], ['ref', true]] as const) {
recursive[methodName]()

expect(recursive.hasRef()).toBe(hasRefExpected)
}

recursive.unref() // let it all go
})
})
1 change: 1 addition & 0 deletions src/with-callback/recursive-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class RecursiveTimeout<Args extends ArgsShape> implements NodeJS.Timeout
// we're naming the tick function the same as the original callback
[this.callback.name]: () => {
this.callback(...this.args)
this.clear()
this.setTimer(tick)
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/with-promises/clear-recursive-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RecursiveTimeout, ArgsShape } from './recursive-timeout'

export function clearRecursiveTimeout(timeout: RecursiveTimeout<ArgsShape>): void {
timeout.clear()
export function clearRecursiveTimeout(recursive: RecursiveTimeout<ArgsShape>): void {
recursive.clear()
}