fix: collection first-run pending and module tab cross-nav

Defer collection scheduling to BackgroundTasks after commit; claim pending runs atomically with retries. Only handle focus-module broadcasts for the matching moduleId so opening one module does not rewrite other tabs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-28 10:22:11 +08:00
parent 3413d5bf32
commit fed7f56ad0
5 changed files with 169 additions and 43 deletions

View file

@ -36,13 +36,18 @@ export function registerModuleWindow(moduleId: string): () => void {
/* ignore */
}
return registerBroadcastListener<FocusModuleMessage>(MODULE_CHANNEL, moduleAckKey(moduleId), (data) => {
const targetPath = String(data.path || "").trim();
if (targetPath && window.location.pathname !== targetPath) {
window.location.assign(targetPath);
}
window.focus();
});
return registerBroadcastListener<FocusModuleMessage>(
MODULE_CHANNEL,
moduleAckKey(moduleId),
(data) => {
const targetPath = String(data.path || "").trim();
if (targetPath && window.location.pathname !== targetPath) {
window.location.assign(targetPath);
}
window.focus();
},
(data) => data.moduleId === moduleId,
);
}
export function openOrFocusModule({ moduleId, path }: ModuleWindowSpec): void {

View file

@ -10,6 +10,7 @@ export function registerBroadcastListener<T extends { requestId: string }>(
channelName: string,
ackStorageKey: string,
onMessage: (data: T) => void,
shouldHandle?: (data: T) => boolean,
): () => void {
if (typeof BroadcastChannel === "undefined") {
return () => undefined;
@ -19,6 +20,7 @@ export function registerBroadcastListener<T extends { requestId: string }>(
channel.onmessage = (ev: MessageEvent<T>) => {
const data = ev.data;
if (!data?.requestId) return;
if (shouldHandle && !shouldHandle(data)) return;
try {
sessionStorage.setItem(ackStorageKey, data.requestId);
} catch {