[ui] TwoTabs: The rose and the garden read again when another tab wrote, keeping their own

This commit is contained in:
2026-11-07 15:06:44 +01:00
parent 102ac44675
commit b67fa3603d
7 changed files with 185 additions and 9 deletions
+14 -8
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import {
careEvent, goodMoment, localDay, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
careEvent, goodMoment, localDay, newCare, newOpened, petalAt, pour, roseAt, ROSE, sentenceOpened, touch, TOUCH, UNTOUCHED,
type Mood, type PetalNow, type Place, type RoseNow, type Stage, type TouchState
} from '@rosea/engine';
import type { CareEvent, SentenceOpened } from '@rosea/schema';
@@ -78,19 +78,25 @@
});
}
/** After she is brought back from a file: read her log and keys again. */
function restored(): void {
/**
* Read her log and keys again: after she is brought back from a file, or when another
* tab wrote (two-tabs.md). What this tab already shows is kept: the log only grows,
* and this tab's own write may still be on its way when the read finishes.
*/
function reload(): void {
Promise.all([wiring.loadCare(), wiring.pastKeys(), wiring.loadOpened()]).then(
([log, past, petals]) => {
events = log;
pastKeys = past;
opened = petals;
events = [...log, ...newCare(log, events)];
opened = [...petals, ...newOpened(petals, opened)];
pastKeys = [...new Set([...past, ...pastKeys])];
now = wiring.now();
},
(error: unknown) => console.error('rose: could not read her back after restoring', error)
(error: unknown) => console.error('rose: could not read her log again', error)
);
}
$effect(() => wiring.changes?.(reload));
function care(act: CareEvent['act'], amount?: number): void {
if (seed === null) return;
now = wiring.now();
@@ -260,7 +266,7 @@
</dl>
{/if}
{#if showSafe && seed}
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={restored} />
<KeepSafe {wiring} currentKey={seed} today={localDay(now, timeZone)} onrestored={reload} />
{/if}
</section>
{/if}
+2
View File
@@ -23,6 +23,8 @@ export interface RoseWiring {
/** The rose as a file's contents, and bringing one back. */
readonly exportRose: (currentKey: string) => Promise<unknown>;
readonly importRose: (file: unknown, currentKey: string) => Promise<{ readonly added: number; readonly petals: number }>;
/** Hears care, a petal or a rose brought back in another tab (two-tabs.md); returns the way to stop. */
readonly changes?: (listener: () => void) => () => void;
/** Hands the player a file to keep. */
readonly saveFile: (name: string, text: string) => void;
/** The browser's promise to keep the garden: true, false, or null when it cannot say. */