UNPKG

@shtse8/fluxus

Version:

A functional, reactive state management library for TypeScript inspired by Riverpod.

1 lines 48.9 kB
{"version":3,"sources":["/home/runner/work/fluxus/fluxus/dist/chunk-TZWDWLSJ.cjs","../src/providers/stateProvider.ts","../src/providers/computedProvider.ts","../src/scope.ts"],"names":[],"mappings":"AAAA;ACqCA,IAAM,eAAA,EAAiB,MAAA,CAAO,GAAA,CAAI,sBAAsB,CAAA;AAoCjD,SAAS,uBAAA,CACd,QAAA,EACsC;AAEtC,EAAA,OAAO,OAAO,SAAA,IAAa,WAAA,GAAc,CAAC,CAAE,QAAA,CAAiB,cAAc,CAAA;AAC7E;AAuBO,SAAS,aAAA,CACd,YAAA,EAC0B;AAI1B,EAAA,MAAM,gBAAA,EAAkB,CAAC,MAAA,EAAqB,UAAA,EAAA,GAA8C;AAC1F,IAAA,IAAI,aAAA,EAAe,OAAO,aAAA,IAAiB,WAAA,EACtC,YAAA,CAA4C,MAAM,EAAA,EACnD,YAAA;AAEJ,IAAA,MAAM,MAAA,EAA+B;AAAA,MACnC,KAAA,EAAO,YAAA;AAAA,MACP,SAAA,kBAAW,IAAI,GAAA,CAAI;AAAA;AAAA,IAErB,CAAA;AAKA,IAAA,OAAO,KAAA;AAAA,EACT,CAAA;AAOA,EAAA,MAAM,WAAA,EAA0B,CAAA,EAAA,GAAM;AAKpC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,EACF,CAAA;AAGA,EAAC,UAAA,CAAmB,cAAc,EAAA,EAAI,EAAE,gBAAgB,CAAA;AACxD,EAAC,UAAA,CAAmB,sBAAA,EAAwB,eAAA;AAG5C,EAAA,OAAO,UAAA;AACT;ADzHA;AACA;AERO,IAAM,kBAAA,EAAoB,MAAA,CAAO,GAAA,CAAI,yBAAyB,CAAA;AAgC9D,SAAS,0BAAA,CACd,QAAA,EACyC;AAEzC,EAAA,OAAO,OAAO,SAAA,IAAa,WAAA,GAAc,CAAC,CAAE,QAAA,CAAiB,iB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UAAA,EAAY;AACnB,QAAA,KAAA,CAAM,OAAA,CAAQ,CAAA;AAAA,MAClB;AAAA,IACF,CAAC,CAAA;AAED,IAAA,IAAA,CAAK,cAAA,CAAe,KAAA,CAAM,CAAA;AAC1B,IAAA,IAAA,CAAK,OAAA,EAAS,IAAA;AAAA,EAEhB;AACF,CAAA;AAOO,SAAS,WAAA,CAAY,OAAA,EAAuB,IAAA,EAAa;AAC5D,EAAA,OAAO,IAAI,KAAA,CAAM,MAAM,CAAA;AAC3B;AH5IA;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACF,yQAAC","file":"/home/runner/work/fluxus/fluxus/dist/chunk-TZWDWLSJ.cjs","sourcesContent":[null,"import { ScopeReader, Provider, Dispose } from '../types.js';\nimport { Scope } from '../scope.js'; // Need Scope to add the updater method later\n\n// --- Type Definitions ---\n\n/**\n * Defines the shape of the function used to update the state of a {@link StateProviderInstance}.\n * This function is retrieved via `scope.updater(provider)`.\n *\n * @template T The type of the state.\n * @param {Scope} scope The scope instance in which the update occurs.\n * @param {StateProviderInstance<T>} provider The specific provider instance being updated.\n * @param {T | ((prev: T) => T)} newValueOrFn The new value or a function that receives the previous value and returns the new value.\n * @returns {void}\n */\nexport type StateUpdater<T> = (\n scope: Scope,\n provider: StateProviderInstance<T>,\n newValue: T | ((prev: T) => T)\n) => void;\n\n/**\n * Represents the internal state associated with a specific {@link StateProviderInstance}\n * within a particular {@link Scope}.\n *\n * @template T The type of the state value.\n */\nexport interface StateProviderState<T> {\n /** The current value of the state. */\n value: T;\n /** A set of listener callbacks subscribed to changes in this state within the scope. */\n listeners: Set<() => void>;\n}\n\nexport \n// Using a unique symbol allows us to identify StateProvider instances\n// without relying on instanceof checks or specific properties.\nconst $stateProvider = Symbol.for('fluxus.stateProvider');\n\n/**\n * A specialized {@link Provider} that manages a mutable piece of state.\n *\n * It provides the current state value when read and allows the state to be\n * updated via an updater function obtained from the {@link Scope}.\n * StateProviders are the primary way to introduce mutable state into the Fluxus system.\n *\n * @template T The type of the state value.\n * @extends {Provider<T>}\n */\nexport interface StateProviderInstance<T> extends Provider<T> {\n /** @internal A unique symbol used to identify StateProvider instances. */\n [$stateProvider]: {\n /**\n * @internal The function called by the Scope to create the initial state\n * for this provider instance within that scope.\n * @param {ScopeReader} reader - The reader for the initializing scope.\n * @param {number} internalId - A unique ID assigned by the scope for debugging.\n * @returns {StateProviderState<T>} The initial internal state.\n */\n initializeState: (reader: ScopeReader, internalId: number) => StateProviderState<T>;\n };\n /** @internal A read-only property for easier type narrowing if needed. */\n // Add a property for easier type narrowing if needed, though symbol is preferred\n readonly _fluxus_provider_type: 'StateProvider';\n}\n\n/**\n * Type guard to check if a given value is a {@link StateProviderInstance}.\n *\n * @template T The potential type of the state managed by the provider.\n * @param {unknown} provider The value to check.\n * @returns {provider is StateProviderInstance<T>} True if the value is a StateProviderInstance, false otherwise.\n */\nexport function isStateProviderInstance<T>(\n provider: unknown\n): provider is StateProviderInstance<T> {\n // Check if it's a function and has the internal symbol identifier.\n return typeof provider === 'function' && !!(provider as any)[$stateProvider];\n}\n\n\n// --- Factory Function ---\n\n/**\n * Creates a {@link StateProviderInstance} which manages a mutable piece of state.\n *\n * @template T The type of the state value.\n * @param {T | ((reader: ScopeReader) => T)} initialValue - The initial value for the state,\n * or a function that computes the initial value using a {@link ScopeReader}.\n * If a function is provided, it will be called once per scope when the provider\n * is first initialized within that scope.\n * @returns {StateProviderInstance<T>} The created StateProvider instance.\n *\n * @example\n * // Simple counter state provider\n * const counterProvider = stateProvider(0);\n *\n * // State provider with initial value computed from another provider\n * const userProvider = stateProvider<{ name: string; id: number } | null>(null);\n * const userIdProvider = stateProvider((reader) => reader.read(userProvider)?.id ?? -1);\n */\nexport function stateProvider<T>(\n initialValue: T | ((reader: ScopeReader) => T)\n): StateProviderInstance<T> {\n\n // This is the core function that Scope will call to initialize the provider's state.\n // Accept internalId from Scope\n const initializeState = (reader: ScopeReader, internalId: number): StateProviderState<T> => {\n let currentValue = typeof initialValue === 'function'\n ? (initialValue as (reader: ScopeReader) => T)(reader)\n : initialValue;\n\n const state: StateProviderState<T> = {\n value: currentValue,\n listeners: new Set(),\n // Updater is no longer defined here\n };\n\n // No specific disposal needed for the value itself unless initialValue function uses onDispose\n // reader.onDispose(() => { console.log('Disposing state provider state'); });\n\n return state;\n };\n\n // This function represents the Provider<T> interface.\n // When scope.read(stateProviderInstance) is called, the Scope internally\n // handles retrieving the actual state value from its map using the\n // stateProviderInstance as the key. This function itself doesn't need\n // to contain the read logic directly.\n const providerFn: Provider<T> = () => {\n // This function body is effectively unused at runtime because the Scope's\n // read/initializeProviderState methods handle the logic based on the\n // presence of the [$stateProvider] symbol.\n // Throwing an error here helps catch incorrect direct invocation.\n throw new Error(\n 'StateProvider function should not be called directly. Use scope.read(provider).'\n );\n };\n\n // Attach the metadata/initializer function using the symbol\n (providerFn as any)[$stateProvider] = { initializeState };\n (providerFn as any)._fluxus_provider_type = 'StateProvider';\n\n\n return providerFn as StateProviderInstance<T>;\n}\n\n// --- Scope Extension (Conceptual - needs integration into Scope class) ---\n\n// We need to modify the Scope class to:\n// 1. Recognize StateProviderInstance when initializing state.\n// 2. Store the *entire* StateProviderState (value + updater + listeners) internally.\n// 3. Return only the `value` when `scope.read(stateProviderInstance)` is called.\n// 4. Provide a new method `scope.updater(stateProviderInstance)` to retrieve the updater.\n\n/*\n// Example of how Scope might be extended:\n\ndeclare module '../scope' {\n interface Scope {\n updater<T>(provider: StateProviderInstance<T>): StateUpdater<T>;\n // Internal method might be needed\n // getStateProviderState<T>(provider: StateProviderInstance<T>): StateProviderState<T> | undefined;\n }\n}\n\n// Implementation within Scope class would involve checking the symbol\n// during initialization and read, and adding the `updater` method.\n*/","import { ScopeReader, Provider, Dispose } from '../types.js';\nimport { Scope } from '../scope.js'; // May be needed later\n\n// --- Type Definitions ---\n\n/**\n * Represents the internal state associated with a specific {@link ComputedProviderInstance}\n * within a particular {@link Scope}. Computed providers typically don't have listeners\n * themselves but rely on dependencies notifying them to become stale.\n *\n * @template T The type of the computed state value.\n */\n// Note: For now, computed providers will use GenericProviderState internally in the Scope,\n// as their value is computed once and cached until marked stale.\n// We might introduce a specific state type later if needed for optimizations.\n\n/** A unique symbol to identify ComputedProvider instances. */\nexport const $computedProvider = Symbol.for('fluxus.computedProvider');\n\n/**\n * Represents an instance of a ComputedProvider.\n * It acts as a Provider<T> for reading the computed value. It carries metadata\n * via a symbol to distinguish it during initialization.\n *\n * @template T The type of the computed value.\n * @extends {Provider<T>}\n */\nexport interface ComputedProviderInstance<T> extends Provider<T> {\n /** @internal A unique symbol used to identify ComputedProvider instances. */\n [$computedProvider]: {\n /**\n * @internal The computation function provided when the provider was created.\n * This function is called by the Scope during initialization or recomputation.\n * @param {ScopeReader} reader - The reader for the current scope.\n * @returns {T} The computed value.\n */\n compute: (reader: ScopeReader) => T;\n };\n /** @internal A read-only property for easier type narrowing if needed. */\n readonly _fluxus_provider_type: 'ComputedProvider';\n}\n\n/**\n * Type guard to check if a given value is a {@link ComputedProviderInstance}.\n *\n * @template T The potential type of the computed value.\n * @param {unknown} provider The value to check.\n * @returns {provider is ComputedProviderInstance<T>} True if the value is a ComputedProviderInstance, false otherwise.\n */\nexport function isComputedProviderInstance<T>(\n provider: unknown\n): provider is ComputedProviderInstance<T> {\n // Check if it's a function and has the internal symbol identifier.\n return typeof provider === 'function' && !!(provider as any)[$computedProvider];\n}\n\n\n// --- Factory Function ---\n\n/**\n * Creates a {@link ComputedProviderInstance} which derives its state by computing\n * a value based on other providers.\n *\n * The computation function is executed lazily when the provider is first read\n * within a scope, and its result is cached. The computed value is automatically\n * re-evaluated when any of the providers it `read` or `watch`ed during the\n * computation change their state.\n *\n * @template T The type of the computed value.\n * @param {(reader: ScopeReader) => T} compute - The function that computes the\n * derived state. It receives a {@link ScopeReader} to access other providers.\n * It's crucial to use `reader.watch` or `reader.read` within this function\n * to establish dependencies correctly for automatic recomputation.\n * @returns {ComputedProviderInstance<T>} The created ComputedProvider instance.\n *\n * @example\n * const countProvider = stateProvider(0);\n * const doubleCountProvider = computedProvider((reader) => {\n * const count = reader.watch(countProvider); // Establish dependency\n * const count = reader.watch(countProvider); // Establish dependency\n * return count * 2;\n * });\n *\n * @see {@link stateProvider} for creating mutable state.\n * @see {@link ScopeReader} for how to access dependencies.\n * });\n */\nexport function computedProvider<T>(\n compute: (reader: ScopeReader) => T\n): ComputedProviderInstance<T> {\n\n // This function represents the Provider<T> interface.\n // Similar to StateProvider, the actual logic happens within the Scope\n // based on the metadata attached via the symbol.\n const providerFn: Provider<T> = () => {\n throw new Error(\n 'ComputedProvider function should not be called directly. Use scope.read(provider).'\n );\n };\n\n // Attach metadata\n (providerFn as any)[$computedProvider] = { compute };\n (providerFn as any)._fluxus_provider_type = 'ComputedProvider';\n\n return providerFn as ComputedProviderInstance<T>;\n}","import { Provider, ScopeReader, Dispose, Disposable, isProvider } from './types.js';\nimport {\n StateProviderInstance,\n isStateProviderInstance,\n StateUpdater,\n StateProviderState,\n $stateProvider\n} from './providers/stateProvider.js';\nimport {\n ComputedProviderInstance,\n isComputedProviderInstance,\n $computedProvider\n} from './providers/computedProvider.js';\n\n// --- Internal State Types ---\n\n/** Common fields for all internal provider states within a scope. */\nlet internalStateIdCounter = 0; // Counter for unique IDs\n\ninterface BaseInternalState extends Disposable {\n /** Unique ID for debugging state instances */\n readonly internalId: number;\n /** Explicitly add dispose signature to help TS inference */\n dispose: Dispose;\n /** List of functions to call when this state is disposed. */\n disposeCallbacks: Set<Dispose>;\n /** Set of providers that this provider depends on. */\n dependencies: Set<Provider<any>>;\n /** Set of providers that depend on this provider. */\n dependents: Set<Provider<any>>;\n /** Flag indicating if the value is currently being computed (to detect circular dependencies). */\n isComputing: boolean;\n /** Flag indicating if the state has been disposed. */\n isDisposed: boolean;\n /** Flag indicating if the provider's value might be outdated due to dependency changes. */\n isStale: boolean;\n}\n\n/** Internal state for regular (non-StateProvider) providers. */\ninterface GenericProviderState<T> extends BaseInternalState {\n type: 'generic';\n /** The computed value of the provider. */\n value: T;\n}\n\n/** Internal state specific to StateProviders. */\ninterface StateProviderInternalState<T> extends BaseInternalState {\n type: 'state';\n /** The specific state managed by the StateProvider (value, updater, listeners). */\n stateProviderState: StateProviderState<T>;\n}\n\n/** Union type for all possible internal states stored in the scope. */\ntype InternalState<T> = GenericProviderState<T> | StateProviderInternalState<T>;\n\n// Type guard for StateProviderInternalState\nfunction isStateProviderInternalState<T>(\n state: InternalState<any> | undefined\n): state is StateProviderInternalState<any> { // Use <any> here\n return state?.type === 'state';\n}\n\n/**\n * Manages the state and lifecycle of providers within a specific context.\n * Scopes can be nested to allow for overriding providers in different parts\n * of an application. Each scope maintains its own instance of provider states.\n *\n * @implements {Disposable}\n */\nexport class Scope implements Disposable {\n /** Stores the state associated with each provider within this scope. */\n private providerStates = new Map<Provider<any>, InternalState<any>>();\n /** Optional parent scope for provider overrides and nesting. */\n private parent: Scope | null;\n /** Flag indicating if the scope itself has been disposed. */\n private _isDisposed = false; // Renamed internal flag\n\n /**\n * Indicates whether the scope has been disposed. Once disposed, a scope\n * cannot be used to read or initialize providers.\n * @returns {boolean} True if the scope is disposed, false otherwise.\n */\n public get isDisposed(): boolean {\n return this._isDisposed;\n }\n\n /**\n * Creates a new Scope instance.\n * @param {Scope | null} [parent=null] - An optional parent scope. If provided,\n * this scope can potentially inherit or override providers from the parent\n * (behavior depends on specific provider implementations and future features).\n */\n constructor(parent: Scope | null = null) {\n this.parent = parent;\n }\n\n /** Checks if the scope or its ancestors have been disposed. */\n private checkDisposed(): void {\n if (this._isDisposed) { // Use internal flag\n throw new Error('Scope has been disposed');\n }\n // Recursively check parent scopes if necessary, though direct check might suffice\n // if disposal propagates correctly.\n }\n\n /**\n * Reads the current value of a given provider within this scope.\n *\n * If the provider has already been initialized in this scope, its cached value\n * is returned. If the provider's state is marked as stale (due to a dependency change),\n * it will be recomputed before returning the value.\n *\n * If the provider has not been initialized, its creation function will be executed,\n * its dependencies tracked, and the resulting value cached and returned.\n *\n * Throws an error if the scope or the specific provider state has been disposed,\n * or if a circular dependency is detected during initialization.\n *\n * @template T The type of the value provided.\n * @param {Provider<T>} provider The provider function/object to read.\n * @returns {T} The current value of the provider.\n * @throws {Error} If the scope or provider state is disposed.\n * @throws {Error} If a circular dependency is detected.\n */\n public read<T>(provider: Provider<T>): T {\n this.checkDisposed();\n\n // 1. Check local cache\n let state = this.providerStates.get(provider) as InternalState<T> | undefined;\n\n if (state) {\n // State exists in cache\n if (state.isComputing) {\n throw new Error('Circular dependency detected');\n }\n if (state.isDisposed) {\n throw new Error('Cannot read provider: its state has been disposed');\n }\n if (state.isStale) {\n // State is stale, recompute (only applicable to GenericProviderState)\n if (isStateProviderInternalState(state)) {\n // Should not happen for StateProvider, but return value if it does\n return state.stateProviderState.value;\n } else {\n // Recompute Generic/Computed provider\n return this._computeAndCacheValue(provider, state as GenericProviderState<T>);\n }\n } else {\n // State is valid and not stale, return cached value\n if (isStateProviderInternalState(state)) {\n return state.stateProviderState.value;\n } else {\n return (state as GenericProviderState<T>).value;\n }\n }\n } else {\n // State not found in cache, initialize structure and compute value\n const newState = this._createProviderStateStructure(provider);\n if (isStateProviderInternalState(newState)) {\n // StateProvider value was already computed during structure creation\n return newState.stateProviderState.value;\n } else {\n // For Generic/Computed, compute the value now using the newly created state structure\n return this._computeAndCacheValue(provider, newState as GenericProviderState<T>);\n }\n }\n }\n\n /**\n * Creates the internal state structure for a given provider within this scope,\n * but does not compute the initial value unless it's a StateProvider.\n * Sets up internal state tracking (dependencies, dependents, lifecycle callbacks).\n * This method is called internally by `read` when a provider is accessed\n * for the first time.\n *\n * @template T The type of the value provided.\n * @param {Provider<T>} provider The provider to initialize the state structure for.\n * @returns {InternalState<T>} The created internal state structure.\n * @private\n */\n private _createProviderStateStructure<T>(provider: Provider<T>): InternalState<T> {\n // --- Common setup for reader and base state ---\n const dependencies = new Set<Provider<any>>();\n const dependents = new Set<Provider<any>>();\n const disposeCallbacks = new Set<Dispose>();\n\n // Create and store a placeholder state immediately to detect cycles\n const internalId = internalStateIdCounter++; // Assign unique ID\n const placeholderState: InternalState<any> = {\n internalId,\n type: 'generic', // Placeholder type\n value: undefined,\n isComputing: true,\n isDisposed: false,\n isStale: false,\n dependencies,\n dependents,\n disposeCallbacks,\n dispose: () => { /* Placeholder dispose, real one set later */ }\n };\n this.providerStates.set(provider, placeholderState);\n\n // Define the base disposal logic here, capturing necessary variables\n const baseDisposeLogic = (stateRef: InternalState<any>, providerKey: Provider<any>) => {\n if (stateRef.isDisposed) return;\n\n // Mark dependents as stale *before* cleaning up this state\n this.markDependentsStale(providerKey, new Set());\n\n stateRef.isDisposed = true; // Mark as disposed\n\n if (isStateProviderInternalState(stateRef)) {\n stateRef.stateProviderState.listeners.clear();\n }\n stateRef.disposeCallbacks.forEach(cb => { try { cb() } catch (e) { console.error(\"Error during dispose callback:\", e)} });\n stateRef.disposeCallbacks.clear();\n\n // Clean up dependency links *before* clearing local dependencies set\n stateRef.dependencies.forEach(depProvider => {\n const depState = this.providerStates.get(depProvider);\n if (depState && 'dependents' in depState) {\n depState.dependents.delete(providerKey); // Use providerKey here\n }\n });\n // Clear local sets captured by the state object itself\n stateRef.dependencies.clear();\n stateRef.dependents.clear();\n\n // Keep the state object in the map but marked as disposed.\n };\n\n // Determine state type and create final structure\n let internalState: InternalState<T>;\n if (isStateProviderInstance<T>(provider)) {\n // State providers require their specific internal state initialized immediately\n const initReader: ScopeReader = {\n read: <P>(dep: Provider<P>) => this.read(dep),\n watch: <P>(dep: Provider<P>) => this.read(dep), // Watch behaves like read during init\n onDispose: (cb: Dispose) => disposeCallbacks.add(cb),\n };\n const stateProviderState = (provider as any)[$stateProvider].initializeState(initReader, internalId);\n internalState = {\n internalId,\n type: 'state',\n stateProviderState: stateProviderState,\n disposeCallbacks,\n dependencies, // Use the set populated by initReader\n dependents,\n isComputing: false,\n isDisposed: false,\n isStale: false, // Initial value is never stale\n dispose: () => baseDisposeLogic(internalState, provider),\n };\n } else {\n // For computed and generic providers, create a generic structure.\n internalState = {\n internalId,\n type: 'generic',\n value: undefined as T, // Value starts undefined\n disposeCallbacks,\n dependencies, // Will be populated by _computeAndCacheValue\n dependents,\n isComputing: false,\n isDisposed: false,\n isStale: true, // Mark as stale initially\n dispose: () => baseDisposeLogic(internalState, provider),\n };\n }\n\n // Replace placeholder and return the created structure\n this.providerStates.set(provider, internalState);\n return internalState;\n }\n\n /**\n * Computes or recomputes the value for a generic or computed provider,\n * updates its state, and returns the new value.\n *\n * @template T The type of the value provided.\n * @param {Provider<T>} provider The provider being computed.\n * @param {GenericProviderState<T>} state The internal state object for the provider.\n * @returns {T} The computed value.\n * @private\n */\n private _computeAndCacheValue<T>(\n provider: Provider<T>,\n state: GenericProviderState<T>\n ): T {\n // Create a reader specific to this computation\n const dependencies = new Set<Provider<any>>();\n\n // Clear old dependencies links *before* re-computation\n state.dependencies.forEach(oldDepProvider => {\n const oldDepState = this.providerStates.get(oldDepProvider);\n if (oldDepState && 'dependents' in oldDepState) {\n oldDepState.dependents.delete(provider);\n }\n });\n state.dependencies.clear(); // Clear the local set\n\n const reader: ScopeReader = {\n read: <P>(depProvider: Provider<P>): P => {\n dependencies.add(depProvider); // Track new dependency for the provider being computed\n const value = this.read(depProvider); // Delegate read (this might initialize depProvider)\n // Re-fetch the state *after* reading, ensuring it's initialized\n const depState = this.providerStates.get(depProvider);\n // Add the provider *being computed* to the dependents list of the dependency\n if (depState && 'dependents' in depState) {\n // console.log(`DEBUG _compute reader.read: Adding dependent ${state.internalId} to dependency ${depState.internalId}`); // Optional debug\n depState.dependents.add(provider); // 'provider' is the one being computed\n }\n return value;\n },\n watch: <P>(depProvider: Provider<P>): P => {\n dependencies.add(depProvider); // Track new dependency for the provider being computed\n const value = this.read(depProvider); // Delegate read (this might initialize depProvider)\n // Re-fetch the state *after* reading, ensuring it's initialized\n const depState = this.providerStates.get(depProvider);\n // Add the provider *being computed* to the dependents list of the dependency\n if (depState && 'dependents' in depState) {\n // console.log(`DEBUG _compute reader.watch: Adding dependent ${state.internalId} to dependency ${depState.internalId}`); // Optional debug\n depState.dependents.add(provider); // 'provider' is the one being computed\n }\n // TODO: Establish actual subscription?\n return value;\n },\n onDispose: (callback: Dispose): void => {\n if (state.isDisposed) return;\n state.disposeCallbacks.add(callback);\n }\n };\n\n state.isComputing = true;\n let newValue: T;\n try {\n // Determine how to get the value based on provider type\n if (isComputedProviderInstance<T>(provider)) {\n newValue = (provider as any)[$computedProvider].compute(reader);\n } else {\n newValue = provider(reader); // Generic provider\n }\n\n // Update state\n state.value = newValue;\n state.dependencies = dependencies; // Store the new dependencies\n state.isStale = false;\n state.isComputing = false;\n return newValue;\n\n } catch (error) {\n state.isComputing = false;\n // Re-throw error but leave state potentially stale/partially cleaned?\n throw error;\n }\n }\n\n /**\n * Retrieves the specialized updater function for a {@link StateProviderInstance}.\n *\n * This method ensures the provider is initialized and is indeed a `StateProvider`.\n * It returns a function that, when called, will update the provider's state\n * within this specific scope and notify listeners and dependents.\n *\n * Throws an error if the scope is disposed, the provider is not a `StateProviderInstance`,\n * or the provider state is disposed or inconsistent.\n *\n * @template T The type of the state managed by the StateProvider.\n * @param {StateProviderInstance<T>} provider The StateProviderInstance whose updater is needed.\n * @returns {StateUpdater<T>} The updater function bound to this scope and provider.\n * @throws {Error} If the scope is disposed.\n * @throws {Error} If the provider is not a valid, initialized StateProvider in this scope.\n */\n public updater<T>(provider: StateProviderInstance<T>): StateUpdater<T> {\n this.checkDisposed();\n // Check if the provider is initialized and is actually a StateProvider *before* returning the updater function.\n // Reading it ensures initialization.\n this.read(provider); // Ensure initialized and get potential errors early\n const state = this.providerStates.get(provider);\n\n // Perform the check here\n if (!isStateProviderInternalState<T>(state)) {\n throw new Error('Provider is not a StateProvider or state is inconsistent');\n }\n if (state.isDisposed) {\n // This check might be redundant if read() already threw, but keep for safety\n throw new Error('Cannot get updater for a disposed provider state');\n }\n\n\n // Return a function that performs the update using the current scope instance ('this')\n // and the specific provider key.\n return (scopeInstance: Scope, providerInstance: StateProviderInstance<T>, newValueOrFn: T | ((prev: T) => T)) => {\n // Check scope disposal *again* inside the returned function, as it might be called later\n try {\n scopeInstance.checkDisposed();\n } catch (e) {\n return;\n }\n\n const currentState = scopeInstance.providerStates.get(providerInstance) as StateProviderInternalState<T> | undefined;\n\n // Check if the state exists, is the correct type, and is not disposed\n if (!currentState || !isStateProviderInternalState(currentState) || currentState.isDisposed) {\n // Log the ID we attempted to find state for, if possible\n const attemptedId = currentState ? currentState.internalId : 'unknown';\n return;\n }\n\n\n const stateProviderState = currentState.stateProviderState;\n const previousValue = stateProviderState.value;\n\n const newValue = typeof newValueOrFn === 'function'\n ? (newValueOrFn as (prev: T) => T)(previousValue)\n : newValueOrFn;\n\n if (!Object.is(previousValue, newValue)) {\n stateProviderState.value = newValue;\n // Notify listeners from the *current* state\n const listenersToNotify = Array.from(stateProviderState.listeners);\n listenersToNotify.forEach(listener => listener());\n\n // Mark dependents as stale and notify their listeners\n scopeInstance.markDependentsStale(providerInstance, new Set());\n }\n };\n }\n\n /**\n * Recursively marks dependents of a given provider as stale and notifies\n * any listeners attached to those dependents (primarily for UI updates via `watch`).\n * This is called when a provider's state changes.\n *\n * Uses a Set to track visited providers in the current propagation chain\n * to prevent infinite loops in potential (though ideally non-existent)\n * circular dependency scenarios during notification.\n *\n * @param {Provider<any>} provider The provider whose dependents should be marked stale.\n * @param {Set<Provider<any>>} visited Set of providers already visited in this notification chain.\n * @private\n */\n private markDependentsStale(provider: Provider<any>, visited: Set<Provider<any>>): void {\n // DEBUG: Log entry and the provider being processed\n const providerState = this.providerStates.get(provider);\n // console.log(`DEBUG: Entering markDependentsStale for provider (Internal ID: ${providerState?.internalId ?? 'N/A'})`);\n if (visited.has(provider)) {\n return; // Avoid infinite loops\n }\n visited.add(provider);\n\n const state = this.providerStates.get(provider);\n if (!state || state.isDisposed) return;\n\n state.dependents.forEach(dependentProvider => {\n const dependentState = this.providerStates.get(dependentProvider);\n // Check if dependent exists and is not disposed\n if (dependentState && !dependentState.isDisposed) {\n // Mark stale unconditionally if it wasn't already.\n // This ensures the staleness propagates even if listeners aren't notified directly.\n const wasAlreadyStale = dependentState.isStale;\n if (!wasAlreadyStale) {\n // console.log(`DEBUG markDependentsStale: Marking dependent ${dependentState.internalId} as stale.`); // DEBUG\n dependentState.isStale = true;\n }\n\n // Notify listeners if it's a StateProvider (listeners are only stored there currently)\n if (isStateProviderInternalState(dependentState)) {\n const listeners = dependentState.stateProviderState.listeners;\n if (listeners.size > 0) {\n // console.log(`DEBUG markDependentsStale: Notifying ${listeners.size} listeners of stale StateProvider ${dependentState.internalId}.`); // DEBUG\n // Convert to array before iterating in case a listener modifies the set during iteration\n Array.from(listeners).forEach(listener => listener());\n }\n }\n\n // Recursively mark *its* dependents as stale\n this.markDependentsStale(dependentProvider, visited);\n }\n });\n }\n /**\n * Subscribes a listener function to changes in a specific provider's state within this scope.\n *\n * Currently, only {@link StateProviderInstance} actively supports notifications. Watching\n * other provider types might read the initial value but won't trigger the callback\n * on changes (unless they are dependents of a changing StateProvider, triggering staleness).\n *\n * Ensures the provider is initialized before attempting to add the listener.\n * Implements auto-disposal: when the last listener for a provider unsubscribes,\n * the provider's internal state is disposed.\n *\n * @template T The type of the value provided.\n * @param {Provider<T>} provider The provider to watch.\n * @param {() => void} callback The function to call when the provider's state changes.\n * @returns {Dispose} A function to call to unsubscribe the listener.\n */\n public watch<T>(provider: Provider<T>, callback: () => void): Dispose {\n this.checkDisposed();\n\n // Ensure the provider is initialized by reading it first.\n // This also retrieves the current state if already initialized.\n this.read(provider);\n const state = this.providerStates.get(provider);\n\n if (!state || state.isDisposed) {\n // Should not happen if read() succeeded, but check for safety.\n return () => {}; // Return a no-op unsubscribe function\n }\n\n // Currently, only StateProviders actively notify listeners.\n // Other provider types (computed, future) will need different mechanisms.\n // TODO: Generalize listener handling? Add listeners set to BaseInternalState?\n if (isStateProviderInternalState(state)) {\n const listeners = state.stateProviderState.listeners;\n listeners.add(callback);\n\n // Return the unsubscribe function\n return () => {\n listeners.delete(callback);\n // Auto-dispose if listener count drops to zero\n if (listeners.size === 0) {\n // We need to access the state again here, using the provider key\n // The 'state' variable from the outer scope should still be valid here\n // Check if it still exists and hasn't been disposed by other means\n if (state && !state.isDisposed) {\n // console.log(`DEBUG: Auto-disposing state due to zero listeners (Internal ID: ${state.internalId})`); // Optional debug\n state.dispose(); // Dispose the internal state\n }\n }\n };\n } else {\n // For generic/computed providers, there's nothing to watch directly yet.\n // Re-computation happens via staleness check in `read`.\n // Return a no-op unsubscribe.\n return () => {};\n }\n }\n\n\n /**\n * Disposes of the scope, cleaning up all provider states created within it.\n *\n * This involves:\n * - Marking the scope itself as disposed.\n * - Iterating through all active provider states in the scope.\n * - Calling the `dispose` method on each provider state, which in turn:\n * - Clears internal listeners (for StateProviders).\n * - Executes any `onDispose` callbacks registered during provider initialization.\n * - Cleans up dependency/dependent links.\n * - Clearing the internal map of provider states.\n *\n * Once disposed, the scope and its provider states should not be used further.\n */\n public dispose(): void {\n if (this.isDisposed) {\n return;\n }\n this._isDisposed = true; // Use internal flag\n\n // Dispose providers in reverse order of initialization? Not strictly necessary\n // if dependencies are handled correctly, but might prevent some issues.\n // For now, dispose in map iteration order.\n // Create a copy of states to dispose, as disposal modifies the map\n const statesToDispose = Array.from(this.providerStates.values());\n statesToDispose.forEach(state => {\n // Ensure dispose is called only once via the state's internal flag\n if (!state.isDisposed) {\n state.dispose();\n }\n });\n\n this.providerStates.clear();\n this.parent = null; // Break potential reference cycle\n // console.log(\"Scope disposed\");\n }\n}\n\n/**\n * Factory function to create a new {@link Scope}.\n * @param {Scope | null} [parent=null] - An optional parent scope.\n * @returns {Scope} A new Scope instance.\n */\nexport function createScope(parent: Scope | null = null): Scope {\n return new Scope(parent);\n}\n\n// Example usage (conceptual)\n// const scope = createScope();\n// const value = scope.read(myProvider);\n// scope.dispose();"]}