(
event: Extract<DesktopEvent, { type: 'thread-update' }>,
listener: (event: DesktopEvent) => void,
)
| 122 | const threadUpdateForwardingBySession = new Map<string, Promise<void>>() |
| 123 | |
| 124 | function 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 | |
| 156 | export function subscribeDesktopEvents(listener: (event: DesktopEvent) => void) { |
| 157 | const unsubscribeLocal = subscribeLocalDesktopEvents(listener) |
no test coverage detected