MCPcopy Create free account
hub / github.com/IgorWarzocha/howcode / enqueueHostThreadUpdate

Function enqueueHostThreadUpdate

desktop/pi-desktop-runtime.ts:124–154  ·  view source on GitHub ↗
(
  event: Extract<DesktopEvent, { type: 'thread-update' }>,
  listener: (event: DesktopEvent) => void,
)

Source from the content-addressed store, hash-verified

122const threadUpdateForwardingBySession = new Map<string, Promise<void>>()
123
124function enqueueHostThreadUpdate(
125 event: Extract<DesktopEvent, { type: 'thread-update' }>,
126 listener: (event: DesktopEvent) => void,
127) {
128 const sessionPath = event.sessionPath
129 const previous = threadUpdateForwardingBySession.get(sessionPath) ?? Promise.resolve()
130 const next = previous
131 .catch(() => {
132 // Keep the per-session queue moving after an earlier failed update.
133 })
134 .then(async () => {
135 markInternalThreadUpdate(sessionPath)
136 rememberLiveThread(sessionPath, event.thread)
137 try {
138 await persistHostThreadUpdate(event)
139 } catch (error) {
140 console.warn(`Failed to persist Pi runtime host thread update: ${sessionPath}`, error)
141 }
142 listener({ ...event })
143 })
144 .catch((error) => {
145 console.warn(`Failed to forward Pi runtime host thread update: ${sessionPath}`, error)
146 })
147 .finally(() => {
148 if (threadUpdateForwardingBySession.get(sessionPath) === next) {
149 threadUpdateForwardingBySession.delete(sessionPath)
150 }
151 })
152
153 threadUpdateForwardingBySession.set(sessionPath, next)
154}
155
156export function subscribeDesktopEvents(listener: (event: DesktopEvent) => void) {
157 const unsubscribeLocal = subscribeLocalDesktopEvents(listener)

Callers 1

subscribeDesktopEventsFunction · 0.85

Calls 4

markInternalThreadUpdateFunction · 0.90
rememberLiveThreadFunction · 0.90
persistHostThreadUpdateFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected