jotai
Version:
👻 Next gen state management that will spook you
2 lines (1 loc) • 5.85 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("jotai/react")):"function"==typeof define&&define.amd?define(["exports","react","jotai/react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiDevtools={},e.React,e.react)}(this,(function(e,t,n){"use strict";function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},r.apply(this,arguments)}function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function u(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return o(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(e){return e.debugLabel||e.toString()},i=function(e){var t=e[0],n=e[1];return Object.fromEntries(n.flatMap((function(e){var n=null==t.dev_get_mounted?void 0:t.dev_get_mounted(e);if(!n)return[];var o=n.t,u=(null==t.dev_get_atom_state?void 0:t.dev_get_atom_state(e))||{};return[[a(e),r({},"e"in u&&{error:u.e},"v"in u&&{value:u.v},{dependents:Array.from(o).map(a)})]]})))};function s(e){var r=n.useStore(e),o=t.useState((function(){return{values:new Map,dependents:new Map}})),a=o[0],i=o[1];return t.useEffect((function(){if(r.dev_subscribe_state){var e=new Map,t=new Map,n=function(){for(var n,o,a,s=new Map,c=new Map,l=u(r.dev_get_mounted_atoms()||[]);!(n=l()).done;){var f=n.value,d=r.dev_get_atom_state(f);d&&"v"in d&&s.set(f,d.v);var v=r.dev_get_mounted(f);v&&c.set(f,v.t)}(a=s,(o=e).size===a.size&&Array.from(o).every((function(e){var t=e[0],n=e[1];return Object.is(a.get(t),n)}))&&function(e,t){return e.size===t.size&&Array.from(e).every((function(e){var n=e[0],r=e[1],o=t.get(n);return o&&r.size===o.size&&Array.from(r).every((function(e){return o.has(e)}))}))}(t,c))||(e=s,t=c,i({values:s,dependents:c}))},o=r.dev_subscribe_state(n);return n(),o}}),[r]),a}function c(e){var r=n.useStore(e);return t.useCallback((function(e){r.dev_restore_atoms&&r.dev_restore_atoms(e.values)}),[r])}var l=function(e){return e.debugLabel?e+":"+e.debugLabel:""+e},f=function(e){var t={};e.values.forEach((function(e,n){t[l(n)]=e}));var n={};return e.dependents.forEach((function(e,t){n[l(t)]=Array.from(e).map(l)})),{values:t,dependents:n}};e.useAtomDevtools=function(e,r){var o,u=r||{},a=u.enabled,i=u.name;try{o=null!=a&&a&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var s=n.useAtom(e),c=s[0],l=s[1],f=t.useRef(c),d=t.useRef(!1),v=t.useRef(),p=i||e.debugLabel||e.toString();t.useEffect((function(){if(o){var t=function(t){"function"!=typeof l?console.warn("[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n",e):l(t)};v.current=o.connect({name:p});var n=v.current.subscribe((function(e){var n,r;if("ACTION"===e.type&&e.payload)try{t(JSON.parse(e.payload))}catch(e){console.error("please dispatch a serializable value that JSON.parse() support\n",e)}else if("DISPATCH"===e.type&&e.state){var o,u;"JUMP_TO_ACTION"!==(null==(o=e.payload)?void 0:o.type)&&"JUMP_TO_STATE"!==(null==(u=e.payload)?void 0:u.type)||(d.current=!0,t(JSON.parse(e.state)))}else if("DISPATCH"===e.type&&"COMMIT"===(null==(n=e.payload)?void 0:n.type)){var a;null==(a=v.current)||a.init(f.current)}else if("DISPATCH"===e.type&&"IMPORT_STATE"===(null==(r=e.payload)?void 0:r.type)){var i;((null==(i=e.payload.nextLiftedState)?void 0:i.computedStates)||[]).forEach((function(e,n){var r,o=e.state;0===n?null==(r=v.current)||r.init(o):t(o)}))}}));return v.current.shouldInit=!0,n}}),[e,o,p,l]),t.useEffect((function(){v.current&&(f.current=c,v.current.shouldInit?(v.current.init(c),v.current.shouldInit=!1):d.current?d.current=!1:v.current.send(p+" - "+(new Date).toLocaleString(),c))}),[e,o,p,c])},e.useAtomsDebugValue=function(e){var r,o=null!=(r=null==e?void 0:e.enabled)&&r,u=n.useStore(e),a=t.useState([]),s=a[0],c=a[1];t.useEffect((function(){if(o){var e=function(){c(Array.from((null==u.dev_get_mounted_atoms?void 0:u.dev_get_mounted_atoms())||[]))},t=null==u.dev_subscribe_state?void 0:u.dev_subscribe_state(e);return e(),t}}),[o,u]),t.useDebugValue([u,s],i)},e.useAtomsDevtools=function(e,n){var r,o=(n||{}).enabled;try{r=null!=o&&o&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var u=s(),a=c(),i=t.useRef(!1),l=t.useRef(!0),d=t.useRef(),v=t.useRef([]);t.useEffect((function(){if(r){var t=function(e){void 0===e&&(e=v.current.length-1);var t=v.current[e>=0?e:0];if(!t)throw new Error("snaphost index out of bounds");return t},n=r.connect({name:e}),o=n.subscribe((function(e){var r;if("DISPATCH"===e.type)switch(null==(r=e.payload)?void 0:r.type){case"RESET":break;case"COMMIT":n.init(f(t())),v.current=[];break;case"JUMP_TO_ACTION":case"JUMP_TO_STATE":i.current=!0,a(t(e.payload.actionId-1));break;case"PAUSE_RECORDING":l.current=!l.current}}));return d.current=n,d.current.shouldInit=!0,function(){r.disconnect(),null==o||o()}}}),[r,a,e]),t.useEffect((function(){if(d.current)return d.current.shouldInit?(d.current.init(void 0),void(d.current.shouldInit=!1)):void(i.current?i.current=!1:l.current&&(v.current.push(u),d.current.send({type:""+v.current.length,updatedAt:(new Date).toLocaleString()},f(u))))}),[u])},e.useAtomsSnapshot=s,e.useGotoAtomsSnapshot=c}));