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

Function subscribe

cjs/src/subscribe.js:50–72  ·  view source on GitHub ↗
(event, fn, onsubscribe = noop, onerror = noop)

Source from the content-addressed store, hash-verified

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