Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow changing repeating task interval and timeout #2934

Merged
merged 1 commit into from
Feb 4, 2025
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
45 changes: 45 additions & 0 deletions packages/utils/src/repeating-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,34 @@ import { anySignal } from 'any-signal'
import type { AbortOptions } from '@libp2p/interface'

export interface RepeatingTask {
/**
* Update the interval after which the next iteration of the task will run.
*
* This is useful if, for example, you want to retry a task with a short rest
* duration until it succeeds, then periodically after that.
*
* This only affects the next iteration of the task, if it is currently
* running, that run will not be interrupted.
*/
setInterval(ms: number): void

/**
* Update the amount of time a task will run before the passed abort signal
* will fire.
*
* * This only affects the next iteration of the task, if it is currently
* running, that run will not be interrupted.
*/
setTimeout(ms: number): void

/**
* Start the task running
*/
start(): void

/**
* Stop the task running
*/
stop(): void
}

Expand All @@ -16,6 +43,8 @@ export interface RepeatingTaskOptions {

/**
* Whether to schedule the task to run immediately
*
* @default false
*/
runImmediately?: boolean
}
Expand Down Expand Up @@ -54,6 +83,22 @@ export function repeatingTask (fn: (options?: AbortOptions) => void | Promise<vo
let started = false

return {
setInterval: (ms) => {
interval = ms

// maybe reschedule
if (timeout != null) {
clearTimeout(timeout)
timeout = setTimeout(runTask, interval)
}
},
setTimeout: (ms) => {
if (options == null) {
options = {}
}

options.timeout = ms
},
start: () => {
if (started) {
return
Expand Down
46 changes: 46 additions & 0 deletions packages/utils/test/repeating-task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,50 @@ describe('repeating-task', () => {

expect(count).to.be.greaterThan(1)
})

it('should update the interval of a task', async () => {
let count = 0

const task = repeatingTask(() => {
count++

if (count === 1) {
task.setInterval(2000)
}
}, 100)
task.start()

await delay(1000)

task.stop()

expect(count).to.equal(1)
})

it('should update the timeout of a task', async () => {
let count = 0

const task = repeatingTask(async (options) => {
// simulate a delay
await delay(100)

if (options?.signal?.aborted !== true) {
count++
}

if (count === 1) {
// set the task timeout to less than our simulated delay
task.setTimeout(10)
}
}, 100, {
timeout: 500
})
task.start()

await delay(1000)

task.stop()

expect(count).to.equal(1)
})
})
Loading