Skip to content

Commit d3c875f

Browse files
donjor-agent[bot]rsi-pr-agent
authored andcommitted
feat(web): scope unsent composer drafts per window
## What Changed Unsent composer drafts (draft sessions, their composer drafts, the project-to-session map) persist per window in `<key>:window:<token>`; the token lives in `sessionStorage`. Thread drafts and sticky model choices stay in the shared `<key>` bucket. All logic lives in `apps/web/src/windowDraftScope.fork.ts`; `composerDraftStore.ts` carries four marked hooks and loses no upstream line. | Concern | Behaviour | | --- | --- | | Shared writes | only the slices this window changed since it last read or wrote; deletions honoured, other windows' edits kept | | Flush | one flush (debounce or `beforeunload`) writes both buckets | | Ownership | each live window holds a Web Lock named for its token for its lifetime | | Adoption | at startup, under an exclusive adopt lock, a window adopts legacy draft sessions in the shared bucket and every window bucket whose token lock is not held (closed tab, previous Electron run) and whose `touchedAt` is older than 5s | | Reload grace | every write and flush, the unload one included, stamps `touchedAt`; an unlocked fresh bucket is re-checked once after 5s with a fresh `query()`, so a reloading window keeps its bucket and a closed one is still recovered | | Live store | adopted entries pass through the store's own hydrate `migrate`/`merge`, then `setState` adds only keys the store lacks; no rehydrate, so unsaved in-memory state survives | | Write order | adopting window's bucket first, then the sources are cleared or removed | | Conflicts | this window's entries win; sources fill gaps; two sources mapping one project keep the newer session by `createdAt`, first source on a tie; the other session keeps its text unmapped | ## Why Two windows on one route shared and clobbered unsent drafts. Electron uses hash routes, so `pathname` is always `/` and every project there resolves to the hub key. ## Checklist - [x] This PR is small and focused - [x] I explained what changed and why - [x] No UI changes (persistence scoping only, no screenshots) - [x] No animation/interaction changes (no video) ## Close conditions - [x] **Settle:** test proves today's hash-route key - [x] **Unsent:** drafts are per window - [x] **Durable:** thread drafts stay shared - [x] **Migration:** never deletes draft text ## Notes Tests use happy-dom `Storage` and a FIFO lock manager with `query()`: simultaneous upgrade, write order, adoption mid-debounce, reload after adopt, durable clear, interleaved edits, unload flush, closed-window recovery, relaunch with two orphans, live bucket untouched, reload inside the grace window, fresh-then-closed re-check, adopted drafts merged into a real zustand persist store beside an unsaved in-memory image; a controlled clock drives every timer. - **No Web Locks API:** ownership is unknowable, so orphan buckets are left in place and not adopted. - **Duplicated web tab:** copies `sessionStorage` and shares the token. Closes #1346 ## Fork trailers Squashes: - a2a5342 feat(web): scope unsent composer drafts per window - cbb2302 fixup! feat(web): scope unsent composer drafts per window Fork-Domain: multi-window Fork-Tier: core
1 parent f2151b6 commit d3c875f

3 files changed

Lines changed: 1100 additions & 0 deletions

File tree

‎apps/web/src/composerDraftStore.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import { UnifiedSettings } from "@t3tools/contracts/settings";
9999

100100
const isElementContextDetails = Schema.is(ElementContextDetails);
101101
import { ReviewCommentContextSchema, type ReviewCommentContext } from "./reviewCommentContext";
102+
import * as forkDraftScope from "./windowDraftScope.fork"; // fork-hook: multi-window/draft-scope-import
102103
const isRuntimeMode = Schema.is(RuntimeMode);
103104
const isProviderDriverKind = Schema.is(ProviderDriverKind);
104105
const isReviewCommentContext = Schema.is(ReviewCommentContextSchema);
@@ -158,6 +159,7 @@ const composerPersistStorage: PersistStorage<ComposerPersistState> = {
158159
setItem: (name, value) => composerDebouncedStorage.setItem(name, value),
159160
removeItem: (name) => composerDebouncedStorage.removeItem(name),
160161
};
162+
forkDraftScope.install(composerDebouncedStorage, partializeComposerDraftStoreState); // fork-hook: multi-window/draft-scope-install
161163

162164
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
163165
window.addEventListener("beforeunload", () => {
@@ -2228,6 +2230,9 @@ function readPersistedAttachmentIdsFromStorage(threadKey: string): string[] {
22282230
if (threadKey.length === 0) {
22292231
return [];
22302232
}
2233+
if (forkDraftScope.ownsDraft(threadKey)) {
2234+
return forkDraftScope.attachmentIds(threadKey);
2235+
} // fork-hook: multi-window/draft-scope-attachments
22312236
try {
22322237
const persisted = getLocalStorageItem(
22332238
activeComposerDraftStorageKey,
@@ -4247,6 +4252,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
42474252
);
42484253

42494254
export const useComposerDraftStore = composerDraftStore;
4255+
forkDraftScope.adoptInto(composerDraftStore); // fork-hook: multi-window/draft-scope-adopt
42504256

42514257
export function subscribeComposerDraftModelReconciliation(input: {
42524258
readonly registry: AtomRegistry.AtomRegistry;

0 commit comments

Comments
 (0)