fix(daemon): split router subscription fanout (#11490)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import type { PtyBackgroundStreamEvent } from '../providers/types'
|
||||
import { combineUnsubscribes } from './combine-unsubscribes'
|
||||
import type { DaemonPtyAdapter } from './daemon-pty-adapter'
|
||||
import type { DaemonPtyRouterDataEvent, DaemonPtyRouterExitEvent } from './daemon-pty-router-events'
|
||||
|
||||
export class DaemonPtyAdapterSubscriptionFanout {
|
||||
private unsubscribers: (() => void)[] = []
|
||||
private dataListeners: ((payload: DaemonPtyRouterDataEvent) => void)[] = []
|
||||
private exitListeners: ((payload: DaemonPtyRouterExitEvent) => void)[] = []
|
||||
|
||||
constructor(
|
||||
private readonly adapters: readonly DaemonPtyAdapter[],
|
||||
onAdapterExit: (id: string) => void
|
||||
) {
|
||||
for (const adapter of adapters) {
|
||||
this.unsubscribers.push(
|
||||
adapter.onData((payload) => {
|
||||
for (const listener of this.dataListeners) {
|
||||
listener(payload)
|
||||
}
|
||||
}),
|
||||
adapter.onExit((payload) => {
|
||||
onAdapterExit(payload.id)
|
||||
for (const listener of this.exitListeners) {
|
||||
listener(payload)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
onData(callback: (payload: DaemonPtyRouterDataEvent) => void): () => void {
|
||||
this.dataListeners.push(callback)
|
||||
return () => {
|
||||
const index = this.dataListeners.indexOf(callback)
|
||||
if (index !== -1) {
|
||||
this.dataListeners.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onBackgroundStreamEvent(callback: (payload: PtyBackgroundStreamEvent) => void): () => void {
|
||||
return combineUnsubscribes(
|
||||
this.adapters.map((adapter) => adapter.onBackgroundStreamEvent(callback))
|
||||
)
|
||||
}
|
||||
|
||||
// Why: main subscribes on the routed provider, so without this the dead-endpoint
|
||||
// fan-out never reaches the renderer and only the written pane recovers (STA-2373).
|
||||
onWriteUnavailable(callback: (payload: { id: string }) => void): () => void {
|
||||
return combineUnsubscribes(this.adapters.map((adapter) => adapter.onWriteUnavailable(callback)))
|
||||
}
|
||||
|
||||
onReplay(_callback: (payload: { id: string; data: string }) => void): () => void {
|
||||
return () => {}
|
||||
}
|
||||
|
||||
onExit(callback: (payload: DaemonPtyRouterExitEvent) => void): () => void {
|
||||
this.exitListeners.push(callback)
|
||||
return () => {
|
||||
const index = this.exitListeners.indexOf(callback)
|
||||
if (index !== -1) {
|
||||
this.exitListeners.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const unsubscribe of this.unsubscribers.splice(0)) {
|
||||
unsubscribe()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { DaemonPtyAdapter } from './daemon-pty-adapter'
|
||||
import { combineUnsubscribes } from './combine-unsubscribes'
|
||||
import { DaemonPtyAdapterSubscriptionFanout } from './daemon-pty-adapter-subscription-fanout'
|
||||
import type {
|
||||
IPtyProvider,
|
||||
PtyBackgroundStreamEvent,
|
||||
@@ -17,29 +17,14 @@ export class DaemonPtyRouter implements IPtyProvider {
|
||||
private current: DaemonPtyAdapter
|
||||
private legacy: DaemonPtyAdapter[]
|
||||
private sessionAdapters = new Map<string, DaemonPtyAdapter>()
|
||||
private unsubscribers: (() => void)[] = []
|
||||
private dataListeners: ((payload: DaemonPtyRouterDataEvent) => void)[] = []
|
||||
private exitListeners: ((payload: DaemonPtyRouterExitEvent) => void)[] = []
|
||||
private readonly subscriptions: DaemonPtyAdapterSubscriptionFanout
|
||||
|
||||
constructor(opts: { current: DaemonPtyAdapter; legacy: DaemonPtyAdapter[] }) {
|
||||
this.current = opts.current
|
||||
this.legacy = opts.legacy
|
||||
|
||||
for (const adapter of this.allAdapters()) {
|
||||
this.unsubscribers.push(
|
||||
adapter.onData((payload) => {
|
||||
for (const listener of this.dataListeners) {
|
||||
listener(payload)
|
||||
}
|
||||
}),
|
||||
adapter.onExit((payload) => {
|
||||
this.sessionAdapters.delete(payload.id)
|
||||
for (const listener of this.exitListeners) {
|
||||
listener(payload)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
this.subscriptions = new DaemonPtyAdapterSubscriptionFanout(this.allAdapters(), (id) => {
|
||||
this.sessionAdapters.delete(id)
|
||||
})
|
||||
}
|
||||
|
||||
async discoverLegacySessions(): Promise<void> {
|
||||
@@ -222,41 +207,23 @@ export class DaemonPtyRouter implements IPtyProvider {
|
||||
}
|
||||
|
||||
onData(callback: (payload: DaemonPtyRouterDataEvent) => void): () => void {
|
||||
this.dataListeners.push(callback)
|
||||
return () => {
|
||||
const idx = this.dataListeners.indexOf(callback)
|
||||
if (idx !== -1) {
|
||||
this.dataListeners.splice(idx, 1)
|
||||
}
|
||||
}
|
||||
return this.subscriptions.onData(callback)
|
||||
}
|
||||
|
||||
onBackgroundStreamEvent(callback: (payload: PtyBackgroundStreamEvent) => void): () => void {
|
||||
return combineUnsubscribes(
|
||||
this.allAdapters().map((adapter) => adapter.onBackgroundStreamEvent(callback))
|
||||
)
|
||||
return this.subscriptions.onBackgroundStreamEvent(callback)
|
||||
}
|
||||
|
||||
// Why: main subscribes on the routed provider, so without this the dead-endpoint
|
||||
// fan-out never reaches the renderer and only the written pane recovers (STA-2373).
|
||||
onWriteUnavailable(callback: (payload: { id: string }) => void): () => void {
|
||||
return combineUnsubscribes(
|
||||
this.allAdapters().map((adapter) => adapter.onWriteUnavailable(callback))
|
||||
)
|
||||
return this.subscriptions.onWriteUnavailable(callback)
|
||||
}
|
||||
|
||||
onReplay(_callback: (payload: { id: string; data: string }) => void): () => void {
|
||||
return () => {}
|
||||
onReplay(callback: (payload: { id: string; data: string }) => void): () => void {
|
||||
return this.subscriptions.onReplay(callback)
|
||||
}
|
||||
|
||||
onExit(callback: (payload: DaemonPtyRouterExitEvent) => void): () => void {
|
||||
this.exitListeners.push(callback)
|
||||
return () => {
|
||||
const idx = this.exitListeners.indexOf(callback)
|
||||
if (idx !== -1) {
|
||||
this.exitListeners.splice(idx, 1)
|
||||
}
|
||||
}
|
||||
return this.subscriptions.onExit(callback)
|
||||
}
|
||||
|
||||
ackColdRestore(sessionId: string): void {
|
||||
@@ -294,9 +261,7 @@ export class DaemonPtyRouter implements IPtyProvider {
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const unsubscribe of this.unsubscribers.splice(0)) {
|
||||
unsubscribe()
|
||||
}
|
||||
this.subscriptions.dispose()
|
||||
for (const adapter of this.allAdapters()) {
|
||||
adapter.dispose()
|
||||
}
|
||||
@@ -310,15 +275,11 @@ export class DaemonPtyRouter implements IPtyProvider {
|
||||
// Without this, each restart leaked a router instance pinned by the legacy
|
||||
// adapters' listener arrays (one pair per adapter per restart).
|
||||
disposeRouterOnly(): void {
|
||||
for (const unsubscribe of this.unsubscribers.splice(0)) {
|
||||
unsubscribe()
|
||||
}
|
||||
this.subscriptions.dispose()
|
||||
}
|
||||
|
||||
async disconnectOnly(): Promise<void> {
|
||||
for (const unsubscribe of this.unsubscribers.splice(0)) {
|
||||
unsubscribe()
|
||||
}
|
||||
this.subscriptions.dispose()
|
||||
await Promise.all([...this.allAdapters()].map((adapter) => adapter.disconnectOnly()))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user