UNPKG

5.91 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(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 r(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)?r(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}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 u=function(e){return e.debugLabel?e+":"+e.debugLabel:""+e},a=function(e){var t={};e[0].forEach((function(e,n){t[u(n)]=e}));var n={};return e[1].forEach((function(e,t){n[u(t)]=Array.from(e).map(u)})),{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],v=t.useRef(f),p=t.useRef(!1),y=t.useRef(),E=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:E});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)||(p.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(v.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,E,d]),t.useEffect((function(){y.current&&(v.current=f,y.current.shouldInit?(y.current.init(f),y.current.shouldInit=!1):p.current?p.current=!1:y.current.send(E+" - "+(new Date).toLocaleString(),f))}),[e,u,E,f])},e.useAtomsDevtools=function(e,r){void 0!==r&&"object"!=typeof r&&(console.warn("DEPRECATED [useAtomsDevtools] use DevtoolsOptions"),r={scope:r});var u,i=r||{},c=i.enabled,s=i.scope,l=n.SECRET_INTERNAL_getScopeContext(s),f=t.useContext(l),d=f.s,v=f.w;try{u=null!=c&&c&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}if(u&&!d.n)throw new Error("useAtomsDevtools can only be used in dev mode.");var p=t.useState((function(){return[new Map,new Map]})),y=p[0],E=p[1];t.useEffect((function(){var e;if(u){var t=function(){for(var e,t=new Map,n=new Map,r=o((null==(u=d.l)?void 0:u.call(d))||[]);!(e=r()).done;){var u,a,i,c=e.value,s=null==(a=d.a)?void 0:a.call(d,c);if(s){if(s.r===s.i)return;"v"in s&&t.set(c,s.v)}var l=null==(i=d.m)?void 0:i.call(d,c);l&&n.set(c,l.t)}E((function(e){return r=e[0],o=t,r.size===o.size&&Array.from(r).every((function(e){var t=e[0],n=e[1];return Object.is(o.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)}))}))}(e[1],n)?e:[t,n];var r,o}))},n=null==(e=d.n)?void 0:e.call(d,t);return t(),n}}),[u,d]);var h=t.useCallback((function(e){v?v((function(t){d.h(e,t)})):d.h(e)}),[d,v]),m=t.useRef(!1),b=t.useRef(!0),A=t.useRef(),S=t.useRef([]);t.useEffect((function(){if(u){var t=function(e){void 0===e&&(e=S.current.length-1);var t=S.current[e>=0?e:0];if(!t)throw new Error("snaphost index out of bounds");return t},n=u.connect({name:e}),r=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(a(t())),S.current=[];break;case"JUMP_TO_ACTION":case"JUMP_TO_STATE":m.current=!0,h(t(e.payload.actionId-1)[0]);break;case"PAUSE_RECORDING":b.current=!b.current}}));return A.current=n,A.current.shouldInit=!0,r}}),[u,h,e]),t.useEffect((function(){if(A.current)return A.current.shouldInit?(A.current.init(void 0),void(A.current.shouldInit=!1)):void(m.current?m.current=!1:b.current&&(S.current.push(y),A.current.send({type:""+S.current.length,updatedAt:(new Date).toLocaleString()},a(y))))}),[y])},e.useAtomsSnapshot=function(e){var r=n.SECRET_INTERNAL_getScopeContext(e),o=t.useContext(r).s;if(!o.n)throw new Error("useAtomsSnapshot can only be used in dev mode.");var u=t.useState((function(){return new Map})),a=u[0],i=u[1];return t.useEffect((function(){var e,t=function(){var e,t=Array.from((null==(e=o.l)?void 0:e.call(o))||[]);i(function(e,t){var n=t.map((function(t){var n,r,o=null!=(n=null==(r=e.a)?void 0:r.call(e,t))?n:{};return[t,"v"in o?o.v:void 0]}));return new Map(n)}(o,t))},n=null==(e=o.n)?void 0:e.call(o,t);return t(),n}),[o]),a},e.useGotoAtomsSnapshot=function(e){var r=n.SECRET_INTERNAL_getScopeContext(e),o=t.useContext(r).s;if(!o.n)throw new Error("useGotoAtomsSnapshot can only be used in dev mode.");return t.useCallback((function(e){o.h(e)}),[o])},Object.defineProperty(e,"__esModule",{value:!0})}));