Skip to content

Commit f3dfb76

Browse files
Merge remote-tracking branch 'upstream/main' into HEAD
# Conflicts: # apps/mobile/src/native/T3ComposerEditor.ios.tsx
2 parents c585c4a + 3f905ca commit f3dfb76

237 files changed

Lines changed: 9054 additions & 1908 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const PREVIEW_SET_ZOOM_FACTOR_CHANNEL = "desktop:preview-set-zoom-factor"
9090
export const PREVIEW_HARD_RELOAD_CHANNEL = "desktop:preview-hard-reload";
9191
export const PREVIEW_SET_COLOR_SCHEME_CHANNEL = "desktop:preview-set-color-scheme";
9292
export const PREVIEW_SET_AUDIO_MUTED_CHANNEL = "desktop:preview-set-audio-muted";
93+
export const PREVIEW_SET_SHOWN_CHANNEL = "desktop:preview-set-shown";
9394
export const PREVIEW_OPEN_DEVTOOLS_CHANNEL = "desktop:preview-open-devtools";
9495
export const PREVIEW_CLEAR_COOKIES_CHANNEL = "desktop:preview-clear-cookies";
9596
export const PREVIEW_CLEAR_CACHE_CHANNEL = "desktop:preview-clear-cache";

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DesktopPreviewRegisterWebviewInputSchema,
1010
DesktopPreviewScreenshotArtifactSchema,
1111
DesktopPreviewSetAudioMutedInputSchema,
12+
DesktopPreviewSetShownInputSchema,
1213
DesktopPreviewSetColorSchemeInputSchema,
1314
DesktopPreviewSetZoomFactorInputSchema,
1415
BrowserImportResult,
@@ -193,6 +194,15 @@ const setAudioMuted = DesktopIpc.makeIpcMethod({
193194
yield* manager.setAudioMuted(tabId, audioMuted);
194195
}),
195196
});
197+
const setShown = DesktopIpc.makeIpcMethod({
198+
channel: IpcChannels.PREVIEW_SET_SHOWN_CHANNEL,
199+
payload: DesktopPreviewSetShownInputSchema,
200+
result: Schema.Void,
201+
handler: Effect.fn("desktop.ipc.preview.setShown")(function* ({ tabId, shown }) {
202+
const manager = yield* PreviewManager.PreviewManager;
203+
yield* manager.setShown(tabId, shown);
204+
}),
205+
});
196206
const openDevTools = tabMethod(
197207
IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
198208
"desktop.ipc.preview.openDevTools",
@@ -432,6 +442,7 @@ export const methods = [
432442
setColorScheme,
433443
setZoomFactor,
434444
setAudioMuted,
445+
setShown,
435446
openDevTools,
436447
clearCookies,
437448
clearCache,

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
348348
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL, { tabId, colorScheme }),
349349
setAudioMuted: (tabId, audioMuted) =>
350350
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL, { tabId, audioMuted }),
351+
setShown: (tabId, shown) =>
352+
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_SHOWN_CHANNEL, { tabId, shown }),
351353
openDevTools: (tabId) =>
352354
ipcRenderer.invoke(IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, { tabId }),
353355
listBrowserImportSources: () => ipcRenderer.invoke(IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL),

‎apps/desktop/src/preview/BrowserImport/BrowserImport.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ export class BrowserCookieWriteError extends Schema.TaggedError<BrowserCookieWri
9696
"BrowserCookieWriteError",
9797
{
9898
url: Schema.String,
99-
name: Schema.String,
99+
cookieName: Schema.String,
100100
cause: Schema.Defect(),
101101
},
102102
) {
103103
override get message(): string {
104-
return `Could not write imported cookie ${this.name} for ${this.url}.`;
104+
return `Could not write imported cookie ${this.cookieName} for ${this.url}.`;
105105
}
106106
}
107107

@@ -167,7 +167,8 @@ export const writeCookies = Effect.fn("BrowserImport.writeCookies")(function* (
167167
sameSite: cookie.sameSite,
168168
...(cookie.expirationDate === undefined ? {} : { expirationDate: cookie.expirationDate }),
169169
}),
170-
catch: (cause) => new BrowserCookieWriteError({ url: cookie.url, name: cookie.name, cause }),
170+
catch: (cause) =>
171+
new BrowserCookieWriteError({ url: cookie.url, cookieName: cookie.name, cause }),
171172
}).pipe(
172173
Effect.as(true),
173174
Effect.tapError((error) => Effect.logDebug(error.message, { cause: error.cause })),

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,44 @@ describe("DesktopBrowserHost", () => {
144144
}),
145145
);
146146

147+
it.effect("re-sends shown to a backend that starts later and when a tab attaches again", () =>
148+
Effect.gen(function* () {
149+
const host = yield* DesktopBrowserHost.make;
150+
host.attach(key, makeDebuggee().tab);
151+
host.setShown(key, true);
152+
const reader = yield* takeEvents(host, 5).pipe(Effect.forkScoped);
153+
154+
yield* Effect.yieldNow;
155+
host.detach(key);
156+
host.attach(key, makeDebuggee().tab);
157+
158+
expect(yield* Fiber.join(reader)).toEqual([
159+
{ type: "attached", ...key },
160+
{ type: "shown", ...key, shown: true },
161+
{ type: "detached", ...key },
162+
{ type: "attached", ...key },
163+
{ type: "shown", ...key, shown: true },
164+
]);
165+
}),
166+
);
167+
168+
it.effect("replays an explicit hidden report before announcing another tab", () =>
169+
Effect.gen(function* () {
170+
const host = yield* DesktopBrowserHost.make;
171+
const other = { ...key, tabId: "tab-2" };
172+
host.setShown(key, false);
173+
host.attach(key, makeDebuggee().tab);
174+
host.setShown(other, true);
175+
host.attach(other, makeDebuggee().tab);
176+
177+
expect(yield* takeEvents(host, 3)).toEqual([
178+
{ type: "attached", ...key },
179+
{ type: "shown", ...key, shown: false },
180+
{ type: "attached", ...other },
181+
]);
182+
}),
183+
);
184+
147185
it.effect("drops replies from a relay the server released", () =>
148186
Effect.gen(function* () {
149187
const host = yield* DesktopBrowserHost.make;

‎apps/desktop/src/preview/DesktopBrowserHost.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class DesktopBrowserHost extends Context.Service<
8787
readonly handleCommandLine: (line: string) => Effect.Effect<void>;
8888
readonly attach: (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => void;
8989
readonly detach: (key: DesktopBrowserTabKey) => void;
90+
readonly setShown: (key: DesktopBrowserTabKey, shown: boolean | undefined) => void;
9091
readonly humanStartedDownload: (source: Electron.WebContents) => boolean;
9192
readonly placeDownload: (source: Electron.WebContents, item: Electron.DownloadItem) => boolean;
9293
readonly pointers: Stream.Stream<{
@@ -119,6 +120,9 @@ export const make = Effect.gen(function* () {
119120
const tabs = new Map<string, AttachedTab>();
120121
/** Threads whose agent turn runs, by the server's account. */
121122
const agentTurns = new Set<string>();
123+
// Kept apart from attachment: the renderer can show a webview before its debugger attaches,
124+
// and the server forgets `shown` on detach, for example while DevTools holds the debugger.
125+
const shownTabs = new Map<string, boolean>();
122126
const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event));
123127

124128
const nextScreenshot = (tab: AttachedTab) => {
@@ -456,6 +460,19 @@ export const make = Effect.gen(function* () {
456460
debuggee.debugger.on("message", tab.onMessage);
457461
debuggee.webContents.on("did-navigate", tab.onNewDocument);
458462
emit({ type: "attached", ...key });
463+
const shown = shownTabs.get(id);
464+
if (shown !== undefined) emit({ type: "shown", ...key, shown });
465+
};
466+
467+
const setShown = (key: DesktopBrowserTabKey, shown: boolean | undefined) => {
468+
const id = keyOf(key);
469+
if (shown === undefined) {
470+
shownTabs.delete(id);
471+
return;
472+
}
473+
if (shownTabs.get(id) === shown) return;
474+
shownTabs.set(id, shown);
475+
if (tabs.has(id)) emit({ type: "shown", ...key, shown });
459476
};
460477

461478
const handleCommandLine = (line: string) =>
@@ -494,7 +511,14 @@ export const make = Effect.gen(function* () {
494511
[...tabs.values()],
495512
(tab) => {
496513
dropRelay(tab);
497-
return PubSub.publish(outbox, { type: "attached", ...tab.key });
514+
const shown = shownTabs.get(keyOf(tab.key));
515+
return PubSub.publish(outbox, { type: "attached", ...tab.key }).pipe(
516+
Effect.andThen(
517+
shown !== undefined
518+
? PubSub.publish(outbox, { type: "shown", ...tab.key, shown })
519+
: Effect.void,
520+
),
521+
);
498522
},
499523
{ discard: true },
500524
);
@@ -514,6 +538,7 @@ export const make = Effect.gen(function* () {
514538
handleCommandLine,
515539
attach,
516540
detach,
541+
setShown,
517542
placeDownload,
518543
humanStartedDownload,
519544
});

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
16851685
});
16861686
if (Option.isNone(tab)) return;
16871687
const closedTab = tab.value;
1688+
if (closedTab.serverTab) browserHost.setShown(closedTab.serverTab, undefined);
16881689
if (closedTab.webContentsId != null) {
16891690
yield* Effect.all(
16901691
[
@@ -1893,6 +1894,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
18931894
yield* rendererHistory.register(wc, { surface: "preview" });
18941895
const webContentsId = wc.id;
18951896
wc.once("destroyed", () => {
1897+
runFork(hideDestroyedGuest(webContentsId));
18961898
runFork(detachControlSession(webContentsId));
18971899
});
18981900
yield* ensureControlSession(wc).pipe(
@@ -2346,6 +2348,21 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23462348
yield* applyColorScheme(tabId, wc, colorScheme);
23472349
});
23482350

2351+
const setShown = Effect.fn("PreviewManager.setShown")(function* (tabId: string, shown: boolean) {
2352+
const serverTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.serverTab;
2353+
if (serverTab) browserHost.setShown(serverTab, shown);
2354+
});
2355+
2356+
// A window reload or guest crash destroys the guest without the renderer reporting it hidden.
2357+
// The renderer reports again once a new guest registers.
2358+
const hideDestroyedGuest = Effect.fnUntraced(function* (webContentsId: number) {
2359+
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
2360+
if (tab.webContentsId === webContentsId && tab.serverTab) {
2361+
browserHost.setShown(tab.serverTab, false);
2362+
}
2363+
}
2364+
});
2365+
23492366
const setAudioMuted = Effect.fn("PreviewManager.setAudioMuted")(function* (
23502367
tabId: string,
23512368
audioMuted: boolean,
@@ -3307,6 +3324,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
33073324
setAnnotationTheme,
33083325
setAnnotationSendEnabled,
33093326
setAudioMuted,
3327+
setShown,
33103328
setColorScheme,
33113329
setMainWindow,
33123330
setForwardedShortcuts: (shortcuts: ReadonlyArray<PreviewForwardedShortcut>) =>
@@ -3512,6 +3530,7 @@ export class PreviewManager extends Context.Service<
35123530
tabId: string,
35133531
audioMuted: boolean,
35143532
) => Effect.Effect<void, PreviewManagerError>;
3533+
readonly setShown: (tabId: string, shown: boolean) => Effect.Effect<void>;
35153534
readonly openDevTools: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
35163535
readonly clearCookies: (
35173536
partitions?: ReadonlyArray<string>,
@@ -3652,6 +3671,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
36523671
setColorScheme: operations.setColorScheme,
36533672
setZoomFactor: operations.setZoomFactor,
36543673
setAudioMuted: operations.setAudioMuted,
3674+
setShown: operations.setShown,
36553675
openDevTools: operations.openDevTools,
36563676
clearCookies: Effect.fn("PreviewManager.clearCookies")(function* (partitions) {
36573677
yield* browserSession

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private struct ComposerControlledDocumentPayload: Decodable {
2424
let tokensJson: String
2525
let mostRecentEventCount: Int
2626
let isNativeEcho: Bool
27+
let resetGeneration: Int
2728
}
2829

2930
private struct ComposerThemePayload: Decodable {
@@ -444,6 +445,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
444445
private var isReadOnly = false
445446
private var isApplyingControlledValue = false
446447
private var nativeEventCount = 0
448+
private var resetGeneration = 0
447449
private var lastContentSize = CGSize.zero
448450
private var iconImages: [String: UIImage] = [:]
449451
private var pendingIconUris = Set<String>()
@@ -489,6 +491,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
489491
var payload: [String: Any] = context
490492
payload["value"] = self.textView.serializedText()
491493
payload["eventCount"] = self.nativeEventCount
494+
payload["resetGeneration"] = self.resetGeneration
492495
payload["selection"] = ["start": selection.start, "end": selection.end]
493496
self.onComposerPasteContext(payload)
494497
}
@@ -499,6 +502,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
499502
self.onComposerPasteText([
500503
"value": self.textView.serializedText(),
501504
"eventCount": self.nativeEventCount,
505+
"resetGeneration": self.resetGeneration,
502506
"text": text,
503507
"selection": ["start": selection.start, "end": selection.end],
504508
])
@@ -651,11 +655,21 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
651655
}
652656

653657
func setControlledDocumentJson(_ documentJson: String) {
654-
guard let document = decode(ComposerControlledDocumentPayload.self, from: documentJson),
655-
document.mostRecentEventCount >= nativeEventCount else {
658+
guard let document = decode(ComposerControlledDocumentPayload.self, from: documentJson) else {
656659
return
657660
}
658-
if document.isNativeEcho && textView.serializedText() != document.value {
661+
// A parent reset (a send clear) wins over native edits it could not see; JS drops their events
662+
// by generation, so the revision restarts where the parent last acknowledged.
663+
let isReset = document.resetGeneration > resetGeneration
664+
guard isReset || document.mostRecentEventCount >= nativeEventCount else {
665+
return
666+
}
667+
if isReset {
668+
resetGeneration = document.resetGeneration
669+
nativeEventCount = document.mostRecentEventCount
670+
endComposition()
671+
}
672+
if document.isNativeEcho && !isReset && textView.serializedText() != document.value {
659673
return
660674
}
661675
if tokensJson != document.tokensJson {
@@ -900,6 +914,15 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
900914
emitContentSizeIfNeeded()
901915
}
902916

917+
private func endComposition() {
918+
guard textView.markedTextRange != nil else {
919+
return
920+
}
921+
isApplyingControlledValue = true
922+
textView.unmarkText()
923+
isApplyingControlledValue = false
924+
}
925+
903926
private func makeAttributedDocument() -> NSAttributedString {
904927
let result = NSMutableAttributedString()
905928
let source = value as NSString
@@ -1113,6 +1136,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11131136
"value": value,
11141137
"selection": ["start": selection.start, "end": selection.end],
11151138
"eventCount": nativeEventCount,
1139+
"resetGeneration": resetGeneration,
11161140
])
11171141
updatePlaceholderVisibility()
11181142
emitContentSizeIfNeeded()
@@ -1129,6 +1153,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11291153
"value": currentValue,
11301154
"selection": ["start": selection.start, "end": selection.end],
11311155
"eventCount": nativeEventCount,
1156+
"resetGeneration": resetGeneration,
11321157
])
11331158
}
11341159

0 commit comments

Comments
 (0)