{
  "version": 3,
  "sources": ["../src/index.ts"],
  "sourcesContent": ["export const CycleError = new Error('Cycle detected')\n\nconst _effectStack:(() => any)[] = []\nlet _globalMaxDepth = 100\nconst DEFAULT_MAX_DEPTH = 100\n\n// Batching state\nlet _batchDepth = 0\nlet _batchedEffects:((() => any) & { _execute?:() => void })[] = []\n\nexport type SignOptions = {\n    maxDepth?:number\n}\n\nexport type Sign<T> = {\n    value:T\n    peek:()=>T\n}\n\nexport function sign<T> (value:T, options?:SignOptions):Sign<T> {\n    const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH\n    const subscriptions = new Set<()=>any>()\n\n    // If this signal has a custom maxDepth, set it globally\n    if (options?.maxDepth !== undefined) {\n        _globalMaxDepth = maxDepth\n    }\n\n    return {\n        get value ():T {\n            const currentEffect = _effectStack[_effectStack.length - 1]\n            if (currentEffect) {\n                subscriptions.add(currentEffect)\n            }\n\n            return value\n        },\n\n        set value (newValue:T) {\n            if (newValue === value) return\n            value = newValue\n\n            // Always notify subscribers immediately\n            // During batching, effects will queue themselves instead of executing\n            subscriptions.forEach(fn => fn())\n        },\n\n        peek () {\n            return value\n        }\n    }\n}\n\nexport function effect (fn:()=>any):()=>void {\n    let isActive = true\n\n    const effectFn = () => {\n        if (!isActive) return\n\n        if (_batchDepth > 0) {\n            // We're in a batch - queue this effect instead of executing immediately\n            if (!_batchedEffects.includes(effectFn)) {\n                _batchedEffects.push(effectFn)\n            }\n            return\n        }\n\n        if (_effectStack.length > _globalMaxDepth) {\n            throw CycleError\n        }\n        _effectStack.push(effectFn)\n        try {\n            fn()\n        } finally {\n            _effectStack.pop()\n        }\n    }\n\n    // Add a way to execute the effect bypassing batch queue\n    effectFn._execute = () => {\n        if (!isActive) return\n\n        if (_effectStack.length > _globalMaxDepth) {\n            throw CycleError\n        }\n        _effectStack.push(effectFn)\n        try {\n            fn()\n        } finally {\n            _effectStack.pop()\n        }\n    }\n\n    effectFn()\n\n    // Unsubscribe function\n    return () => {\n        isActive = false\n    }\n}\n\nexport function computed<T> (fn:()=>T):Sign<T> {\n    const initialValue = fn()\n    const computedSign = sign(initialValue)\n    let isStale = false\n\n    const updateComputed = () => {\n        if (isStale) {\n            const newValue = fn()\n            computedSign.value = newValue\n            isStale = false\n        }\n    }\n\n    // Override the getter to update if stale when accessed\n    const originalDescriptor = Object.getOwnPropertyDescriptor(computedSign, 'value')!\n    Object.defineProperty(computedSign, 'value', {\n        get () {\n            if (isStale) {\n                updateComputed()\n            }\n            return originalDescriptor.get!.call(this)\n        },\n        set: originalDescriptor.set\n    })\n\n    // Create a special computed effect that immediately marks as stale\n    const computedEffect = () => {\n        const newValue = fn()\n        const currentValue = computedSign.peek()\n\n        if (newValue !== currentValue) {\n            isStale = true\n            // Always update the computed signal to notify subscribers\n            // The subscribers (effects) will handle batching themselves\n            updateComputed()\n        }\n    }\n\n    // This effect should NOT be batched - it needs to run immediately to mark the computed as stale\n    const isActive = true\n\n    const dependencyEffect = () => {\n        if (!isActive) return\n\n        // This effect should NEVER be batched - it needs to run immediately\n        // to mark computed signals as stale when their dependencies change\n\n        if (_effectStack.length > _globalMaxDepth) {\n            throw CycleError\n        }\n        _effectStack.push(dependencyEffect)\n        try {\n            fn() // This subscribes to dependencies\n            computedEffect() // This handles the update logic - runs immediately\n        } finally {\n            _effectStack.pop()\n        }\n    }\n\n    dependencyEffect()\n\n    return computedSign\n}\n\nexport function batch<T> (fn:() => T):T {\n    _batchDepth++\n\n    try {\n        const result = fn()\n\n        // If this is the outermost batch, flush all batched effects\n        if (_batchDepth === 1) {\n            const effects = [..._batchedEffects]\n            _batchedEffects = []\n\n            // Execute all batched effects\n            effects.forEach(effectFn => {\n                if (effectFn._execute) {\n                    effectFn._execute()\n                } else {\n                    effectFn()\n                }\n            })\n        }\n\n        return result\n    } finally {\n        _batchDepth--\n    }\n}\n"],
  "mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,UAAAC,EAAA,aAAAC,EAAA,WAAAC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,EAAG,EAClBO,EAAed,EAAKa,CAAY,EACtC,IAAIE,EAAU,GAEd,MAAMC,EAAiBR,EAAA,IAAM,CACzB,GAAIO,EAAS,CACT,MAAMT,EAAWC,EAAG,EACpBO,EAAa,MAAQR,EACrBS,EAAU,EACd,CACJ,EANuB,kBASjBE,EAAqB,OAAO,yBAAyBH,EAAc,OAAO,EAChF,OAAO,eAAeA,EAAc,QAAS,CACzC,KAAO,CACH,OAAIC,GACAC,EAAe,EAEZC,EAAmB,IAAK,KAAK,IAAI,CAC5C,EACA,IAAKA,EAAmB,GAC5B,CAAC,EAGD,MAAMC,EAAiBV,EAAA,IAAM,CACzB,MAAMF,EAAWC,EAAG,EACdY,EAAeL,EAAa,KAAK,EAEnCR,IAAaa,IACbJ,EAAU,GAGVC,EAAe,EAEvB,EAVuB,kBAajBN,EAAW,GAEXU,EAAmBZ,EAAA,IAAM,CAC3B,GAAKE,EAKL,IAAIf,EAAa,OAASC,EACtB,MAAMF,EAEVC,EAAa,KAAKyB,CAAgB,EAClC,GAAI,CACAb,EAAG,EACHW,EAAe,CACnB,QAAE,CACEvB,EAAa,IAAI,CACrB,EACJ,EAhByB,oBAkBzB,OAAAyB,EAAiB,EAEVN,CACX,CA9DgBN,EAAAI,EAAA,YAgET,SAASS,EAAUd,EAAc,CACpCT,IAEA,GAAI,CACA,MAAMwB,EAASf,EAAG,EAGlB,GAAIT,IAAgB,EAAG,CACnB,MAAMyB,EAAU,CAAC,GAAGxB,CAAe,EACnCA,EAAkB,CAAC,EAGnBwB,EAAQ,QAAQZ,GAAY,CACpBA,EAAS,SACTA,EAAS,SAAS,EAElBA,EAAS,CAEjB,CAAC,CACL,CAEA,OAAOW,CACX,QAAE,CACExB,GACJ,CACJ,CAzBgBU,EAAAa,EAAA",
  "names": ["index_exports", "__export", "CycleError", "batch", "computed", "effect", "sign", "__toCommonJS", "CycleError", "_effectStack", "_globalMaxDepth", "DEFAULT_MAX_DEPTH", "_batchDepth", "_batchedEffects", "sign", "value", "options", "maxDepth", "subscriptions", "currentEffect", "newValue", "fn", "__name", "effect", "isActive", "effectFn", "computed", "initialValue", "computedSign", "isStale", "updateComputed", "originalDescriptor", "computedEffect", "currentValue", "dependencyEffect", "batch", "result", "effects"]
}
