MCPcopy Create free account
hub / github.com/electric-sql/electric / matchStream

Function matchStream

examples/tanstack/src/match-stream.ts:8–49  ·  view source on GitHub ↗
({
  stream,
  operations,
  matchFn,
  timeout = 10000,
}: {
  stream: ShapeStream<T>
  operations: Array<`insert` | `update` | `delete`>
  matchFn: ({
    operationType,
    message,
  }: {
    operationType: string
    message: ChangeMessage<T>
  }) => boolean
  timeout?: number
})

Source from the content-addressed store, hash-verified

6} from "@electric-sql/client"
7
8export async function matchStream<T extends Row<unknown>>({
9 stream,
10 operations,
11 matchFn,
12 timeout = 10000,
13}: {
14 stream: ShapeStream<T>
15 operations: Array<`insert` | `update` | `delete`>
16 matchFn: ({
17 operationType,
18 message,
19 }: {
20 operationType: string
21 message: ChangeMessage<T>
22 }) => boolean
23 timeout?: number
24}): Promise<ChangeMessage<T>> {
25 return new Promise<ChangeMessage<T>>((resolve, reject) => {
26 const unsubscribe = stream.subscribe((messages) => {
27 const message = messages.filter(isChangeMessage).find(
28 (message) =>
29 operations.includes(message.headers.operation) &&
30 matchFn({
31 operationType: message.headers.operation,
32 message: message,
33 })
34 )
35 if (message) return finish(message)
36 })
37
38 const timeoutId = setTimeout(() => {
39 console.error(`matchStream timed out after ${timeout}ms`)
40 reject(`matchStream timed out after ${timeout}ms`)
41 }, timeout)
42
43 function finish(message: ChangeMessage<T>) {
44 clearTimeout(timeoutId)
45 unsubscribe()
46 return resolve(message)
47 }
48 })
49}

Callers 2

createItemFunction · 0.90
clearItemsFunction · 0.90

Calls 6

setTimeoutFunction · 0.85
findMethod · 0.80
filterMethod · 0.80
finishFunction · 0.70
subscribeMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected