@d3vtool/hooks
Version:
Collection of custom React hooks to simplify tasks in your React projects.
2 lines (1 loc) • 1.06 kB
JavaScript
var f=function(t,r){var n=typeof Symbol=="function"&&t[Symbol.iterator];if(!n)return t;var e=n.call(t),a,o=[],i;try{for(;(r===void 0||r-- >0)&&!(a=e.next()).done;)o.push(a.value)}catch(s){i={error:s}}finally{try{a&&!a.done&&(n=e.return)&&n.call(e)}finally{if(i)throw i.error}}return o};import{Hub as l}from"./Hub";import{useCallback as v,useEffect as u,useState as c}from"react";export function useHub(t){var r=f(c(t.getCurrentState()),2),n=r[0],e=r[1];u(function(){return t.attachListener(e),function(){t.detachListener(e)}},[t]);var a=v(function(o){var i=o instanceof Function?o(t.getCurrentState()):o;t.notifyListener(i)},[t]);return[n,a]}export function useReadHub(t){var r=f(c(t.getCurrentState()),2),n=r[0],e=r[1];return u(function(){return t.attachListener(e),function(){t.detachListener(e)}},[t]),n}export function useComputeHub(t,r){var n=new l(r(t.getCurrentState()));function e(){var a=t.getCurrentState(),o=r(a);n.notifyListener(o)}return u(function(){return t.onChange(e),function(){t.removeOnChange(e)}},[t]),n.getCurrentState()}
;