From d3ff3a8f1d89b037d9fc6b82f3ae3bd3e35cb7ee Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Sun, 9 Nov 2025 21:21:49 -0500 Subject: [PATCH] eng-1044: experiments wip --- apps/roam/src/utils/fireQuery.ts | 16 +++++++++++----- apps/roam/src/utils/getExportTypes.ts | 2 +- apps/roam/src/utils/getRelationData.ts | 18 ++++++++++++------ apps/roam/src/utils/migrateRelations.ts | 2 +- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/apps/roam/src/utils/fireQuery.ts b/apps/roam/src/utils/fireQuery.ts index a0e2bda9c..1d68589d6 100644 --- a/apps/roam/src/utils/fireQuery.ts +++ b/apps/roam/src/utils/fireQuery.ts @@ -30,6 +30,7 @@ type RelationInQuery = { export type FireQueryArgs = QueryArgs & { isCustomEnabled?: boolean; customNode?: string; + local?: boolean; context?: { relationsInQuery?: RelationInQuery[]; customNodes?: DiscourseNode[]; @@ -315,7 +316,7 @@ export const fireQuerySync = (args: FireQueryArgs): QueryResult[] => { }; const fireQuery: FireQuery = async (_args) => { - const { isCustomEnabled, customNode, ...args } = _args; + const { isCustomEnabled, customNode, local, ...args } = _args; const { query, formatResult, inputs } = isCustomEnabled ? { @@ -347,10 +348,15 @@ const fireQuery: FireQuery = async (_args) => { console.groupEnd(); } - const queryResults = await window.roamAlphaAPI.data.backend.q( - query, - ...inputs, - ); + let queryResults: unknown[][] = []; + if (local) { + queryResults = await window.roamAlphaAPI.data.async.fast.q( + query, + ...inputs, + ); + } else { + queryResults = await window.roamAlphaAPI.data.backend.q(query, ...inputs); + } if (nodeEnv === "development") { console.timeEnd(`Query - ${queryId}`); diff --git a/apps/roam/src/utils/getExportTypes.ts b/apps/roam/src/utils/getExportTypes.ts index 6f6e19b99..3287ba820 100644 --- a/apps/roam/src/utils/getExportTypes.ts +++ b/apps/roam/src/utils/getExportTypes.ts @@ -375,7 +375,7 @@ const getExportTypes = ({ ); }; const getRelationData = () => - getRelationDataUtil(allRelations, nodeLabelByType); + getRelationDataUtil({ allRelations, nodeLabelByType, local: true }); const getJsonData = async () => { const grammar = allRelations.map(({ label, destination, source }) => ({ diff --git a/apps/roam/src/utils/getRelationData.ts b/apps/roam/src/utils/getRelationData.ts index b1311d75d..a0b436c93 100644 --- a/apps/roam/src/utils/getRelationData.ts +++ b/apps/roam/src/utils/getRelationData.ts @@ -6,10 +6,15 @@ import internalError from "./internalError"; // lifted from getExportTypes -export const getRelationDataUtil = async ( - allRelations: DiscourseRelation[], - nodeLabelByType: Record, -) => +export const getRelationDataUtil = async ({ + allRelations, + nodeLabelByType, + local, +}: { + allRelations: DiscourseRelation[]; + nodeLabelByType: Record; + local?: boolean; +}) => Promise.all( allRelations .filter( @@ -24,6 +29,7 @@ export const getRelationDataUtil = async ( ? [] : fireQuery({ returnNode: sourceLabel, + local, conditions: [ { relation: s.label, @@ -60,13 +66,13 @@ export const getRelationDataUtil = async ( }), ).then((r) => r.flat()); -const getRelationData = async () => { +const getRelationData = async (local?: boolean) => { const allRelations = getDiscourseRelations(); const allNodes = getDiscourseNodes(allRelations); const nodeLabelByType = Object.fromEntries( allNodes.map((a) => [a.type, a.text]), ); - return await getRelationDataUtil(allRelations, nodeLabelByType); + return await getRelationDataUtil({ allRelations, nodeLabelByType, local }); }; export default getRelationData; diff --git a/apps/roam/src/utils/migrateRelations.ts b/apps/roam/src/utils/migrateRelations.ts index 59c2f799b..41cba8014 100644 --- a/apps/roam/src/utils/migrateRelations.ts +++ b/apps/roam/src/utils/migrateRelations.ts @@ -20,7 +20,7 @@ const migrateRelations = async (dryRun = false): Promise => { await new Promise((resolve) => setTimeout(resolve, 150)); try { const processed = new Set(); - const relationData = await getRelationData(); + const relationData = await getRelationData(true); for (const rel of relationData) { const key = `${rel.source}:${rel.relUid}:${rel.target}`; if (processed.has(key)) continue;