react-understate
Version:
A lightweight, reactive signals library for React 18+ with automatic dependency tracking and optimized performance
2 lines (1 loc) • 7.04 kB
JavaScript
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&&(S={...S,...e}),{...S}}function o(){if(0===v.size)return;const e=Array.from(v);v.clear(),e.forEach(e=>e())}function r(e){const t=f;return f=e,t}function i(e){const t=$;return $=e,t}function s(e){f&&0!=$?.preventLoops&&f===p&&y.add(e)}function u(){y.clear()}function c(e){b.set(e,new Set(y))}function a(e){p=e}function l(e){const t=w;return w=e,t}function d(n,r){const i=r?t(r):void 0;if(i&&e(`batch: '${i}'`,S),w)n();else{l(1);try{n()}finally{l(0),o()}}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let f=null,w=0;const v=new Set;let $=null;const y=new Set;let p=null;const h=new WeakMap,b=new WeakMap,m=new WeakMap;let S={enabled:0,logger:(...e)=>console.log(...e)},g=0;const E=new Map,q=new Map;exports.batch=d,exports.derived=function(o,i){const s=i?t(i):void 0;let u,c=1,a=0;const l=new Set,d=new Set,f=()=>{c=1,l.forEach(e=>{e!==f&&e()}),d.forEach(e=>{e!==f&&e()})},w=()=>{if(c&&!a){a=1;const t=r(f);try{if(u=o(),s){const t=n();e(`derived: '${s}' ${JSON.stringify(u,null,2)}`,t)}}finally{r(t),a=0}c=0}const t=r(null);return t&&d.add(t),u};try{const e=r(f);try{u=o(),c=0}finally{r(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=r(null);return e&&d.add(e),w()},set value(e){throw Error("Cannot update derived values directly")},get requiredValue(){const e=r(null);e&&d.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},exports.effect=function(o,s,l){const f=s?t(s):void 0;let w,v=0,$=0,y=0,p=0;const b=[],S={preventLoops:1,preventOverlap:1,...l},g=(t=0)=>{if(v)return;if(S.once&&$)return;if(f&&t&&E.has(f)){const e=E.get(f);return e&&"object"==typeof e&&"abort"in e&&e.abort(),q.has(f)||q.set(f,[]),void q.get(f).push(()=>g(1))}if(S.preventOverlap&&y)return void(p=1);if(0!=S.preventLoops){const e=Date.now();if(b.push(e),b.length>20&&b.shift(),b.length>=5){const t=b.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(x){}w=void 0}var s;u(),a(g),s=g,h.set(s,new Set),u();const l=i(S),m=r(g);try{if(f){const e=E.get(f);e&&"object"==typeof e&&"abort"in e&&e.abort()}const t=new AbortController,s={signal:t.signal};let u;if(d(()=>{u=o(s)}),c(g),u instanceof Promise){if(f){const e={promise:u,abort:()=>t.abort()};E.set(f,e)}r(m),i(l),a(null),u.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){E.delete(f);const e=q.get(f);if(e&&e.length>0){const t=e.shift();setTimeout(()=>t(),0)}}p&&(p=0,0!=S.preventLoops&&g(1))})}else c(g),w=u,r(m),i(l),a(null),y=0,$=1,p&&(p=0,0!=S.preventLoops&&g(1))}catch(j){r(m),i(l),a(null),y=0,$=1}};if(function(e,t){m.set(e,t)}(g,S),g(),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)}},exports.state=function(r,i){const u=i?t(i):void 0;let c=r;const a=new Set,l=new Set,d=()=>{a.forEach(e=>{e!==f&&e()}),l.forEach(e=>{if(0!=$?.preventLoops&&function(e){return y.has(e)}(S)&&(e===f||e===p))return;const t=(n=e,m.get(n));var n;0!=t?.preventLoops&&function(e,t){const n=h.get(e);return!!n&&n.has(t)}(e,S)||e()})},b=async t=>{try{let i;const s=n();if("function"==typeof t){const n=t(c);if(n instanceof Promise)try{i=await n,u&&e(`state: '${u}' async resolved: ${JSON.stringify(i,null,2)}`,s)}catch(r){throw u&&e(`state: '${u}' async rejected: ${r}`,s),r}else i=n}else i=t;Object.is(c,i)||(u&&e(`state: '${u}' ${JSON.stringify(i,null,2)}`,s),c=i,function(e){if(!p)return;let t=h.get(p);t||(t=new Set,h.set(p,t)),t.add(e)}(S),v.add(d),w||o())}catch{}},S={get rawValue(){return c},update:async t=>{try{const r=t(c);if(r instanceof Promise)try{const t=await r;if(u){const o=n();e(`state: '${u}' update async resolved: ${JSON.stringify(t,null,2)}`,o)}b(t)}catch(o){if(u){const t=n();e(`state: '${u}' update async rejected: ${o}`,t)}throw o}else b(r)}catch{}},subscribe:e=>(a.add(e),()=>a.delete(e)),get value(){return f&&(s(S),l.add(f)),c},set value(e){if("function"==typeof e){const t=e(c);if(t instanceof Promise)return void t.then(e=>b(e));b(t)}else b(e)},get requiredValue(){if(f&&(s(S),l.add(f)),null==c)throw Error(`Required value${u?` '${u}'`:""} 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${u?` '${u}'`:""} to ${null===e?"null":"undefined"}. Use .value to set null/undefined values.`);b(e)},toString:()=>c+""};if(u&&"undefined"!=typeof window){if("undefined"==typeof window||g||(window.reactUnderstate={configureDebug:n,states:{},actions:{}},g=1),window.reactUnderstate.states[u])throw Error(`State with name '${u}' already exists. State names must be unique.`);window.reactUnderstate.states[u]=S}return S};//# sourceMappingURL=react-understate.signals.cjs.js.map