Skip to content

Commit

Permalink
chore: Expose function to sleep or abort on AbortSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody committed Jul 29, 2024
1 parent 985d76e commit b21b7e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
21 changes: 5 additions & 16 deletions packages/base-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { CID } from 'multiformats/cid'
import { randomCID, StreamID } from '@ceramicnetwork/streamid'

const delay = async function (ms: number) {
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms))
}

export class BaseTestUtils {
static randomCID(version: 0 | 1 = 1, codec = 0x71, hasher = 0x12): CID {
return randomCID(version, codec, hasher)
Expand Down Expand Up @@ -32,7 +36,7 @@ export class BaseTestUtils {
const deadline = new Date(startTime.getTime() + timeoutMs)
let now = startTime
while (now < deadline) {
await BaseTestUtils.delay(100)
await delay(100)
now = new Date()

try {
Expand All @@ -53,19 +57,4 @@ export class BaseTestUtils {
const customMsg = typeof errMsgGenerator == 'string' ? errMsgGenerator : errMsgGenerator()
throw new Error(baseErrMsg + ': ' + customMsg)
}

static async delay(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => resolve(), ms)
if (signal) {
const handleAbort = () => {
clearTimeout(timeout)
signal.removeEventListener('abort', handleAbort)
reject(signal.reason)
}
if (signal.aborted) handleAbort()
signal.addEventListener('abort', handleAbort)
}
})
}
}
4 changes: 3 additions & 1 deletion packages/common-test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { StreamState, Stream } from '@ceramicnetwork/common'
import {
AdminApi,
AnchorStatus,
delayOrAbort,
EnvironmentUtils,
EventType,
RunningStateLike,
Expand Down Expand Up @@ -45,8 +46,9 @@ export class CommonTestUtils {
}

static async delay(ms: number, signal?: AbortSignal): Promise<void> {
return BaseTestUtils.delay(ms, signal)
return delayOrAbort(ms, signal)
}

/**
* Given a stream and a predicate that operates on the stream state, continuously waits for
* changes to the stream until the predicate returns true.
Expand Down
15 changes: 15 additions & 0 deletions packages/common/src/utils/abort-signal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ export async function abortable<T>(
original.removeEventListener('abort', onAbort)
})
}

export async function delayOrAbort(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => resolve(), ms)
if (signal) {
const handleAbort = () => {
clearTimeout(timeout)
signal.removeEventListener('abort', handleAbort)
reject(signal.reason)
}
if (signal.aborted) handleAbort()
signal.addEventListener('abort', handleAbort)
}
})
}

0 comments on commit b21b7e8

Please sign in to comment.