UNPKG

react-signify

Version:

A JS library for predictable and maintainable global state management

3 lines (2 loc) 5.97 kB
import e,{memo as t,useState as s,useRef as r,useLayoutEffect as i}from"react";function a(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 n(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 o={LocalStorage:()=>localStorage,SessionStorage:()=>sessionStorage},c=(e,t)=>{var s;(null==t?void 0:t.key)&&o[null!==(s=null==t?void 0:t.type)&&void 0!==s?s:"LocalStorage"]().setItem(t.key,JSON.stringify(e))},l=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)},h=(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(!h(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)&&h(e[s],t[s])))},f=e=>t=>(e.add(t),{unsubscribe:()=>e.delete(t)}),u=e=>(t,s)=>{i((()=>(e.add(t),()=>{e.delete(t)})),null!=s?s:[])},p=(e,t)=>a=>{const n=s({})[1],o=r((()=>{let e=null==a?void 0:a(t());return()=>{if(a){let s=a(t());if(h(e,s))return;e=s}n({})}})());return i((()=>(e.add(o.current),()=>{e.delete(o.current)})),[]),a?a(t()):t()},d=t=>e.createElement((()=>t())),v=e=>({children:t})=>t(e()),w=e=>t(v(e),(()=>!0));var y,g,b,m,O,S,k,M,j,E,W;class P{constructor(e,t){if(y.set(this,!0),g.set(this,void 0),b.set(this,void 0),m.set(this,void 0),O.set(this,new Set),S.set(this,new Set),k.set(this,void 0),M.set(this,void 0),j.set(this,void 0),E.set(this,((e=!0)=>{!a(this,y,"f")||a(this,j,"f")&&!a(this,j,"f").call(this,this.value)||a(this,O,"f").forEach((e=>e(this.value))),e&&a(this,S,"f").forEach((e=>e(this.value)))})),W.set(this,(e=>{var t,s;void 0!==e&&n(this,b,e,"f"),c(this.value,null===(t=a(this,m,"f"))||void 0===t?void 0:t.cache),null===(s=a(this,k,"f"))||void 0===s||s.call(this,this.value),a(this,E,"f").call(this)})),this.set=(e,t=!1)=>{let s;if("function"==typeof e){let r={value:t?a(this,b,"f"):l(a(this,b,"f"))};e(r),s=r.value}else s=e;!t&&(h(this.value,s)||a(this,M,"f")&&!a(this,M,"f").call(this,this.value,s))||a(this,W,"f").call(this,s)},this.stop=()=>{n(this,y,!1,"f")},this.resume=()=>{n(this,y,!0,"f"),a(this,E,"f").call(this,!1)},this.reset=()=>{a(this,W,"f").call(this,l(a(this,g,"f")))},this.conditionUpdating=e=>n(this,M,e,"f"),this.conditionRendering=e=>n(this,j,e,"f"),this.use=p(a(this,O,"f"),(()=>this.value)),this.watch=u(a(this,S,"f")),this.subscribe=f(a(this,S,"f")),this.html=d(this.use),this.Wrap=v(this.use),this.HardWrap=w(this.use),this.slice=e=>{let t,s=e(this.value),r=!0;const i=new Set,n=new Set,o=(a=!0)=>{const o=e(this.value);!r||t&&!t(o)||(s=o,i.forEach((e=>e(o)))),a&&n.forEach((e=>e(o)))},c=p(i,(()=>s)),l={value:s,use:c,watch:u(n),html:d(c),Wrap:v(c),HardWrap:w(c),stop:()=>r=!1,resume:()=>{r=!0,o(!1)},conditionRendering:e=>t=e,subscribe:f(n)};return a(this,O,"f").add((()=>{h(e(this.value),s)||o()})),Object.defineProperty(l,"value",{get:()=>s,enumerable:!1,configurable:!1}),l},n(this,g,e,"f"),n(this,b,((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 r=null!==(s=null==t?void 0:t.type)&&void 0!==s?s:"LocalStorage",i=o[r]().getItem(t.key);if(i)return JSON.parse(i);o[r]().setItem(t.key,JSON.stringify(e))}return e})(l(e),null==t?void 0:t.cache),"f"),n(this,m,t,"f"),null==t?void 0:t.syncKey){const{post:e,sync:s}=(({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:t.syncKey,cb:e=>{var t;n(this,b,e,"f"),c(this.value,null===(t=a(this,m,"f"))||void 0===t?void 0:t.cache),a(this,E,"f").call(this)}});n(this,k,e,"f"),s((()=>this.value))}}get value(){return a(this,b,"f")}}y=new WeakMap,g=new WeakMap,b=new WeakMap,m=new WeakMap,O=new WeakMap,S=new WeakMap,k=new WeakMap,M=new WeakMap,j=new WeakMap,E=new WeakMap,W=new WeakMap;const R=(e,t)=>new P(e,t);export{R as signify}; //# sourceMappingURL=index.js.map