Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/core/superstate/cacheParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SpaceInfo } from "shared/types/spaceInfo";
import { orderStringArrayByArray, uniq } from "shared/utils/array";

import { builtinSpaces } from "core/types/space";
import { resolvePath } from "core/superstate/utils/path";
import { linkContextRow, mergeContextRows, propertyDependencies, syncContextRow } from "core/utils/contexts/linkContextRow";
import { pathByJoins } from "core/utils/spaces/query";
import { ensureArray, initiateString, tagSpacePathFromTag } from "core/utils/strings";
Expand Down Expand Up @@ -47,10 +48,11 @@ export const parseContextTableToCache = (space: SpaceInfo, mdb: SpaceTables, pat
cols = defaultContextFields.rows as SpaceProperty[];
}
const schema = mdb[defaultContextSchemaID]?.schema ?? defaultContextDBSchema;
const contextPaths = mdb[defaultContextSchemaID]?.rows?.map(f => f[PathPropertyName]) ?? [];
const contextPaths = mdb[defaultContextSchemaID]?.rows?.map(f => resolvePath(f[PathPropertyName], space.path, (p) => pathsIndex.get(p)?.type == 'space')) ?? [];

const missingPaths = paths.filter(f => !contextPaths.includes(f));
const newPaths = [...orderStringArrayByArray(paths ?? [], contextPaths), ...missingPaths];
const knownPaths = orderStringArrayByArray((paths ?? []).filter(f => contextPaths.includes(f)), contextPaths);
const newPaths = [...knownPaths, ...missingPaths];
const dependencies = propertyDependencies(cols);
const spacePath = pathsIndex.get(space.path);
let rows = mergeContextRows(paths, mdb[defaultContextSchemaID]?.rows ?? [], pathsIndex, spacesMap, spacePath)
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/contexts/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export const updateContextValue = async (
{
const updateFunction = _updateFunction ?? updateValue
let newMDB = updateFunction(f, PathPropertyName, path, field, value);
if (rank)
if (rank != null)
newMDB = reorderRowsForPath(newMDB, [path], rank);
if (manager.superstate.settings.enhancedLogs) {
// Update Context Value
Expand Down
6 changes: 5 additions & 1 deletion src/core/utils/contexts/linkContextRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ const resolvedPath = resolvePath(_row[PathPropertyName], path?.path, (spacePath)
export const mergeContextRows = ( paths: string[], rows: DBRows, pathStates: Map<string, PathState>, spaceMap: IndexMap, path: PathState) => {

// Filter existing rows to only include valid paths, preserving database order (rank)
// Dedupe by resolved path so a corrupted table with duplicate rows heals on merge
const seenPaths = new Set<string>();
const validRows = rows.filter(row => {
const resolvedPath = resolvePath(row[PathPropertyName], path?.path, (spacePath) => pathStates.get(spacePath)?.type == 'space');
return paths.includes(resolvedPath);
if (!paths.includes(resolvedPath) || seenPaths.has(resolvedPath)) return false;
seenPaths.add(resolvedPath);
return true;
});

// Find paths that are in the paths array but not in any existing row
Expand Down
9 changes: 6 additions & 3 deletions src/shared/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export const onlyUniquePropCaseInsensitive =

return array.sort( function (a, b) {
const A = order.indexOf(a), B = order.indexOf(b);

if (A == -1 && B == -1) {
// neither is ranked: keep original relative order (stable sort)
return 0
Comment on lines 51 to +55
}
if (A > B) {
if (A != -1 && B == -1) {
return -1
Expand All @@ -62,9 +65,9 @@ export const onlyUniquePropCaseInsensitive =
}
return -1;
}

});

};


Expand Down