diff --git a/imports/minilinks.ts b/imports/minilinks.ts index c8b5fc4b..57189cc2 100644 --- a/imports/minilinks.ts +++ b/imports/minilinks.ts @@ -641,28 +641,25 @@ export function useMinilinksQuery>(ml, query: QueryLink | * Recalculates when data in minilinks changes. (Take query into useMemo!). */ export function useMinilinksSubscription>(ml, query: QueryLink | number) { - const observerRef = useRef(); const [d, setD] = useState(); - const qRef = useRef(query); - qRef.current = query; - console.log('useMinilinksSubscription', 'render', d, query); + const sRef = useRef(); + const qPrevRef = useRef(query); + const q = useMemo(() => _isEqual(query, qPrevRef.current) ? qPrevRef.current : query, [query]); + qPrevRef.current = q; useEffect(() => { - !!observerRef.current && observerRef.current.unsubscribe(); - const obs = observerRef.current = ml.subscribe(qRef.current); - console.log('useMinilinksSubscription', 'useEffect', obs); - const sub = obs.subscribe({ + if (sRef.current) sRef.current.unsubscribe(); + const obs = ml.subscribe(q); + const sub = sRef.current = obs.subscribe({ next: (links) => { - console.log('useMinilinksSubscription', 'next', links); setD(links); }, error: (error) => { - console.log('useMinilinksSubscription', 'error', error); throw new Error(error); }, }); return () => { sub.unsubscribe(); } - }, []); + }, [q]); return d || ml.query(query); }; \ No newline at end of file