Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/36619-studio-save-waits-verdicts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@object-ui/app-shell': patch
---

fix(studio): Save waits for an expression's check to reach the page before it sends, so a predicate that does not parse is held, not saved, on a busy machine too

Clause-②: no
Metadata-protocol: none

Studio's top-bar Save reads whether an edit is held (a formula, rule guard or action predicate that does not parse) once the expression checks of the last keystroke have run. It waited a fixed 50 ms past the check's delay, but an action predicate's verdict reaches the object's Save through several renders (the expression field, the condition builder, the action inspector, the actions panel, then the Data pillar), and on a busy main thread those renders came after the 50 ms: Save read the verdict from before the edit and sent the unparsable predicate. Save now also waits until every render already queued has run, so it reads the verdict of the text it is about to send. A Save pressed after the check has reported is not slower; one pressed while renders are pending waits for them.
55 changes: 52 additions & 3 deletions ui/packages/app-shell/src/views/studio-design/studioSaveBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
* - A buffer that holds an edit it cannot send yet (`held`) refuses the whole
* Save before anything is written, naming what it needs. The held state is
* read once the expression checks of the last keystroke have run (they run
* {@link LINT_DEBOUNCE_MS} after it): a Save pressed at once never sends a
* formula its check has not judged yet (objectui#4306).
* {@link LINT_DEBOUNCE_MS} after it) AND their verdicts have reached the
* buffers: a Save pressed at once never sends a formula its check has not
* judged yet (objectui#4306).
* - A verdict climbs to its buffer through effects, one render per hop (an
* action predicate's: the CEL field, the condition builder, the action
* inspector, the actions panel, then the Data pillar's `held`). Those
* renders run at default priority, so Save waits for a render of its own
* at transition priority ({@link useRendersSettled}): React commits it only
* once no default-priority render is left, the last hop included. A fixed
* margin past the debounce lost that race on a busy main thread and sent
* the unparsable text (objectstack#36619).
* - Cancel drops every dirty buffer's edits: each goes back to what the
* server holds.
*/
Expand Down Expand Up @@ -116,12 +125,50 @@ export interface StudioSaveCoordinator {
cancel: () => void;
}

/**
* objectstack#36619 — an answer that resolves once React has no render more
* urgent than a transition left: the caller's component renders at transition
* priority, and React reaches that render only after every default-priority
* one, including those scheduled by the effects of the renders before it. So
* an effect chain already under way (a CEL verdict climbing to the buffer that
* holds it) has run to its end when the answer resolves. Unmounted first, it
* resolves at once: nothing is left to wait for.
*/
function useRendersSettled(): () => Promise<void> {
const [tick, setTick] = React.useState(0);
const seq = React.useRef(0);
const waiters = React.useRef<Array<{ tick: number; resolve: () => void }>>([]);
React.useEffect(() => {
const due = waiters.current.filter((w) => w.tick <= tick);
if (due.length === 0) return;
waiters.current = waiters.current.filter((w) => w.tick > tick);
for (const w of due) w.resolve();
}, [tick]);
React.useEffect(
() => () => {
for (const w of waiters.current) w.resolve();
waiters.current = [];
},
[],
);
return React.useCallback(
() =>
new Promise<void>((resolve) => {
const next = ++seq.current;
waiters.current.push({ tick: next, resolve });
React.startTransition(() => setTick(next));
}),
[],
);
}

/** The surface's side: the registered buffers, and the Save and Cancel over them. */
export function useStudioSaveCoordinator(locale: string): StudioSaveCoordinator {
const entriesRef = React.useRef(new Map<number, React.MutableRefObject<StudioSaveEntry | null>>());
const [dirty, setDirty] = React.useState(false);
const [saving, setSaving] = React.useState(false);
const savingRef = React.useRef(false);
const rendersSettled = useRendersSettled();
const readDirty = React.useCallback(() => {
for (const ref of entriesRef.current.values()) if (ref.current?.dirty) return true;
return false;
Expand Down Expand Up @@ -152,6 +199,8 @@ export function useStudioSaveCoordinator(locale: string): StudioSaveCoordinator
savingRef.current = true;
setSaving(true);
await new Promise((settled) => setTimeout(settled, VERDICT_SETTLE_MS));
// The checks have run; their verdicts may still be climbing to the buffers.
await rendersSettled();
const entries = dirtyEntries();
const held = entries.find((e) => e.held !== null);
if (held || entries.length === 0) {
Expand Down Expand Up @@ -189,7 +238,7 @@ export function useStudioSaveCoordinator(locale: string): StudioSaveCoordinator
} else if (saved.length > 0) {
toast.success(tFormat('engine.studio.save.done', locale, { items: names(saved) }), { id: STUDIO_SAVE_TOAST_ID });
}
}, [dirtyEntries, locale, readDirty]);
}, [dirtyEntries, locale, readDirty, rendersSettled]);
const cancel = React.useCallback(() => {
if (savingRef.current) return;
for (const entry of dirtyEntries()) entry.cancel();
Expand Down
Loading