From d9c3587633dd79dfbe4460dd56e721c53f237dd0 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sat, 12 Apr 2025 15:28:34 -0600 Subject: [PATCH 1/3] Refactor getOverlayInfo to use async/await and improve error handling. Update cache key from title to tag and remove overlayQueue logic for cleaner implementation. --- .../components/DiscourseContextOverlay.tsx | 85 +++++++------------ 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/apps/roam/src/components/DiscourseContextOverlay.tsx b/apps/roam/src/components/DiscourseContextOverlay.tsx index f50e1db2f..e90ffedff 100644 --- a/apps/roam/src/components/DiscourseContextOverlay.tsx +++ b/apps/roam/src/components/DiscourseContextOverlay.tsx @@ -22,66 +22,39 @@ type DiscourseData = { }; const cache: { - [title: string]: DiscourseData; + [tag: string]: DiscourseData; } = {}; -const overlayQueue: { - tag: string; - callback: () => Promise; - start: number; - queued: number; - end: number; - mid: number; - id: string; -}[] = []; -const getOverlayInfo = (tag: string, id: string): Promise => { - if (cache[tag]) return Promise.resolve(cache[tag]); - const relations = getDiscourseRelations(); - const nodes = getDiscourseNodes(relations); +const getOverlayInfo = async (tag: string): Promise => { + try { + if (cache[tag]) return cache[tag]; + + const relations = getDiscourseRelations(); + const nodes = getDiscourseNodes(relations); - return new Promise((resolve) => { - const triggerNow = overlayQueue.length === 0; - overlayQueue.push({ - id, - start: 0, - end: 0, - mid: 0, - queued: new Date().valueOf(), - async callback() { - const self = this; - const start = (self.start = new Date().valueOf()); - // @ts-ignore - const queryResult = await window.roamAlphaAPI.data.backend.q( - `[:find ?a :where [?b :node/title "${normalizePageTitle( - tag, - )}"] [?a :block/refs ?b]]`, - ); - const refs = queryResult.length; - return getDiscourseContextResults({ - uid: getPageUidByPageTitle(tag), - nodes, - relations, - }).then(function resultCallback(results) { - self.mid = new Date().valueOf(); - const output = (cache[tag] = { - results, + const [results, refs] = await Promise.all([ + getDiscourseContextResults({ + uid: getPageUidByPageTitle(tag), + nodes, + relations, + }), + // @ts-ignore - backend to be added to roamjs-components + window.roamAlphaAPI.data.backend.q( + `[:find ?a :where [?b :node/title "${normalizePageTitle(tag)}"] [?a :block/refs ?b]]`, + ), + ]); - refs, - }); - const runTime = (self.end = new Date().valueOf() - start); - setTimeout(() => { - overlayQueue.splice(0, 1); - if (overlayQueue.length) { - overlayQueue[0].callback(); - } - }, runTime * 4); - resolve(output); - }); - }, - tag, + return (cache[tag] = { + results, + refs: refs.length, }); - if (triggerNow) overlayQueue[0].callback?.(); - }); + } catch (error) { + console.error(`Error getting overlay info for ${tag}:`, error); + return { + results: [], + refs: 0, + }; + } }; // const experimentalGetOverlayInfo = (title: string) => @@ -131,7 +104,7 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => { // localStorageGet("experimental") === "true" // ? experimentalGetOverlayInfo(tag) // : - getOverlayInfo(tag, id) + getOverlayInfo(tag) .then(({ refs, results }) => { const discourseNode = findDiscourseNode(tagUid); if (discourseNode) { From 1dc257d48d260ad69803480ea68be8f29d61f0eb Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sat, 12 Apr 2025 15:28:59 -0600 Subject: [PATCH 2/3] Remove experimental getOverlayInfo function --- .../components/DiscourseContextOverlay.tsx | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/apps/roam/src/components/DiscourseContextOverlay.tsx b/apps/roam/src/components/DiscourseContextOverlay.tsx index e90ffedff..af4b72ce8 100644 --- a/apps/roam/src/components/DiscourseContextOverlay.tsx +++ b/apps/roam/src/components/DiscourseContextOverlay.tsx @@ -57,32 +57,6 @@ const getOverlayInfo = async (tag: string): Promise => { } }; -// const experimentalGetOverlayInfo = (title: string) => -// Promise.all([ -// getDiscourseContextResults({ uid: getPageUidByPageTitle(title) }), -// fireWorkerQuery({ -// where: [ -// { -// type: "data-pattern", -// arguments: [ -// { type: "variable", value: "b" }, -// { type: "constant", value: ":node/title" }, -// { type: "constant", value: `"${title}"` }, -// ], -// }, -// { -// type: "data-pattern", -// arguments: [ -// { type: "variable", value: "a" }, -// { type: "constant", value: ":block/refs" }, -// { type: "variable", value: `b` }, -// ], -// }, -// ], -// pull: [], -// }), -// ]).then(([results, allrefs]) => ({ results, refs: allrefs.length })); - export const refreshUi: { [k: string]: () => void } = {}; const refreshAllUi = () => Object.entries(refreshUi).forEach(([k, v]) => { @@ -101,9 +75,6 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => { const [score, setScore] = useState(0); const getInfo = useCallback( () => - // localStorageGet("experimental") === "true" - // ? experimentalGetOverlayInfo(tag) - // : getOverlayInfo(tag) .then(({ refs, results }) => { const discourseNode = findDiscourseNode(tagUid); From ca2882f9c13c1fd79a44513cede092ae79e0a810 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sat, 12 Apr 2025 15:35:12 -0600 Subject: [PATCH 3/3] Remove unused refreshUi logic --- apps/roam/src/components/DiscourseContextOverlay.tsx | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/apps/roam/src/components/DiscourseContextOverlay.tsx b/apps/roam/src/components/DiscourseContextOverlay.tsx index af4b72ce8..e7e69a316 100644 --- a/apps/roam/src/components/DiscourseContextOverlay.tsx +++ b/apps/roam/src/components/DiscourseContextOverlay.tsx @@ -57,16 +57,6 @@ const getOverlayInfo = async (tag: string): Promise => { } }; -export const refreshUi: { [k: string]: () => void } = {}; -const refreshAllUi = () => - Object.entries(refreshUi).forEach(([k, v]) => { - if (document.getElementById(k)) { - v(); - } else { - delete refreshUi[k]; - } - }); - const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => { const tagUid = useMemo(() => getPageUidByPageTitle(tag), [tag]); const [loading, setLoading] = useState(true); @@ -102,7 +92,6 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => { getInfo(); }, [getInfo, setLoading]); useEffect(() => { - refreshUi[id] = refresh; getInfo(); }, [refresh, getInfo]); return (