react-signify
Version:
A JS library for predictable and maintainable global state management
3 lines (2 loc) • 5.97 kB
JavaScript
"use strict";var e=require("react");function t(e,t,s,r){if("a"===s&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?r:"a"===s?r.call(e):r?r.value:t.get(e)}function s(e,t,s,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?i.call(e,s):i?i.value=s:t.set(e,s),s}"function"==typeof SuppressedError&&SuppressedError;const r={LocalStorage:()=>localStorage,SessionStorage:()=>sessionStorage},i=(e,t)=>{var s;(null==t?void 0:t.key)&&r[null!==(s=null==t?void 0:t.type)&&void 0!==s?s:"LocalStorage"]().setItem(t.key,JSON.stringify(e))},a=e=>{if("object"!=typeof e||null===e)return e;const t=new WeakMap,s=new Map([[Date,e=>new Date(e.getTime())],[RegExp,e=>new RegExp(e.source,e.flags)],[Map,(e,t)=>new Map([...e].map((([e,s])=>[t(e),t(s)])))],[Set,(e,t)=>new Set([...e].map(t))],[Error,(e,t)=>Object.assign(new e.constructor(e.message),{stack:e.stack,cause:e.cause&&t(e.cause)})],[URL,e=>new URL(e.href)],[Blob,e=>e.slice()],[File,e=>new File([e],e.name,{type:e.type,lastModified:e.lastModified})]]),r=e=>{if("object"!=typeof e||null===e)return e;if(t.has(e))return t.get(e);let i;if(Array.isArray(e))i=e.map(r);else if(ArrayBuffer.isView(e))i=e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length);else{if(!s.has(e.constructor))return i=Object.create(Object.getPrototypeOf(e)),t.set(e,i),[...Object.getOwnPropertyNames(e),...Object.getOwnPropertySymbols(e)].forEach((t=>{const s=Object.getOwnPropertyDescriptor(e,t);void 0!==s.value&&(s.value=r(s.value)),Object.defineProperty(i,t,s)})),i;i=s.get(e.constructor)(e,r)}return t.set(e,i),i};return r(e)},n=(e,t)=>{if(e===t)return!0;if(!e||!t||"object"!=typeof e||"object"!=typeof t)return e!=e&&t!=t;if(e.constructor!==t.constructor)return!1;if(Array.isArray(e)){if(e.length!==t.length)return!1;for(let s=e.length;s--;)if(!n(e[s],t[s]))return!1;return!0}if(e.constructor===RegExp)return e.source===t.source&&e.flags===t.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===t.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===t.toString();const s=Object.keys(e);return s.length===Object.keys(t).length&&s.every((s=>Object.prototype.hasOwnProperty.call(t,s)&&n(e[s],t[s])))},o=e=>t=>(e.add(t),{unsubscribe:()=>e.delete(t)}),c=t=>(s,r)=>{e.useLayoutEffect((()=>(t.add(s),()=>{t.delete(s)})),null!=r?r:[])},l=(t,s)=>r=>{const i=e.useState({})[1],a=e.useRef((()=>{let e=null==r?void 0:r(s());return()=>{if(r){let t=r(s());if(n(e,t))return;e=t}i({})}})());return e.useLayoutEffect((()=>(t.add(a.current),()=>{t.delete(a.current)})),[]),r?r(s()):s()},h=t=>e.createElement((()=>t())),f=e=>({children:t})=>t(e()),u=t=>e.memo(f(t),(()=>!0));var p,d,v,w,y,g,b,m,S,O,k;class E{constructor(e,E){if(p.set(this,!0),d.set(this,void 0),v.set(this,void 0),w.set(this,void 0),y.set(this,new Set),g.set(this,new Set),b.set(this,void 0),m.set(this,void 0),S.set(this,void 0),O.set(this,((e=!0)=>{!t(this,p,"f")||t(this,S,"f")&&!t(this,S,"f").call(this,this.value)||t(this,y,"f").forEach((e=>e(this.value))),e&&t(this,g,"f").forEach((e=>e(this.value)))})),k.set(this,(e=>{var r,a;void 0!==e&&s(this,v,e,"f"),i(this.value,null===(r=t(this,w,"f"))||void 0===r?void 0:r.cache),null===(a=t(this,b,"f"))||void 0===a||a.call(this,this.value),t(this,O,"f").call(this)})),this.set=(e,s=!1)=>{let r;if("function"==typeof e){let i={value:s?t(this,v,"f"):a(t(this,v,"f"))};e(i),r=i.value}else r=e;!s&&(n(this.value,r)||t(this,m,"f")&&!t(this,m,"f").call(this,this.value,r))||t(this,k,"f").call(this,r)},this.stop=()=>{s(this,p,!1,"f")},this.resume=()=>{s(this,p,!0,"f"),t(this,O,"f").call(this,!1)},this.reset=()=>{t(this,k,"f").call(this,a(t(this,d,"f")))},this.conditionUpdating=e=>s(this,m,e,"f"),this.conditionRendering=e=>s(this,S,e,"f"),this.use=l(t(this,y,"f"),(()=>this.value)),this.watch=c(t(this,g,"f")),this.subscribe=o(t(this,g,"f")),this.html=h(this.use),this.Wrap=f(this.use),this.HardWrap=u(this.use),this.slice=e=>{let s,r=e(this.value),i=!0;const a=new Set,p=new Set,d=(t=!0)=>{const n=e(this.value);!i||s&&!s(n)||(r=n,a.forEach((e=>e(n)))),t&&p.forEach((e=>e(n)))},v=l(a,(()=>r)),w={value:r,use:v,watch:c(p),html:h(v),Wrap:f(v),HardWrap:u(v),stop:()=>i=!1,resume:()=>{i=!0,d(!1)},conditionRendering:e=>s=e,subscribe:o(p)};return t(this,y,"f").add((()=>{n(e(this.value),r)||d()})),Object.defineProperty(w,"value",{get:()=>r,enumerable:!1,configurable:!1}),w},s(this,d,e,"f"),s(this,v,((e,t)=>{var s;if(null==t?void 0:t.key){if("undefined"==typeof window)throw new Error("The cache feature is not recommended for Server-Side Rendering. Please remove the cache properties from the Signify variable.");const i=null!==(s=null==t?void 0:t.type)&&void 0!==s?s:"LocalStorage",a=r[i]().getItem(t.key);if(a)return JSON.parse(a);r[i]().setItem(t.key,JSON.stringify(e))}return e})(a(e),null==E?void 0:E.cache),"f"),s(this,w,E,"f"),null==E?void 0:E.syncKey){const{post:e,sync:r}=(({key:e,cb:t})=>{const s=`bc_${e}`,r=new BroadcastChannel(s);return r.onmessage=e=>t(e.data),{post:e=>{r.postMessage(e)},sync:e=>{const t=new BroadcastChannel("bcs");t.onmessage=t=>s===t.data&&r.postMessage(e()),t.postMessage(s)}}})({key:E.syncKey,cb:e=>{var r;s(this,v,e,"f"),i(this.value,null===(r=t(this,w,"f"))||void 0===r?void 0:r.cache),t(this,O,"f").call(this)}});s(this,b,e,"f"),r((()=>this.value))}}get value(){return t(this,v,"f")}}p=new WeakMap,d=new WeakMap,v=new WeakMap,w=new WeakMap,y=new WeakMap,g=new WeakMap,b=new WeakMap,m=new WeakMap,S=new WeakMap,O=new WeakMap,k=new WeakMap,exports.signify=(e,t)=>new E(e,t);
//# sourceMappingURL=index.js.map