Skip to content

Commit

Permalink
Add updateTimeout to timeoutify (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber authored Dec 20, 2024
2 parents 620da51 + 73cff61 commit ee9c05a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-books-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zemble/utils": patch
---

Add updateTimeout to timeoutfiy
23 changes: 23 additions & 0 deletions packages/utils/node/Timeoutify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ describe('Timeoutify', () => {
expect(timeoutify.abortSignal.aborted).toBe(true)
})

test('should update timeout', async () => {
const timeoutify = new Timeoutify({ timeoutMS: 10 })

timeoutify.updateTimeout(100)

await wait(30)
expect(timeoutify.timeLeftMS).toBeGreaterThan(50)
expect(timeoutify.timeLeftMS).toBeLessThanOrEqual(70)
expect(timeoutify.status).toEqual(TimeoutifyStatus.Running)
expect(timeoutify.abortSignal.aborted).toBe(false)
})

test('should update timeout and reset startedAt', async () => {
const timeoutify = new Timeoutify({ timeoutMS: 10 })
const { startedAt } = timeoutify

await wait(10)

timeoutify.updateTimeout(100, true)

expect(timeoutify.startedAt).toBeGreaterThan(startedAt)
})

test('timeLeftMS', () => {
const timeoutify = new Timeoutify({ timeoutMS: 10 })
expect(timeoutify.timeLeftMS).toBeLessThanOrEqual(10)
Expand Down
43 changes: 34 additions & 9 deletions packages/utils/node/Timeoutify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ export enum TimeoutifyStatus {
}

export class Timeoutify {
readonly startedAt: number
// eslint-disable-next-line functional/prefer-readonly-type
#startedAt: number

get startedAt() {
return this.#startedAt
}

private readonly timeoutMS: number
// eslint-disable-next-line functional/prefer-readonly-type
#timeoutMS: number

private readonly abortController: AbortController

Expand All @@ -24,39 +30,55 @@ export class Timeoutify {

private readonly logger: Pick<Console, 'debug'>

private readonly handle: ReturnType<typeof setTimeout> | undefined
// eslint-disable-next-line functional/prefer-readonly-type
private handle: ReturnType<typeof setTimeout> | undefined

constructor({
timeoutMS,
logPrefix = LOG_PREFIX,
logger = console,
}: { readonly timeoutMS: number; readonly logPrefix?: string; readonly logger?: Pick<Console, 'debug'> }) {
this.startedAt = Date.now()
this.timeoutMS = timeoutMS
this.#startedAt = Date.now()
this.#timeoutMS = timeoutMS
this.abortController = new AbortController()
this.logPrefix = logPrefix
this.statusInternal = TimeoutifyStatus.Running
this.logger = logger

this.updateTimeout(timeoutMS, true)
}

updateTimeout(timeoutMS: number, resetStartedAt = true) {
clearTimeout(this.handle)

// eslint-disable-next-line functional/immutable-data
this.#timeoutMS = timeoutMS
if (resetStartedAt) {
// eslint-disable-next-line functional/immutable-data
this.#startedAt = Date.now()
}

if (timeoutMS > 0) {
// eslint-disable-next-line functional/immutable-data
this.handle = setTimeout(() => {
if (process.env['DEBUG']) {
this.logger.debug(`${this.logPrefix} setTimeout called`)
}

if (!this.abortController.signal.aborted) {
// eslint-disable-next-line functional/immutable-data
this.statusInternal = TimeoutifyStatus.TimedOut
this.abortController.abort()
}
}, timeoutMS)
}, timeoutMS - (Date.now() - this.#startedAt))
}
}

/**
* This is the time left in milliseconds before the timeout is reached. Used for handling MongoDB timeouts.
*/
get timeLeftMS(): number {
return Math.max(this.timeoutMS - (Date.now() - this.startedAt), 0)
return Math.max(this.#timeoutMS - (Date.now() - this.startedAt), 0)
}

/**
Expand Down Expand Up @@ -110,7 +132,7 @@ export class Timeoutify {
throw new Error(`${this.logPrefix} runMongoOpWithTimeout: AbortSignal already aborted`)
}

const isNoop = this.timeoutMS <= 0
const isNoop = this.#timeoutMS <= 0

if (isNoop || this.timeLeftMS > 0) {
return cursor.maxTimeMS(this.timeLeftMS).toArray()
Expand All @@ -133,7 +155,10 @@ export class Timeoutify {
}

static noop() {
return new Timeoutify({ timeoutMS: 0, logger: { debug: () => {} } })
return new Timeoutify({
timeoutMS: 0,
logger: { debug: () => {} },
})
}
}

Expand Down

0 comments on commit ee9c05a

Please sign in to comment.