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
19 changes: 12 additions & 7 deletions src/applier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1609,14 +1609,19 @@ function writeWidgetValue(
value: unknown,
): void {
const widgets = widgetsOf(node);
mset(widgets, widgetStorageKey(widget, occurrence), structuredClone(value));

const key = widgetStorageKey(widget, occurrence);
// Lay out exactly what will be stored, as the stored-then-refreshed order did.
const stored = structuredClone(value);
const form = storedWidgetFormOf(node);
if (!form || form.shape !== "array") return;
const entry = catalogEntry(catalog, String(node.get("type") ?? ""));
if (!entry?.dynamic_combos || !Object.hasOwn(entry.dynamic_combos, widget)) return;

const order = widgetOrderForProducerOrder(entry, form.order, widgets);
const entry = form?.shape === "array" ? catalogEntry(catalog, String(node.get("type") ?? "")) : undefined;
// Lay out the prospective selection before writing: the refresh can refuse
// it (a stored row count outside the group range), and a rejected op must
// leave the document untouched (KA-4).
const order = form && entry?.dynamic_combos && Object.hasOwn(entry.dynamic_combos, widget)
? widgetOrderForProducerOrder(entry, form.order, widgets, { key, value: stored })
: undefined;
mset(widgets, key, stored);
if (!form || !order) return;
if (order.length === form.order.length && order.every((name, index) => name === form.order[index])) return;
mset(node, WIDGET_FORM_KEY, { ...structuredClone(form), order: [...order] });
}
Expand Down
10 changes: 8 additions & 2 deletions src/dynamic-combos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ export function widgetOrderForWidgets(
* write wins. `producerOrder` may already contain the previous selection's
* children; {@link expand} removes every catalog-owned child before expanding
* the selectors from their stored values, so repeated A→B→A switches remain
* anchored to the same producer-local order.
* anchored to the same producer-local order. `pending` overlays one register
* write that has not been stored yet, so a caller can lay out the prospective
* selection and refuse it before mutating anything (KA-4).
*/
export function widgetOrderForProducerOrder(
entry: WidgetCatalogEntry,
producerOrder: readonly string[],
widgets: Y.Map<unknown> | undefined,
pending?: { key: string; value: unknown },
): readonly string[] {
const combos = combosOf(entry);
if (!combos) return [...producerOrder];
Expand All @@ -180,7 +183,10 @@ export function widgetOrderForProducerOrder(
return expand(
entry,
combos,
(name, _index, occurrence) => widgets?.get(widgetStorageKey(name, occurrence)),
(name, _index, occurrence) => {
const key = widgetStorageKey(name, occurrence);
return pending?.key === key ? pending.value : widgets?.get(key);
},
producerOrder,
declaredGroups,
).order;
Expand Down
14 changes: 14 additions & 0 deletions test/dynamic-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,20 @@ describe("DynamicGroup catalog layout", () => {
},
);

it.each([-1, 1_000_000])("refuses a selector write before mutation when the declared form stores count %s", (count) => {
// A declared form is accepted as the producer wrote it, so it can carry a
// count the group range refuses. Selecting the option that lays that group
// out must be rejected whole, not after the selector register is written.
const workflow = structuredClone(cases.empty);
const node = workflow.nodes[0]!;
delete node["widgets_values_named"];
node.widgets_values = ["first", "off", count, "last"];
node.widgets_values_form = { order: ["before", "mode", "loras", "after"] };

assertRejectedWithoutMutation(workflow, edit("mode", "on"), "dynamic_group_count_out_of_range", conditionalCatalog,
edit("after", "must not run", 2));
});

it("preserves rows above a newer catalog's advertised maximum", () => {
const smaller = structuredClone(catalog);
smaller.types["DevToolsNodeWithDynamicGroup"]!.dynamic_groups!["loras"]!.max = 1;
Expand Down
Loading