UNPKG

7.63 kBJavaScriptView Raw
1!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("jotai")):"function"==typeof define&&define.amd?define(["exports","react","jotai"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiDevtools={},e.React,e.jotai)}(this,(function(e,n,t){"use strict";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 o(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}function a(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,n){if(e){if("string"==typeof e)return o(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(e,n):void 0}}(e))||n&&e&&"number"==typeof e.length){t&&(e=t);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 u=function(e){return e.debugLabel||e.toString()},i=function(e){var n=e[0],t=e[1];return Object.fromEntries(t.flatMap((function(e){var t,o,a=null==(t=n.m)?void 0:t.call(n,e);if(!a)return[];var i=a.t,l=(null==(o=n.a)?void 0:o.call(n,e))||{};return[[u(e),r({},"e"in l&&{error:l.e},"p"in l&&{promise:l.p},"v"in l&&{value:l.v},{dependents:Array.from(i).map(u)})]]})))};var l=function(e){return"function"==typeof e[Symbol.iterator]},c=function(e){return e.debugLabel?e+":"+e.debugLabel:""+e},s=function(e){var n={};e.values.forEach((function(e,t){n[c(t)]=e}));var t={};return e.dependents.forEach((function(e,n){t[c(n)]=Array.from(e).map(c)})),{values:n,dependents:t}};e.useAtomDevtools=function(e,r,o){"string"!=typeof r&&void 0===o||(console.warn("DEPRECATED [useAtomDevtools] use DevtoolOptions"),r={name:r,scope:o});var a,u=r||{},i=u.enabled,l=u.name,c=u.scope;try{a=null!=i&&i&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}var s=t.useAtom(e,c),f=s[0],v=s[1],d=n.useRef(f),p=n.useRef(!1),y=n.useRef(),b=l||e.debugLabel||e.toString();n.useEffect((function(){if(a){var n=function(n){"function"!=typeof v?console.warn("[Warn] you cannot do write operations (Time-travelling, etc) in read-only atoms\n",e):v(n)};y.current=a.connect({name:b});var t=y.current.subscribe((function(e){var t,r;if("ACTION"===e.type&&e.payload)try{n(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,a;"JUMP_TO_ACTION"!==(null==(o=e.payload)?void 0:o.type)&&"JUMP_TO_STATE"!==(null==(a=e.payload)?void 0:a.type)||(p.current=!0,n(JSON.parse(e.state)))}else if("DISPATCH"===e.type&&"COMMIT"===(null==(t=e.payload)?void 0:t.type)){var u;null==(u=y.current)||u.init(d.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,t){var r,o=e.state;0===t?null==(r=y.current)||r.init(o):n(o)}))}}));return y.current.shouldInit=!0,t}}),[e,a,b,v]),n.useEffect((function(){y.current&&(d.current=f,y.current.shouldInit?(y.current.init(f),y.current.shouldInit=!1):p.current?p.current=!1:y.current.send(b+" - "+(new Date).toLocaleString(),f))}),[e,a,b,f])},e.useAtomsDebugValue=function(e){var r,o=null!=(r=null==e?void 0:e.enabled)&&r,a=t.SECRET_INTERNAL_getScopeContext(null==e?void 0:e.scope),u=n.useContext(a).s,l=n.useState([]),c=l[0],s=l[1];n.useEffect((function(){var e;if(o){var n=function(){var e;s(Array.from((null==(e=u.l)?void 0:e.call(u))||[]))},t=null==(e=u.n)?void 0:e.call(u,n);return n(),t}}),[o,u]),n.useDebugValue([u,c],i)},e.useAtomsDevtools=function(e,r){void 0!==r&&"object"!=typeof r&&(console.warn("DEPRECATED [useAtomsDevtools] use DevtoolsOptions"),r={scope:r});var o,u=r||{},i=u.enabled,l=u.scope,c=t.SECRET_INTERNAL_getScopeContext(l),f=n.useContext(c),v=f.s,d=f.w;try{o=null!=i&&i&&window.__REDUX_DEVTOOLS_EXTENSION__}catch(e){}if(o&&!v.n)throw new Error("useAtomsDevtools can only be used in dev mode.");var p=n.useState((function(){return{values:new Map,dependents:new Map}})),y=p[0],b=p[1];n.useEffect((function(){var e;if(o){var n=new Map,t=new Map,r=new Set,u=function(){for(var e,o,u,i=new Map,l=new Map,c=!1,s=a((null==(f=v.l)?void 0:f.call(v))||[]);!(e=s()).done;){var f,d,p,y=e.value,m=null==(d=v.a)?void 0:d.call(v,y);if(m){if(!m.y){if("p"in m)return;r.has(y)||(r.add(y),c=!0)}"v"in m&&i.set(y,m.v)}var E=null==(p=v.m)?void 0:p.call(v,y);E&&l.set(y,E.t)}c||(u=i,(o=n).size===u.size&&Array.from(o).every((function(e){var n=e[0],t=e[1];return Object.is(u.get(n),t)}))&&function(e,n){return e.size===n.size&&Array.from(e).every((function(e){var t=e[0],r=e[1],o=n.get(t);return o&&r.size===o.size&&Array.from(r).every((function(e){return o.has(e)}))}))}(t,l)||(n=i,t=l,r.clear(),b({values:i,dependents:l})))},i=null==(e=v.n)?void 0:e.call(v,u);return u(),i}}),[o,v]);var m=n.useCallback((function(e){var n=e.values;d?d((function(e){v.h(n,e)})):v.h(n)}),[v,d]),E=n.useRef(!1),h=n.useRef(!0),S=n.useRef(),A=n.useRef([]);n.useEffect((function(){if(o){var n=function(e){void 0===e&&(e=A.current.length-1);var n=A.current[e>=0?e:0];if(!n)throw new Error("snaphost index out of bounds");return n},t=o.connect({name:e}),r=t.subscribe((function(e){var r;if("DISPATCH"===e.type)switch(null==(r=e.payload)?void 0:r.type){case"RESET":break;case"COMMIT":t.init(s(n())),A.current=[];break;case"JUMP_TO_ACTION":case"JUMP_TO_STATE":E.current=!0,m(n(e.payload.actionId-1));break;case"PAUSE_RECORDING":h.current=!h.current}}));return S.current=t,S.current.shouldInit=!0,r}}),[o,m,e]),n.useEffect((function(){if(S.current)return S.current.shouldInit?(S.current.init(void 0),void(S.current.shouldInit=!1)):void(E.current?E.current=!1:h.current&&(A.current.push(y),S.current.send({type:""+A.current.length,updatedAt:(new Date).toLocaleString()},s(y))))}),[y])},e.useAtomsSnapshot=function(e){var r=t.SECRET_INTERNAL_getScopeContext(e),o=n.useContext(r).s;if(!o.n)throw new Error("useAtomsSnapshot can only be used in dev mode.");var u=n.useState((function(){return{values:new Map,dependents:new Map}})),i=u[0],l=u[1];return n.useEffect((function(){var e,n=new Map,t=new Map,r=new Set,u=function(){for(var e,u,i,c=new Map,s=new Map,f=!1,v=a((null==(d=o.l)?void 0:d.call(o))||[]);!(e=v()).done;){var d,p,y,b=e.value,m=null==(p=o.a)?void 0:p.call(o,b);if(m){if(!m.y){if("p"in m)return;r.has(b)||(r.add(b),f=!0)}"v"in m&&c.set(b,m.v)}var E=null==(y=o.m)?void 0:y.call(o,b);E&&s.set(b,E.t)}f||(i=c,(u=n).size===i.size&&Array.from(u).every((function(e){var n=e[0],t=e[1];return Object.is(i.get(n),t)}))&&function(e,n){return e.size===n.size&&Array.from(e).every((function(e){var t=e[0],r=e[1],o=n.get(t);return o&&r.size===o.size&&Array.from(r).every((function(e){return o.has(e)}))}))}(t,s)||(n=c,t=s,r.clear(),l({values:c,dependents:s})))},i=null==(e=o.n)?void 0:e.call(o,u);return u(),i}),[o]),i},e.useGotoAtomsSnapshot=function(e){var r=t.SECRET_INTERNAL_getScopeContext(e),o=n.useContext(r),a=o.s,u=o.w;if(!a.n)throw new Error("useGotoAtomsSnapshot can only be used in dev mode.");return n.useCallback((function(e){var n=function(e){u?u((function(n){a.h(e,n)})):a.h(e)};l(e)?n(e):n(e.values)}),[a,u])},Object.defineProperty(e,"__esModule",{value:!0})}));