UNPKG

5.84 kBJavaScriptView Raw
1!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 u(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 o(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 u(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)?u(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 u=n.t,o=(null==t.dev_get_atom_state?void 0:t.dev_get_atom_state(e))||{};return[[a(e),r({},"e"in o&&{error:o.e},"v"in o&&{value:o.v},{dependents:Array.from(u).map(a)})]]})))};function c(e){var r=n.useStore(e),u=t.useState((function(){return{values:new Map,dependents:new Map}})),a=u[0],i=u[1];return t.useEffect((function(){if(r.dev_subscribe_state){var e=new Map,t=new Map,n=function(){for(var n,u,a,c=new Map,s=new Map,l=o(r.dev_get_mounted_atoms()||[]);!(n=l()).done;){var f=n.value,d=r.dev_get_atom_state(f);d&&"v"in d&&c.set(f,d.v);var v=r.dev_get_mounted(f);v&&s.set(f,v.t)}(a=c,(u=e).size===a.size&&Array.from(u).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],u=t.get(n);return u&&r.size===u.size&&Array.from(r).every((function(e){return u.has(e)}))}))}(t,s))||(e=c,t=s,i({values:c,dependents:s}))},u=r.dev_subscribe_state(n);return n(),u}}),[r]),a}function s(e){var r=n.useStore(e);return t.useCallback((function(e){r.dev_subscribe_state&&r.res(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 u,o=r||{},a=o.enabled,i=o.name;try{u=null!=a&&a&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var c=n.useAtom(e),s=c[0],l=c[1],f=t.useRef(s),d=t.useRef(!1),v=t.useRef(),p=i||e.debugLabel||e.toString();t.useEffect((function(){if(u){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=u.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 u,o;"JUMP_TO_ACTION"!==(null==(u=e.payload)?void 0:u.type)&&"JUMP_TO_STATE"!==(null==(o=e.payload)?void 0:o.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,u=e.state;0===n?null==(r=v.current)||r.init(u):t(u)}))}}));return v.current.shouldInit=!0,n}}),[e,u,p,l]),t.useEffect((function(){v.current&&(f.current=s,v.current.shouldInit?(v.current.init(s),v.current.shouldInit=!1):d.current?d.current=!1:v.current.send(p+" - "+(new Date).toLocaleString(),s))}),[e,u,p,s])},e.useAtomsDebugValue=function(e){var r,u=null!=(r=null==e?void 0:e.enabled)&&r,o=n.useStore(e),a=t.useState([]),c=a[0],s=a[1];t.useEffect((function(){if(u){var e=function(){s(Array.from((null==o.dev_get_mounted_atoms?void 0:o.dev_get_mounted_atoms())||[]))},t=null==o.dev_subscribe_state?void 0:o.dev_subscribe_state(e);return e(),t}}),[u,o]),t.useDebugValue([o,c],i)},e.useAtomsDevtools=function(e,n){var r,u=(n||{}).enabled;try{r=null!=u&&u&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var o=c(),a=s(),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}),u=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==u||u()}}}),[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(o),d.current.send({type:""+v.current.length,updatedAt:(new Date).toLocaleString()},f(o))))}),[o])},e.useAtomsSnapshot=c,e.useGotoAtomsSnapshot=s}));