UNPKG

@cervello/react

Version:

🤯 Simple, reactive, tiny and performant state-management library

3 lines (2 loc) • 5.14 kB
import{isValidElement as e,useRef as n,useLayoutEffect as t,useSyncExternalStore as r}from"react";var u=Symbol("nonReactiveObject");function o(){return o=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},o.apply(this,arguments)}var a=function(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)};"hasOwn"in Object||(Object.hasOwn=Object.call.bind(Object.hasOwnProperty));var i=Symbol("value");function l(n,t,r){var s,v;void 0===r&&(r={});var p=null!=(s=r.nestedFieldPath)?s:"root",h=((v={})[i]=t,v),b="root"===p?Object.fromEntries(Object.entries(t).filter(function(e){return"function"==typeof e[1]})):{};return new Proxy(h,{get:function(t,o,c){if("_$fieldPath"===o)return p;var s=Object.hasOwn(t,i)?t[i]:t;if("toJSON"===o)return function(){return f(s)};if("$value"===o)return s;var v,h=Reflect.get(s,o,c);if("symbol"==typeof o)return h;if("function"==typeof h)return h.bind(c);if(a(v=h)&&!e(v)&&!v[u]&&h!==s){var b,y=p+"."+o;if(h._$fieldPath===y)return h;var O=l(n,h,{nestedFieldPath:y,parentObjectToProxify:null!=(b=r.parentObjectToProxify)?b:s,afterChange:r.afterChange});return s[o]=O}return h},set:function(t,l,f,s){var v,h;if("symbol"==typeof l)return!0;var y=f,O=Object.hasOwn(t,i);if(O&&"$$value"===l)return t[i]=y,!0;if(O&&"$value"===l){var g=t[i];return y===g||c(y)===c(g)||("root"!==p?t[i]=y:(t[i]=o({},b,y),n.next({storeValue:t[i],change:{fieldPath:"root",newValue:y,previousValue:g}}))),!0}var d=O?t[i]:t,j=Reflect.get(d,l,s);if(j===y)return!0;a(y)&&!e(y)&&!y[u]&&null!=j&&j._$fieldPath?j.$value=y:Reflect.set(d,l,y,d);var w={storeValue:null!=(v=null==(h=r)?void 0:h.parentObjectToProxify)?v:d,change:{fieldPath:(p+"."+l).replace("root.",""),newValue:y,previousValue:j}};return n.next(w),null==r.afterChange||r.afterChange([w]),!0},has:function(e,n){return Reflect.has(e[i],n)},ownKeys:function(e){return Reflect.ownKeys(e[i])},getOwnPropertyDescriptor:function(e,n){return Reflect.getOwnPropertyDescriptor(e[i],n)}})}function c(n){var t=f(n);return JSON.stringify(t,function(n,t){return e(t)?{props:c(t.props),type:t.type}:t})}function f(e){var n=new WeakSet;return function e(t){if(t&&"object"==typeof t){if(n.has(t))return null;if(n.add(t),Array.isArray(t))return t.map(e);var r={};for(var u in t)Object.prototype.hasOwnProperty.call(t,u)&&(r[u]=e(t[u]));return r}return t}(e)}function s(e){return Object.defineProperty(e,u,{value:!0,enumerable:!1,writable:!1,configurable:!1}),e}function v(e,u){var o=(null!=u?u:{}).afterChange,a=e,i=function(e,n){void 0===n&&(n=!1);var t=!1,r=[],u=[],o=e;return{getValue:function(){return o},next:function(e){o=e,r.push(e),t||(t=!0,Promise.resolve().then(function(){u.forEach(function(e){e.next(r)}),r=[],t=!1}))},subscribe:function(e){return n&&e.next(o),u.push(e),{unsubscribe:function(){u=u.filter(function(n){return n!==e})}}},unSubscribeAll:function(){u=[]}}}(a),c=l(i,a,{afterChange:o});return{store:c,reset:function(){c.$value=function(e){var n=[],t=[],r=new Map;r.set(Date,function(e){return new Date(e)}),r.set(Map,function(e,n){return new Map(o(Array.from(e),n))}),r.set(Set,function(e,n){return new Set(o(Array.from(e),n))});var u=null;return function e(a){if("object"!=typeof a||null===a)return a;if(Array.isArray(a))return o(a,e);if(a.constructor!==Object&&(u=r.get(a.constructor)))return u(a,e);var i={};for(var l in n.push(a),t.push(i),Object.getOwnPropertySymbols(a).forEach(function(e){i[e]=a[e]}),a)if(Object.hasOwnProperty.call(a,l)){var c=a[l];if("object"!=typeof c||null===c)i[l]=c;else if(c.constructor!==Object&&(u=r.get(c.constructor)))i[l]=u(c,e);else{var f=n.indexOf(c);i[l]=-1!==f?t[f]:e(c)}}return n.pop(),t.pop(),i}(e);function o(e,o){for(var a=Object.keys(e),i=new Array(a.length),l=0;l<a.length;l++){var c=a[l],f=e[c];if("object"!=typeof f||null===f)i[c]=f;else if(f.constructor!==Object&&(u=r.get(f.constructor)))i[c]=u(f,o);else{var s=n.indexOf(f);i[c]=-1!==s?t[s]:o(f)}}return i}}(e)},useStore:function(e){var u,o=null==e||null==e.initialValue?void 0:e.initialValue(c.$value),a=n(!1);!a.current&&o&&o!==c&&(a.current=!0,c.$value=o);var l=n(null!=(u="function"==typeof(null==e?void 0:e.select)?null==e||null==e.select?void 0:e.select():null==e?void 0:e.select)?u:[]),f=n(l.current.filter(function(e){return e.includes(".*")}).map(function(e){return e.replace(".*","")}));return t(function(){null!=e&&e.setValueOnMount&&e.setValueOnMount(c.$value).then(function(e){c.$value=e}).catch(function(e){console.error("Error setting initial value on mount",e)})},[]),r(function(n){var t=i.subscribe({next:function(t){var r=Array.isArray(t)?t:[t];if(null==e||!e.select)return n(),void(null==e||null==e.onChange||e.onChange(r));r.some(function(e){return"root"===e.change.fieldPath||l.current.includes(e.change.fieldPath)||f.current.find(function(n){return e.change.fieldPath.startsWith(n)})})&&(n(),null==e||null==e.onChange||e.onChange(r))}});return function(){t.unsubscribe()}},function(){return i.getValue()},function(){return i.getValue()}),c}}}export{v as cervello,s as nonReactive}; //# sourceMappingURL=cervello.esm.js.map