UNPKG

react-understate

Version:

A lightweight, reactive signals library for React 18+ with automatic dependency tracking and optimized performance

2 lines (1 loc) 6.98 kB
function e(e,t){if(t.enabled&&t.logger){if(t.showFile){const i=Error().stack;if(i){const s=function(e){return e.split("\n").slice(1).find(e=>!e.includes("core.ts")&&!e.includes("derived.ts")&&!e.includes("effects.ts")&&!e.includes("react.ts")&&!e.includes("node_modules")&&!e.includes("dist/react-understate")&&!e.includes(".js?t=")&&!e.includes("rollup")&&!e.includes("vite")&&!e.includes("ModuleJob.run")&&!e.includes("node:internal")&&""!==e.trim()&&e.includes(":"))}(i);if(s){const i=function(e){const t=e.match(/at\s+(.+):(\d+):(\d+)/);if(t){const[,e,n,o]=t;if(e&&n&&o)return{file:e,line:n,col:o}}return null}(s);return i?void t.logger(`${e} ${n=i.file,o=i.line,r=i.col,`%c${n}:${o}:${r}`}`,"color: #0066cc; text-decoration: underline; cursor: pointer;"):void t.logger(`${e} at ${s.trim()}`)}}}var n,o,r;t.logger(e)}}function t(e){if(e.includes("."))throw Error(`Invalid state name '${e}': Names cannot contain dots (.) as they break the code.`);if(!/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(e))throw Error(`Invalid state name '${e}': Names must be valid JavaScript identifiers (start with letter, underscore, or $, followed by letters, numbers, underscores, or $).`);return e}function n(e){return void 0!==e&&(q={...q,...e}),{...q}}function o(o,i){const s=i?t(i):void 0;let c=o;const a=new Set,l=new Set,f=()=>{a.forEach(e=>{e!==$&&e()}),l.forEach(e=>{if(0!=h?.preventLoops&&function(e){return m.has(e)}(w)&&(e===$||e===b))return;const t=function(e){return E.get(e)}(e);0!=t?.preventLoops&&function(e,t){const n=S.get(e);return!!n&&n.has(t)}(e,w)||e()})},d=async t=>{try{let i;const u=n();if("function"==typeof t){const n=t(c);if(n instanceof Promise)try{i=await n,s&&e(`state: '${s}' async resolved: ${JSON.stringify(i,null,2)}`,u)}catch(o){throw s&&e(`state: '${s}' async rejected: ${o}`,u),o}else i=n}else i=t;Object.is(c,i)||(s&&e(`state: '${s}' ${JSON.stringify(i,null,2)}`,u),c=i,function(e){if(!b)return;let t=S.get(b);t||(t=new Set,S.set(b,t)),t.add(e)}(w),p.add(f),y||r())}catch{}},w={get rawValue(){return c},update:async t=>{try{const r=t(c);if(r instanceof Promise)try{const t=await r;if(s){const o=n();e(`state: '${s}' update async resolved: ${JSON.stringify(t,null,2)}`,o)}d(t)}catch(o){if(s){const t=n();e(`state: '${s}' update async rejected: ${o}`,t)}throw o}else d(r)}catch{}},subscribe:e=>(a.add(e),()=>a.delete(e)),get value(){return $&&(u(w),l.add($)),c},set value(e){if("function"==typeof e){const t=e(c);if(t instanceof Promise)return void t.then(e=>d(e));d(t)}else d(e)},get requiredValue(){if($&&(u(w),l.add($)),null==c)throw Error(`Required value${s?` '${s}'`:""} is ${null===c?"null":"undefined"}. Use .value to access the actual value or ensure the state is initialized.`);return c},set requiredValue(e){if(null==e)throw Error(`Cannot set required value${s?` '${s}'`:""} to ${null===e?"null":"undefined"}. Use .value to set null/undefined values.`);d(e)},toString:()=>c+""};if(s&&"undefined"!=typeof window){if("undefined"==typeof window||j||(window.reactUnderstate={configureDebug:n,states:{},actions:{}},j=1),window.reactUnderstate.states[s])throw Error(`State with name '${s}' already exists. State names must be unique.`);window.reactUnderstate.states[s]=w}return w}function r(){if(0===p.size)return;const e=Array.from(p);p.clear(),e.forEach(e=>e())}function i(e){const t=$;return $=e,t}function s(e){const t=h;return h=e,t}function u(e){$&&0!=h?.preventLoops&&$===b&&m.add(e)}function c(){m.clear()}function a(e){g.set(e,new Set(m))}function l(e){b=e}function f(e){const t=y;return y=e,t}function d(n,o){const i=o?t(o):void 0;if(i&&e(`batch: '${i}'`,q),y)n();else{f(1);try{n()}finally{f(0),r()}}}function w(o,r){const s=r?t(r):void 0;let u,c=1,a=0;const l=new Set,f=new Set,d=()=>{c=1,l.forEach(e=>{e!==d&&e()}),f.forEach(e=>{e!==d&&e()})},w=()=>{if(c&&!a){a=1;const t=i(d);try{if(u=o(),s){const t=n();e(`derived: '${s}' ${JSON.stringify(u,null,2)}`,t)}}finally{i(t),a=0}c=0}const t=i(null);return t&&f.add(t),u};try{const e=i(d);try{u=o(),c=0}finally{i(e)}}catch{u=void 0,c=1}const v={rawValue:u,update:()=>{throw Error("Cannot update derived values directly")},subscribe:e=>(l.add(e),()=>l.delete(e)),get value(){const e=i(null);return e&&f.add(e),w()},set value(e){throw Error("Cannot update derived values directly")},get requiredValue(){const e=i(null);e&&f.add(e);const t=w();if(null==t)throw Error(`Required derived value${s?` '${s}'`:""} is ${null===t?"null":"undefined"}. Use .value to access the actual value or ensure the derived value is properly computed.`);return t},set requiredValue(e){throw Error(`Cannot set required value on derived value${s?` '${s}'`:""} - they are computed from dependencies`)}};if(s&&"undefined"!=typeof window){if(window.reactUnderstate||(window.reactUnderstate={configureDebug:()=>({}),states:{},actions:{}}),window.reactUnderstate.states[s])throw Error(`Derived value with name '${s}' already exists. State names must be unique.`);window.reactUnderstate.states[s]=v}return v}function v(o,r,u){const f=r?t(r):void 0;let w,v=0,$=0,y=0,p=0;const h=[],m={preventLoops:1,preventOverlap:1,...u},b=(t=0)=>{if(v)return;if(m.once&&$)return;if(f&&t&&J.has(f)){const e=J.get(f);return e&&"object"==typeof e&&"abort"in e&&e.abort(),M.has(f)||M.set(f,[]),void M.get(f).push(()=>b(1))}if(m.preventOverlap&&y)return void(p=1);if(0!=m.preventLoops){const e=Date.now();if(h.push(e),h.length>20&&h.shift(),h.length>=5){const t=h.filter(t=>1e3>e-t);if(t.length>10)return void 0!==console&&console.error&&t.length,void(v=1)}}if(y=1,f){const t=n();e(`effect: '${f}' running`,t)}if(w){try{w()}catch(g){}w=void 0}c(),l(b),function(e){S.set(e,new Set)}(b),c();const r=s(m),u=i(b);try{if(f){const e=J.get(f);e&&"object"==typeof e&&"abort"in e&&e.abort()}const t=new AbortController,c={signal:t.signal};let v;if(d(()=>{v=o(c)}),a(b),v instanceof Promise){if(f){const e={promise:v,abort:()=>t.abort()};J.set(f,e)}i(u),s(r),l(null),v.then(t=>{if(w=t,f){const t=n();e(`effect: '${f}' async resolved`,t)}}).catch(t=>{if(f){const o=n();e(`effect: '${f}' async rejected: ${t}`,o)}}).finally(()=>{if(y=0,$=1,f){J.delete(f);const e=M.get(f);if(e&&e.length>0){const t=e.shift();setTimeout(()=>t(),0)}}p&&(p=0,0!=m.preventLoops&&b(1))})}else a(b),w=v,i(u),s(r),l(null),y=0,$=1,p&&(p=0,0!=m.preventLoops&&b(1))}catch(E){i(u),s(r),l(null),y=0,$=1}};if(function(e,t){E.set(e,t)}(b,m),b(),f&&"undefined"!=typeof window){window.reactUnderstate||(window.reactUnderstate={configureDebug:()=>({}),states:{},actions:{}});const e=window.reactUnderstate;if(e.states[f])throw Error(`Effect with name '${f}' already exists. State names must be unique.`);e.states[f]={value:"effect",dispose:()=>{v=1,w&&(w(),w=void 0)}}}return()=>{v=1,w&&(w(),w=void 0)}}let $=null,y=0;const p=new Set;let h=null;const m=new Set;let b=null;const S=new WeakMap,g=new WeakMap,E=new WeakMap;let q={enabled:0,logger:(...e)=>console.log(...e)},j=0;const J=new Map,M=new Map;export{d as batch,w as derived,v as effect,o as state};//# sourceMappingURL=react-understate.signals.js.map