UNPKG

@cervello/react

Version:

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

3 lines (2 loc) • 5.06 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e||self).react={},e.react)}(this,function(e,n){var t=Symbol("nonReactiveObject");function r(){return r=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},r.apply(this,arguments)}function u(e){if(!e||"object"!=typeof e)return e;var n={};return Array.isArray(e)?n=e.map(function(e){return u(e)}):function(e){if(!i(e))return!1;if(o(e))return!0;var n=Object.keys(e);return n.includes("tag")&&n.includes("containerInfo")||n.some(function(e){return e.startsWith("__reactContainer")})||n.includes("tag")&&n.includes("stateNode")}(e)?n=e:(Object.entries(e).forEach(function(e){n[e[0]]=u(e[1])}),Object.getOwnPropertySymbols(e).forEach(function(t){n[t]=e[t]})),n}var i=function(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)},o=function(e){return!!i(e)&&!!e.$$typeof};function a(e){return i(e)&&!o(e)&&!e[t]}"hasOwn"in Object||(Object.hasOwn=Object.call.bind(Object.hasOwnProperty));var l=Symbol("value");function c(e,n,t){var i,o;void 0===t&&(t={});var s=null!=(i=t.nestedFieldPath)?i:"root",h=((o={})[l]=n,o),v="root"===s?Object.fromEntries(Object.entries(n).filter(function(e){return"function"==typeof e[1]})):{};return new Proxy(h,{get:function(n,r,i){if("_$fieldPath"===r)return s;var o=Object.hasOwn(n,l)?n[l]:n;if("toJSON"===r)return function(){return f(o)};if("$value"===r)return u(o);var h=Reflect.get(o,r,i);if("symbol"==typeof r)return h;if("function"==typeof h)return h.bind(i);if(a(h)&&h!==o){var v;if(h._$fieldPath)return h;var b=c(e,h,{nestedFieldPath:s+"."+r,parentObjectToProxify:null!=(v=t.parentObjectToProxify)?v:o,afterChange:t.afterChange});return o[r]=b}return h},set:function(n,u,i,o){var c,h;if("symbol"==typeof u)return!0;var b=i,p=Object.hasOwn(n,l);if(p&&"$$value"===u){var d=n[l];return n[l]=b.newValue,e.next({storeValue:b.newValue,change:{fieldPath:"root",newValue:b.newValue,previousValue:d}},b.id),!0}if(p&&"$value"===u){var y=n[l];return b===y||JSON.stringify(f(b))===JSON.stringify(f(y))||("root"!==s?n[l]=b:(n[l]=r({},v,b),e.next({storeValue:n[l],change:{fieldPath:"root",newValue:b,previousValue:y}}))),!0}var g=p?n[l]:n,O=Reflect.get(g,u,o);if(O===b)return!0;a(b)&&null!=O&&O._$fieldPath?O.$value=b:Reflect.set(g,u,b,g);var j={storeValue:null!=(c=null==(h=t)?void 0:h.parentObjectToProxify)?c:g,change:{fieldPath:(s+"."+u).replace("root.",""),newValue:b,previousValue:O}};return e.next(j),null==t.afterChange||t.afterChange([j]),!0},has:function(e,n){return Reflect.has(e[l],n)},ownKeys:function(e){return Reflect.ownKeys(e[l])},getOwnPropertyDescriptor:function(e,n){return Reflect.getOwnPropertyDescriptor(e[l],n)}})}function f(e){var n={};return"object"!=typeof e||null==e?e:Array.isArray(e)?e.map(function(e){return f(e)}):o(e)?{props:f(e.props),type:"string"==typeof e.type?e.type:""}:null!=globalThis&&globalThis.HTMLElement&&e instanceof globalThis.HTMLElement?{type:"[HTMLElement]",content:e.innerHTML}:(Object.entries(e).forEach(function(e){n[e[0]]=f(e[1])}),n)}e.cervello=function(e,t){var r,i,o,a=(null!=t?t:{}).afterChange,l=u(e),f=(r=!1,i=[],o=[],{next:function(e,n){i.push({newValue:e,subscriberId:n}),r||(r=!0,Promise.resolve().then(function(){o.forEach(function(e){var n=i.filter(function(n){return n.subscriberId!==e.id});n.length&&e.next(n.map(function(e){return e.newValue}))}),i=[],r=!1}))},subscribe:function(e){return o.push(e),{unsubscribe:function(){o=o.filter(function(n){return n!==e})}}}}),s=c(f,l,{afterChange:a});return{store:s,reset:function(){s.$value=u(e)},useStore:function(e){var t,r=n.useId(),u=null==e||null==e.initialValue?void 0:e.initialValue(s.$value),i=n.useRef(!1),o=n.useState(0)[1];!i.current&&u&&u!==s&&(i.current=!0,s.$$value={id:r,newValue:u});var a=n.useRef(null!=(t="function"==typeof(null==e?void 0:e.select)?null==e||null==e.select?void 0:e.select():null==e?void 0:e.select)?t:[]),l=n.useRef(a.current.filter(function(e){return e.includes(".*")}).map(function(e){return e.replace(".*","")})),c=function(){o(function(e){return e+1})};return n.useEffect(function(){null!=e&&e.setValueOnMount&&e.setValueOnMount(s.$value).then(function(e){s.$value=e}).catch(function(e){console.error("Error setting initial value on mount",e)})},[]),n.useEffect(function(){var n=f.subscribe({id:r,next:function(n){var t=Array.isArray(n)?n:[n];if(null==e||!e.select)return c(),void(null==e||null==e.onChange||e.onChange(t));t.some(function(e){return"root"===e.change.fieldPath||a.current.includes(e.change.fieldPath)||l.current.find(function(n){return e.change.fieldPath.startsWith(n)})})&&(c(),null==e||null==e.onChange||e.onChange(t))}});return function(){n.unsubscribe()}},[null==e?void 0:e.select,null==e?void 0:e.onChange]),s}}},e.nonReactive=function(e){return Object.defineProperty(e,t,{value:!0,enumerable:!1,writable:!1,configurable:!1}),e}}); //# sourceMappingURL=cervello.umd.js.map