react-use-tools
Version:
A small library to manage global reactive tools across your React application.
3 lines (2 loc) • 969 B
JavaScript
import{useState as e,useMemo as t,useEffect as r,useRef as c}from"react";function n(){var e,t;return((new Date).getTime()+(e=0,t=1e8,Math.floor(Math.random()*(t-e+1))+e)).toString(36)}function o(c){return(o,a)=>{const[s,u]=e(0),i=t(()=>function(e){const t=new Set(e);let r;do{r=n()}while(t.has(r));return r}(c.watchers.current.keys()),[]);r(()=>()=>{c.tracks.current.delete(i),c.watchers.current.delete(i)},[]),r(()=>{c.watchers.current.set(i,{tracker:o??(e=>e),callback(e){(void 0===a?.reRender||a.reRender)&&u(e=>e+1),a?.callback?.(e)}})},[o,a])}}function a(e){window.tools=function(e){const n=c(new Map),a=c(new Map),s=t(()=>o({watchers:n,tracks:a}),[]);return r(()=>{n.current.forEach((t,r)=>{const c=t.tracker(e);a.current.get(r)!==c&&(a.current.set(r,c),t.callback(c))})},[e]),t(()=>{const t=Object.create(Object.getPrototypeOf(e),Object.getOwnPropertyDescriptors(e));return t.watch=s,t},[e])}(e)}export{a as registerTools};
//# sourceMappingURL=index.esm.js.map