Skip to content

Commit

Permalink
fix minilinks sub
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansglazunov committed Nov 18, 2023
1 parent d7b3854 commit 409879d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions imports/minilinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,28 +641,25 @@ export function useMinilinksQuery<L extends Link<number>>(ml, query: QueryLink |
* Recalculates when data in minilinks changes. (Take query into useMemo!).
*/
export function useMinilinksSubscription<L extends Link<number>>(ml, query: QueryLink | number) {
const observerRef = useRef<any>();
const [d, setD] = useState();
const qRef = useRef<any>(query);
qRef.current = query;
console.log('useMinilinksSubscription', 'render', d, query);
const sRef = useRef<any>();
const qPrevRef = useRef<any>(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);
};

0 comments on commit 409879d

Please sign in to comment.