@anew/hooks
Version:
AnewJS React Hooks
2 lines (1 loc) • 2.46 kB
JavaScript
import{useDebugValue as n,useReducer as t,useMemo as r,useRef as e,useLayoutEffect as u,useEffect as i}from"react";function c(n,t){return n===t?0!==n||0!==t||1/n==1/t:n!=n&&t!=t}function s(n,t){if(c(n,t))return!0;if("object"!=typeof n||null===n||"object"!=typeof t||null===t)return!1;var r=Object.keys(n);if(r.length!==Object.keys(t).length)return!1;for(var e=0;r.length>e;e++)if(!Object.prototype.hasOwnProperty.call(t,r[e])||!c(n[r[e]],t[r[e]]))return!1;return!0}var o=function(n){n()},f=function(){return o},a={notify:function(){}};var l=function(){function n(n){var t=this;this.store=n,this.unsubscribe=null,this.listeners=a,this.handleChangeWrapper=function(){t.onStateChange&&t.onStateChange()}}var t=n.prototype;return t.notifyNestedSubs=function(){this.listeners.notify()},t.isSubscribed=function(){return!!this.unsubscribe},t.trySubscribe=function(){this.unsubscribe||(this.unsubscribe=this.store.subscribe(this.handleChangeWrapper),this.listeners=function(){var n=f(),t=null,r=null;return{clear:function(){t=null,r=null},notify:function(){n((function(){for(var n=t;n;)n.callback(),n=n.next}))},get:function(){for(var n=[],r=t;r;)n.push(r),r=r.next;return n},subscribe:function(n){var e=!0,u=r={callback:n,next:null,prev:r};return u.prev?u.prev.next=u:t=u,function(){e&&null!==t&&(e=!1,u.next?u.next.prev=u.prev:r=u.prev,u.prev?u.prev.next=u.next:t=u.next)}}}}())},t.tryUnsubscribe=function(){this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=null,this.listeners&&this.listeners.clear&&this.listeners.clear(),this.listeners=a)},n}(),h=function(n,t){return n===t},b="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?u:i;function v(u){function i(i,c){void 0===c&&(c=h);var s=function(n,u,i){var c,s=t((function(n){return n+1}),0)[1],o=r((function(){return new l(n)}),[n]),f=e(),a=e(),h=e(),v=e(),p=n.state;try{c=u!==a.current||p!==h.current||f.current?u(p):v.current}catch(n){throw f.current&&(n.message+="\nThe error may be correlated with this previous error:\n"+f.current.stack+"\n\n"),n}return b((function(){a.current=u,h.current=p,v.current=c,f.current=void 0})),b((function(){function t(){try{var t=a.current(n.state);if(i(t,v.current))return;v.current=t}catch(n){f.current=n}s()}return o.onStateChange=t,o.trySubscribe(),t(),function(){return o.tryUnsubscribe()}}),[n,o]),c}(u,i,c);return n(s),s}return i.withSallowEqual=function(n){return i(n,s)},i}export{v as createUseStoreState,s as shallowEqual};