-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
import { WebSocketEventType, WebSocketLike } from "@lophus/lib/websockets"; | ||
import { AnyEventTypeRecord } from "@lophus/core/nodes"; | ||
import { EventType, Node, NodeEvent } from "@lophus/core/nodes"; | ||
import type { InterNodeMessage } from "@lophus/core/protocol"; | ||
|
||
interface WatchChainable< | ||
interface WatchNodeChainable< | ||
R extends AnyEventTypeRecord, | ||
> { | ||
<T extends EventType<R>>(...events: T[]): ReadableStream<NodeEvent<R, T>>; | ||
} | ||
|
||
interface WatchWebSocketChainable { | ||
<T extends WebSocketEventType>( | ||
...events: T[] | ||
): ReadableStream<WebSocketEventMap[T]>; | ||
} | ||
|
||
export function watch<R extends AnyEventTypeRecord>( | ||
...nodes: Node<InterNodeMessage, R>[] | ||
): WatchChainable<R> { | ||
): WatchNodeChainable<R>; | ||
|
||
export function watch( | ||
...wss: WebSocketLike[] | ||
): WatchWebSocketChainable; | ||
|
||
export function watch<R extends AnyEventTypeRecord>( | ||
...targets: Node<InterNodeMessage, R>[] | WebSocketLike[] | ||
): WatchNodeChainable<R> | WatchWebSocketChainable { | ||
const aborter = new AbortController(); | ||
return <T extends EventType<R>>(...events: T[]) => | ||
new ReadableStream<NodeEvent<R, T>>({ | ||
start(controller) { | ||
nodes.forEach((node) => | ||
events.forEach((type) => | ||
node.addEventListener(type, (event) => { | ||
// De-prioritize to regular listeners | ||
queueMicrotask(() => controller.enqueue(event)); | ||
}, { signal: aborter.signal }) | ||
) | ||
); | ||
}, | ||
cancel() { | ||
aborter.abort(); | ||
return <T extends EventType<R> | WebSocketEventType>( | ||
...events: T[] | ||
) => { | ||
return new ReadableStream( | ||
{ | ||
start(controller) { | ||
targets.forEach((target) => | ||
events.forEach((type) => | ||
// @ts-ignore we do not type this strictly for readability | ||
target.addEventListener(type, (event) => { | ||
// de-prioritize to regular listeners | ||
queueMicrotask(() => controller.enqueue(event)); | ||
}, { signal: aborter.signal }) | ||
) | ||
); | ||
}, | ||
cancel() { | ||
aborter.abort(); | ||
}, | ||
}, | ||
}); | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters