UNPKG

@leancodepl/hook-pipe-client

Version:

React hooks for real-time data streaming and subscriptions using @leancodepl/pipe

27 lines (26 loc) 790 B
import { useState as D, useRef as c, useEffect as E } from "react"; import T from "deep-equal"; import { share as $ } from "rxjs"; function w({ pipe: n }) { return { createTopic(u) { function o(t, { onData: i, ...s }) { const [m, a] = D(), p = c(i), f = c(s); p.current = i, f.current = s; const r = c(null); return (r.current === null || !T(r.current, t)) && (r.current = t), E(() => { const l = n.topic(u, r.current).pipe($()).subscribe((e) => { a(e), p.current?.(e); const [b, R] = e; f.current[b]?.(R); }); return () => l.unsubscribe(); }, [r.current]), { data: m }; } return o.topic = (t) => n.topic(u, t), o; } }; } export { w as mkPipeClient };