| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // RemoteTabHydrationState is a local publication snapshot, with no Serve I/O. |
| 9 | // Taking the route publication lock joins mount-time hydration to a ready |
| 10 | // barrier even when its frontend event is still queued for delivery. |
| 11 | func (a *App) RemoteTabHydrationState(tabID string) (RemoteTabStateView, error) { |
| 12 | tab := a.lockRemoteTabPublication(tabID) |
| 13 | if tab == nil { |
| 14 | return RemoteTabStateView{}, fmt.Errorf("remote tab %q not found", tabID) |
| 15 | } |
| 16 | defer tab.routeEventMu.Unlock() |
| 17 | a.remoteTabMu.Lock() |
| 18 | defer a.remoteTabMu.Unlock() |
| 19 | if a.remoteTabs[tabID] != tab { |
| 20 | return RemoteTabStateView{}, fmt.Errorf("remote tab %q was replaced", tabID) |
| 21 | } |
| 22 | return RemoteTabStateView{State: tab.state, Error: tab.err, HydrationRevision: tab.publication.hydrationRevision}, nil |
| 23 | } |
| 24 | |
| 25 | // publishRemoteTabState is the only writer of the state channel, so every |
| 26 | // publication carries the tab's next hydration revision. Callers must not hold |
| 27 | // remoteTabMu. |
| 28 | func (a *App) publishRemoteTabState(tabID string, view RemoteTabStateView) { |
| 29 | a.remoteTabMu.Lock() |
| 30 | if tab := a.remoteTabs[tabID]; tab != nil { |
| 31 | tab.publication.hydrationRevision++ |
| 32 | view.HydrationRevision = tab.publication.hydrationRevision |
| 33 | } |
| 34 | a.remoteTabMu.Unlock() |
| 35 | a.emitRemoteEvent(fmt.Sprintf("remote-tab:%s:state", tabID), view) |
| 36 | } |
| 37 | |
| 38 | // emitRemoteEvent bridges a kernel callback to the frontend through the async |
| 39 | // emitter so a slow webview never blocks the kernel. |
| 40 | func (a *App) emitRemoteEvent(name string, payload any) { |
| 41 | if a.remoteEventHook != nil { |
| 42 | a.remoteEventHook(name, payload) |
| 43 | } |
| 44 | if strings.HasPrefix(name, "remote-tab:") && strings.HasSuffix(name, ":state") { |
| 45 | a.emitRuntimeStateChanged() |
| 46 | } |
| 47 | ctx := a.bootContext() |
| 48 | if ctx == nil { |
| 49 | return |
| 50 | } |
| 51 | a.runtimeEvents.Emit(ctx, name, payload) |
| 52 | } |
| 53 |