@furystack/shades
Version:
A lightweight UI framework for FuryStack with JSX support
45 lines • 2.75 kB
TypeScript
import type { ValueChangeCallback, ValueObserver, ValueObserverOptions } from '@furystack/utils';
import { ObservableValue } from '@furystack/utils';
/**
* Class for managing observables and disposables for components, based on key-value maps
*/
export declare class ResourceManager implements AsyncDisposable {
private readonly disposables;
private readonly disposableDeps;
/**
* Returns an existing disposable resource by key, or creates and caches a new one.
* Resources are automatically disposed when the component is removed from the DOM.
* When `deps` is provided, the resource is re-created (and the old one disposed) whenever
* the serialized deps value changes. This is useful for resources that depend on dynamic
* parameters (e.g., entity-sync subscriptions with changing query options).
* @param key Unique key for caching this resource
* @param factory Factory function called once to create the resource
* @param deps Optional dependency array -- when deps change, the old resource is disposed and a new one is created.
* Values are compared via `JSON.stringify`, so `undefined` and `null` are treated as equal within arrays.
* @returns The cached or newly created resource
*/
useDisposable<T extends Disposable | AsyncDisposable>(key: string, factory: () => T, deps?: readonly unknown[]): T;
readonly observers: Map<string, ValueObserver<any>>;
/**
* Subscribes to an observable value by key. If the observable changes between renders,
* the previous subscription is disposed and a new one is created.
* @param key Unique key for caching this subscription
* @param observable The observable to subscribe to
* @param onChange Callback invoked when the value changes
* @param options Additional observer options
* @returns Tuple of [currentValue, setValue]
*/
useObservable: <T>(key: string, observable: ObservableValue<T>, onChange: ValueChangeCallback<T>, options?: ValueObserverOptions<T>) => [value: T, setValue: (newValue: T) => void];
readonly stateObservers: Map<string, ObservableValue<any>>;
/**
* Creates or retrieves a local state observable by key.
* State is persisted across re-renders and disposed with the component.
* @param key Unique key for caching this state
* @param initialValue Initial value used on first call
* @param callback Callback invoked when the state changes
* @returns Tuple of [currentValue, setValue]
*/
useState: <T>(key: string, initialValue: T, callback: ValueChangeCallback<T>) => [value: T, setValue: (newValue: T) => void];
[Symbol.asyncDispose](): Promise<void>;
}
//# sourceMappingURL=resource-manager.d.ts.map