@@ -22,104 +22,41 @@ type DiscourseData = {
2222} ;
2323
2424const cache : {
25- [ title : string ] : DiscourseData ;
25+ [ tag : string ] : DiscourseData ;
2626} = { } ;
27- const overlayQueue : {
28- tag : string ;
29- callback : ( ) => Promise < void > ;
30- start : number ;
31- queued : number ;
32- end : number ;
33- mid : number ;
34- id : string ;
35- } [ ] = [ ] ;
3627
37- const getOverlayInfo = ( tag : string , id : string ) : Promise < DiscourseData > => {
38- if ( cache [ tag ] ) return Promise . resolve ( cache [ tag ] ) ;
39- const relations = getDiscourseRelations ( ) ;
40- const nodes = getDiscourseNodes ( relations ) ;
28+ const getOverlayInfo = async ( tag : string ) : Promise < DiscourseData > => {
29+ try {
30+ if ( cache [ tag ] ) return cache [ tag ] ;
31+
32+ const relations = getDiscourseRelations ( ) ;
33+ const nodes = getDiscourseNodes ( relations ) ;
4134
42- return new Promise ( ( resolve ) => {
43- const triggerNow = overlayQueue . length === 0 ;
44- overlayQueue . push ( {
45- id,
46- start : 0 ,
47- end : 0 ,
48- mid : 0 ,
49- queued : new Date ( ) . valueOf ( ) ,
50- async callback ( ) {
51- const self = this ;
52- const start = ( self . start = new Date ( ) . valueOf ( ) ) ;
53- // @ts -ignore
54- const queryResult = await window . roamAlphaAPI . data . backend . q (
55- `[:find ?a :where [?b :node/title "${ normalizePageTitle (
56- tag ,
57- ) } "] [?a :block/refs ?b]]`,
58- ) ;
59- const refs = queryResult . length ;
60- return getDiscourseContextResults ( {
61- uid : getPageUidByPageTitle ( tag ) ,
62- nodes,
63- relations,
64- } ) . then ( function resultCallback ( results ) {
65- self . mid = new Date ( ) . valueOf ( ) ;
66- const output = ( cache [ tag ] = {
67- results,
35+ const [ results , refs ] = await Promise . all ( [
36+ getDiscourseContextResults ( {
37+ uid : getPageUidByPageTitle ( tag ) ,
38+ nodes,
39+ relations,
40+ } ) ,
41+ // @ts -ignore - backend to be added to roamjs-components
42+ window . roamAlphaAPI . data . backend . q (
43+ `[:find ?a :where [?b :node/title "${ normalizePageTitle ( tag ) } "] [?a :block/refs ?b]]` ,
44+ ) ,
45+ ] ) ;
6846
69- refs,
70- } ) ;
71- const runTime = ( self . end = new Date ( ) . valueOf ( ) - start ) ;
72- setTimeout ( ( ) => {
73- overlayQueue . splice ( 0 , 1 ) ;
74- if ( overlayQueue . length ) {
75- overlayQueue [ 0 ] . callback ( ) ;
76- }
77- } , runTime * 4 ) ;
78- resolve ( output ) ;
79- } ) ;
80- } ,
81- tag,
47+ return ( cache [ tag ] = {
48+ results,
49+ refs : refs . length ,
8250 } ) ;
83- if ( triggerNow ) overlayQueue [ 0 ] . callback ?.( ) ;
84- } ) ;
51+ } catch ( error ) {
52+ console . error ( `Error getting overlay info for ${ tag } :` , error ) ;
53+ return {
54+ results : [ ] ,
55+ refs : 0 ,
56+ } ;
57+ }
8558} ;
8659
87- // const experimentalGetOverlayInfo = (title: string) =>
88- // Promise.all([
89- // getDiscourseContextResults({ uid: getPageUidByPageTitle(title) }),
90- // fireWorkerQuery({
91- // where: [
92- // {
93- // type: "data-pattern",
94- // arguments: [
95- // { type: "variable", value: "b" },
96- // { type: "constant", value: ":node/title" },
97- // { type: "constant", value: `"${title}"` },
98- // ],
99- // },
100- // {
101- // type: "data-pattern",
102- // arguments: [
103- // { type: "variable", value: "a" },
104- // { type: "constant", value: ":block/refs" },
105- // { type: "variable", value: `b` },
106- // ],
107- // },
108- // ],
109- // pull: [],
110- // }),
111- // ]).then(([results, allrefs]) => ({ results, refs: allrefs.length }));
112-
113- export const refreshUi : { [ k : string ] : ( ) => void } = { } ;
114- const refreshAllUi = ( ) =>
115- Object . entries ( refreshUi ) . forEach ( ( [ k , v ] ) => {
116- if ( document . getElementById ( k ) ) {
117- v ( ) ;
118- } else {
119- delete refreshUi [ k ] ;
120- }
121- } ) ;
122-
12360const DiscourseContextOverlay = ( { tag, id } : { tag : string ; id : string } ) => {
12461 const tagUid = useMemo ( ( ) => getPageUidByPageTitle ( tag ) , [ tag ] ) ;
12562 const [ loading , setLoading ] = useState ( true ) ;
@@ -128,10 +65,7 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
12865 const [ score , setScore ] = useState < number | string > ( 0 ) ;
12966 const getInfo = useCallback (
13067 ( ) =>
131- // localStorageGet("experimental") === "true"
132- // ? experimentalGetOverlayInfo(tag)
133- // :
134- getOverlayInfo ( tag , id )
68+ getOverlayInfo ( tag )
13569 . then ( ( { refs, results } ) => {
13670 const discourseNode = findDiscourseNode ( tagUid ) ;
13771 if ( discourseNode ) {
@@ -158,7 +92,6 @@ const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
15892 getInfo ( ) ;
15993 } , [ getInfo , setLoading ] ) ;
16094 useEffect ( ( ) => {
161- refreshUi [ id ] = refresh ;
16295 getInfo ( ) ;
16396 } , [ refresh , getInfo ] ) ;
16497 return (
0 commit comments