mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
refactor(desktop): reuse JSON array count facts (#16474)
This commit is contained in:
1 parent
2f0bfa1f31
commit
726fd2d86c
1 file changed
+54
-64
@@ -170,14 +170,6 @@ export const ownNamesUnreadOtherwise = (
|
||||
return kept;
|
||||
};
|
||||
|
||||
/** The array a member pointer sits in, when its parent is one in a view. The empty pointer is the view itself, as a file whose top level is an array. */
|
||||
const arrayOf = (view: JsonValue | null, pointer: string) => {
|
||||
const list = pointer.replace(/\/[^/]+$/u, "");
|
||||
if (list === pointer) return undefined;
|
||||
const items = list === "" ? view : resolvePointer(view, list);
|
||||
return Array.isArray(items) ? list : undefined;
|
||||
};
|
||||
|
||||
/** A counted array, and the kind field and value its counted members share, when they carry one. */
|
||||
interface Counted {
|
||||
readonly list: string;
|
||||
@@ -197,26 +189,24 @@ const kindOf = (member: JsonValue | undefined, items: readonly JsonValue[]) => {
|
||||
: { key, value };
|
||||
};
|
||||
|
||||
const membersOf = (view: JsonValue | null, { list, kind }: Counted) => {
|
||||
const items = list === "" ? view : resolvePointer(view, list);
|
||||
return (Array.isArray(items) ? items : []).filter(
|
||||
const memberCount = (items: readonly JsonValue[], kind?: Counted["kind"]) =>
|
||||
items.filter(
|
||||
(item) =>
|
||||
kind === undefined || (isRecord(item) && item[kind.key] === kind.value),
|
||||
);
|
||||
};
|
||||
).length;
|
||||
|
||||
const countOf = (
|
||||
file: string,
|
||||
counted: Counted,
|
||||
view: JsonValue | null,
|
||||
{ list, kind }: Counted,
|
||||
equals: number,
|
||||
): Suggestion => ({
|
||||
kind: "member_count",
|
||||
file,
|
||||
pointer: counted.list,
|
||||
...(counted.kind === undefined
|
||||
pointer: list,
|
||||
...(kind === undefined
|
||||
? {}
|
||||
: { matching: spellCondition({ prefix: "", ...counted.kind }) }),
|
||||
equals: membersOf(view, counted).length,
|
||||
: { matching: spellCondition({ prefix: "", ...kind }) }),
|
||||
equals,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -231,50 +221,55 @@ const countOf = (
|
||||
export const jsonCounts = (
|
||||
file: string,
|
||||
change: Pick<DesktopStateDifference, "added" | "changed" | "removed">,
|
||||
ends: {
|
||||
readonly baseline: JsonValue | null;
|
||||
readonly final: JsonValue | null;
|
||||
},
|
||||
ends: Ends,
|
||||
): { readonly checks: Suggestion[]; readonly guards: Suggestion[] } => {
|
||||
const named = (list: string, view: JsonValue | null) =>
|
||||
locatePointer(view, list).byPosition.length === 0;
|
||||
const lists = new Map<string, Counted>();
|
||||
const note = (pointer: string, view: JsonValue | null) => {
|
||||
const list = arrayOf(view, pointer);
|
||||
const items = list === undefined ? [] : resolvePointer(view, list);
|
||||
const atEnd =
|
||||
list === undefined
|
||||
? undefined
|
||||
: list === ""
|
||||
? ends.final
|
||||
: resolvePointer(ends.final, list);
|
||||
const namedArrays = (list: string) => {
|
||||
const seed = locatePointer(ends.baseline, list);
|
||||
const end = locatePointer(ends.final, list);
|
||||
if (
|
||||
list === undefined ||
|
||||
!Array.isArray(atEnd) ||
|
||||
!named(list, ends.baseline) ||
|
||||
!named(list, ends.final)
|
||||
seed.byPosition.length ||
|
||||
end.byPosition.length ||
|
||||
end.kind !== "value" ||
|
||||
!Array.isArray(end.value)
|
||||
)
|
||||
return;
|
||||
return undefined;
|
||||
return {
|
||||
seed:
|
||||
seed.kind === "value" && Array.isArray(seed.value)
|
||||
? seed.value
|
||||
: undefined,
|
||||
end: end.value,
|
||||
};
|
||||
};
|
||||
const lists = new Map<string, Counted & { seed: number; end: number }>();
|
||||
const note = (pointer: string, removed: boolean) => {
|
||||
const list = pointer.replace(/\/[^/]+$/u, "");
|
||||
if (list === pointer) return;
|
||||
const arrays = namedArrays(list);
|
||||
if (arrays === undefined) return;
|
||||
const items = removed ? arrays.seed : arrays.end;
|
||||
if (items === undefined) return;
|
||||
const kind = kindOf(
|
||||
resolvePointer(view, pointer),
|
||||
Array.isArray(items) ? items : [],
|
||||
resolvePointer(removed ? ends.baseline : ends.final, pointer),
|
||||
items,
|
||||
);
|
||||
const counted = { list, ...(kind && { kind }) };
|
||||
lists.set(JSON.stringify(counted), counted);
|
||||
const key = JSON.stringify(counted);
|
||||
if (!lists.has(key))
|
||||
lists.set(key, {
|
||||
...counted,
|
||||
seed: memberCount(arrays.seed ?? [], kind),
|
||||
end: memberCount(arrays.end, kind),
|
||||
});
|
||||
};
|
||||
for (const pointer of change.added) note(pointer, ends.final);
|
||||
for (const pointer of change.removed) note(pointer, ends.baseline);
|
||||
for (const pointer of change.added) note(pointer, false);
|
||||
for (const pointer of change.removed) note(pointer, true);
|
||||
const depth = ({ list }: Counted) => list.split("/").length;
|
||||
const checks = [...lists.values()]
|
||||
.toSorted((left, right) => depth(left) - depth(right))
|
||||
.filter((counted) => {
|
||||
const [seed, end] = [ends.baseline, ends.final].map(
|
||||
(view) => membersOf(view, counted).length,
|
||||
);
|
||||
// A lone member makes no list.
|
||||
return seed !== end && Math.max(seed ?? 0, end ?? 0) > 1;
|
||||
})
|
||||
.map((counted) => countOf(file, counted, ends.final))
|
||||
// A lone member makes no list.
|
||||
.filter(({ seed, end }) => seed !== end && Math.max(seed, end) > 1)
|
||||
.map((counted) => countOf(file, counted, counted.end))
|
||||
// generation-cap: suggested count checks
|
||||
.slice(0, MAX_COUNTS);
|
||||
// Edits stop at the first guard, and each list is read once, however many records they touch.
|
||||
@@ -287,17 +282,12 @@ export const jsonCounts = (
|
||||
const counted = { list };
|
||||
if (tried.has(list)) continue;
|
||||
tried.add(list);
|
||||
const items = list === "" ? ends.final : resolvePointer(ends.final, list);
|
||||
if (
|
||||
Array.isArray(items) &&
|
||||
!lists.has(JSON.stringify(counted)) &&
|
||||
named(list, ends.baseline) &&
|
||||
named(list, ends.final) &&
|
||||
membersOf(ends.baseline, counted).length > 1 &&
|
||||
membersOf(ends.baseline, counted).length ===
|
||||
membersOf(ends.final, counted).length
|
||||
) {
|
||||
guards.push(countOf(file, counted, ends.baseline));
|
||||
if (lists.has(JSON.stringify(counted))) continue;
|
||||
const arrays = namedArrays(list);
|
||||
if (arrays?.seed === undefined) continue;
|
||||
const seed = memberCount(arrays.seed);
|
||||
if (seed > 1 && seed === memberCount(arrays.end)) {
|
||||
guards.push(countOf(file, counted, seed));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user