MCPcopy Create free account
hub / github.com/porsager/postgres / subscribe

Function subscribe

cf/src/subscribe.js:51–73  ·  view source on GitHub ↗
(event, fn, onsubscribe = noop, onerror = noop)

Source from the content-addressed store, hash-verified

49 return subscribe
50
51 async function subscribe(event, fn, onsubscribe = noop, onerror = noop) {
52 event = parseEvent(event)
53
54 if (!connection)
55 connection = init(sql, slot, options.publications)
56
57 const subscriber = { fn, onsubscribe }
58 const fns = subscribers.has(event)
59 ? subscribers.get(event).add(subscriber)
60 : subscribers.set(event, new Set([subscriber])).get(event)
61
62 const unsubscribe = () => {
63 fns.delete(subscriber)
64 fns.size === 0 && subscribers.delete(event)
65 }
66
67 return connection.then(x => {
68 connected(x)
69 onsubscribe()
70 stream && stream.on('error', onerror)
71 return { unsubscribe, state, sql }
72 })
73 }
74
75 function connected(x) {
76 stream = x.stream

Callers

nothing calls this directly

Calls 4

parseEventFunction · 0.70
initFunction · 0.70
connectedFunction · 0.70
thenMethod · 0.45

Tested by

no test coverage detected