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
1 change: 1 addition & 0 deletions deno.lock

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

47 changes: 34 additions & 13 deletions src/data-pump/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ export class FlowcoreNotifier {

private async waitWebSocket(signal?: AbortSignal) {
this.options.logger?.debug("Waiting for web socket")

const promise = new Promise<void>((resolve) => {
this.eventResolver = resolve
})

this.subject = new Subject<NotificationEvent>()
this.subject.subscribe({
next: this.onWebSocketEvent.bind(this),
error: (error: Error) => this.options.logger?.error("Notification stream error:", { error }),
error: (error: Error) => {
this.options.logger?.error("Notification stream error:", { error })
this.eventResolver?.()
},
})

this.notificationClient = new NotificationClient(
this.notificationClient = _internals.createNotificationClient(
this.subject,
this.webSocketAuth(),
{
Expand All @@ -126,17 +134,17 @@ export class FlowcoreNotifier {
},
)

await this.notificationClient.connect()

const promise = new Promise<void>((resolve) => {
this.eventResolver = resolve
})
clearTimeout(this.timer)
this.timer = setTimeout(() => this.eventResolver?.(), this.options.timeoutMs ?? DEFAULT_TIMEOUT_MS)
signal?.addEventListener("abort", () => this.eventResolver?.())
await promise
this.eventResolver = undefined
this.notificationClient.disconnect()
try {
await this.notificationClient.connect()
clearTimeout(this.timer)
this.timer = setTimeout(() => this.eventResolver?.(), this.options.timeoutMs ?? DEFAULT_TIMEOUT_MS)
signal?.addEventListener("abort", () => this.eventResolver?.())
await promise
} finally {
clearTimeout(this.timer)
this.eventResolver = undefined
this.notificationClient.disconnect()
}
}

private webSocketAuth() {
Expand All @@ -157,3 +165,16 @@ export class FlowcoreNotifier {
}
}
}

/**
* Test seam for stubbing the NotificationClient constructor. Production code
* routes through `_internals.createNotificationClient`; tests replace this
* property with a fake factory to drive the subject manually. Not part of the
* public API.
*/
// deno-lint-ignore no-explicit-any
type NotificationClientFactory = (...args: any[]) => NotificationClient
export const _internals: { createNotificationClient: NotificationClientFactory } = {
createNotificationClient: (...args) =>
new (NotificationClient as new (...a: unknown[]) => NotificationClient)(...args),
}
Loading
Loading