UNPKG

6.3 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("jotai")):"function"==typeof define&&define.amd?define(["exports","react","jotai"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiDevtools={},e.React,e.jotai)}(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="n",i=function(e){return e.debugLabel||e.toString()},c=function(e){var t=e[0],n=e[1];return Object.fromEntries(n.flatMap((function(e){var n,o,u=null==(n=t.m)?void 0:n.call(t,e);if(!u)return[];var a=u.t,c=(null==(o=t.a)?void 0:o.call(t,e))||{};return[[i(e),r({},"e"in c&&{error:c.e},"p"in c&&{promise:c.p},"v"in c&&{value:c.v},{dependents:Array.from(a).map(i)})]]})))};function s(e){var r=n.SECRET_INTERNAL_getScopeContext(e),o=t.useContext(r).s,i=t.useState((function(){return{values:new Map,dependents:new Map}})),c=i[0],s=i[1];return t.useEffect((function(){if(o[a]){var e=new Map,t=new Map,n=new Set,r=function(){for(var r,a,i,c=new Map,l=new Map,f=!1,d=u(o.l()||[]);!(r=d()).done;){var p=r.value,v=o.a(p);if(v){if(!v.y){if("p"in v)return;n.has(p)||(n.add(p),f=!0)}"v"in v&&c.set(p,v.v)}var y=o.m(p);y&&l.set(p,y.t)}f||(i=c,(a=e).size===i.size&&Array.from(a).every((function(e){var t=e[0],n=e[1];return Object.is(i.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,l)||(e=c,t=l,n.clear(),s({values:c,dependents:l})))},i=o[a](r);return r(),i}}),[o]),c}function l(e){var r=n.SECRET_INTERNAL_getScopeContext(e),o=t.useContext(r),u=o.s,i=o.w;return t.useCallback((function(e){if(u[a]){var t=function(e){i?i((function(t){u.h(e,t)})):u.h(e)};f(e)?t(e):t(e.values)}}),[u,i])}var f=function(e){return"function"==typeof e[Symbol.iterator]},d=function(e){return e.debugLabel?e+":"+e.debugLabel:""+e},p=function(e){var t={};e.values.forEach((function(e,n){t[d(n)]=e}));var n={};return e.dependents.forEach((function(e,t){n[d(t)]=Array.from(e).map(d)})),{values:t,dependents:n}};e.useAtomDevtools=function(e,r,o){"string"!=typeof r&&void 0===o||(console.warn("DEPRECATED [useAtomDevtools] use DevtoolOptions"),r={name:r,scope:o});var u,a=r||{},i=a.enabled,c=a.name,s=a.scope;try{u=null!=i&&i&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var l=n.useAtom(e,s),f=l[0],d=l[1],p=t.useRef(f),v=t.useRef(!1),y=t.useRef(),b=c||e.debugLabel||e.toString();t.useEffect((function(){if(u){var t=function(t){"function"!=typeof d?console.warn("[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n",e):d(t)};y.current=u.connect({name:b});var n=y.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)||(v.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=y.current)||a.init(p.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=y.current)||r.init(o):t(o)}))}}));return y.current.shouldInit=!0,n}}),[e,u,b,d]),t.useEffect((function(){y.current&&(p.current=f,y.current.shouldInit?(y.current.init(f),y.current.shouldInit=!1):v.current?v.current=!1:y.current.send(b+" - "+(new Date).toLocaleString(),f))}),[e,u,b,f])},e.useAtomsDebugValue=function(e){var r,o=null!=(r=null==e?void 0:e.enabled)&&r,u=n.SECRET_INTERNAL_getScopeContext(null==e?void 0:e.scope),i=t.useContext(u).s,s=t.useState([]),l=s[0],f=s[1];t.useEffect((function(){var e;if(o){var t=function(){var e;f(Array.from((null==(e=i.l)?void 0:e.call(i))||[]))},n=null==(e=i[a])?void 0:e.call(i,t);return t(),n}}),[o,i]),t.useDebugValue([i,l],c)},e.useAtomsDevtools=function(e,n){void 0!==n&&"object"!=typeof n&&(console.warn("DEPRECATED [useAtomsDevtools] use DevtoolsOptions"),n={scope:n});var r,o=n||{},u=o.enabled,a=o.scope;try{r=null!=u&&u&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var i=s(a),c=l(a),f=t.useRef(!1),d=t.useRef(!0),v=t.useRef(),y=t.useRef([]);t.useEffect((function(){if(r){var t=function(e){void 0===e&&(e=y.current.length-1);var t=y.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(p(t())),y.current=[];break;case"JUMP_TO_ACTION":case"JUMP_TO_STATE":f.current=!0,c(t(e.payload.actionId-1));break;case"PAUSE_RECORDING":d.current=!d.current}}));return v.current=n,v.current.shouldInit=!0,function(){r.disconnect(),null==o||o()}}}),[r,c,e]),t.useEffect((function(){if(v.current)return v.current.shouldInit?(v.current.init(void 0),void(v.current.shouldInit=!1)):void(f.current?f.current=!1:d.current&&(y.current.push(i),v.current.send({type:""+y.current.length,updatedAt:(new Date).toLocaleString()},p(i))))}),[i])},e.useAtomsSnapshot=s,e.useGotoAtomsSnapshot=l}));