UNPKG

@shtse8/fluxus

Version:

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

1 lines 3.78 kB
{"version":3,"sources":["/home/runner/work/fluxus/fluxus/dist/index.cjs","../src/types.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;AC4DO,IAAM,UAAA,EAAY,MAAA,CAAO,GAAA,CAAI,iBAAiB,CAAA;AAU9C,SAAS,UAAA,CAAc,GAAA,EAAkC;AAC5D,EAAA,OAAO,OAAO,IAAA,IAAQ,UAAA;AAC1B;ADnEA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,sZAAC","file":"/home/runner/work/fluxus/fluxus/dist/index.cjs","sourcesContent":[null,"/**\n * Represents a function that can be called to clean up resources or subscriptions.\n * @returns {void}\n */\nexport type Dispose = () => void;\n\n/**\n * Represents an object that holds a disposable resource or manages a lifecycle\n * that requires explicit cleanup.\n */\nexport interface Disposable {\n /**\n * Disposes of the resource or ends the lifecycle managed by this object.\n * Calling dispose multiple times should be safe (idempotent).\n */\n dispose: Dispose;\n}\n\n/**\n * Provides read access to other providers within the current scope and methods\n * for managing the lifecycle of the current provider's state.\n * Passed to the provider's creation function.\n */\nexport interface ScopeReader {\n /**\n * Reads the current value of a provider without subscribing to updates.\n * If the provider is not yet initialized in the scope, it will be created.\n * @param provider The provider to read.\n * @template P The type of the value provided by the dependency provider.\n * @param provider The dependency provider to read.\n * @returns The current value of the dependency provider within the current scope.\n */\n read<T>(provider: Provider<T>): T;\n\n /**\n * Watches the value of a provider and subscribes to updates.\n * This is typically used by reactive providers or UI bindings.\n * @template P The type of the value provided by the dependency provider.\n * @param provider The dependency provider to watch.\n * @returns The current value of the provider.\n */\n watch<T>(provider: Provider<T>): T; // Implementation detail: watching implies reading\n\n /**\n * Adds a cleanup function to be called when the provider's state\n * associated with the current scope is disposed.\n * @param callback The cleanup function to be executed.\n */\n onDispose(callback: Dispose): void;\n}\n\n/**\n * The core building block of Fluxus. A Provider defines how to create a value\n * within a specific scope. Providers are functions or objects that encapsulate\n * state creation logic.\n *\n * Providers are typically created using factory functions like `stateProvider`,\n * `computedProvider`, etc.\n *\n * They are identified by object identity, meaning you don't register them\n * with strings; you use the provider function/object itself as the key.\n *\n * @template T The type of the value created by the provider.\n * @param reader A {@link ScopeReader} instance to interact with the scope.\n * @returns The created value of type T.\n */\nexport type Provider<T> = (reader: ScopeReader) => T;\n\n/** A unique symbol to identify providers internally. */\nexport const $provider = Symbol.for('fluxus.provider');\n\n/**\n * A basic type guard to check if an unknown value is potentially a Fluxus provider.\n * Note: This is a very basic check and might need refinement if provider\n * structures become more complex (e.g., objects with specific methods).\n * @template T The potential type provided by the provider.\n * @param obj The value to check.\n * @returns True if the value is a function (the basic form of a provider), false otherwise.\n */\nexport function isProvider<T>(obj: unknown): obj is Provider<T> {\n return typeof obj === 'function';\n}\n\n// More specific provider types (like StateProvider, ComputedProvider) will extend or utilize this base concept."]}