use-mutable-source
Version:
Minimal and elegant way to integrate any library with React
21 lines (20 loc) • 2.05 kB
TypeScript
import { type DependencyList } from 'react';
/**
* Generates a source, and allows to use it safely inside any component. The
* source is re-created every time a dependency changes.
*
* @param init - function that generates the source, it can optionally returns also a "destroy" function
* @param deps - dependency list that defines the source lifecycle
* @returns an hook to derive snapshots and a function to lazily initialize the source
*/
export declare function useSource<Source>(init: () => [source: Source, destroy?: (source: Source) => void], deps?: DependencyList): readonly [<Snapshot>(getSnapshot: (source: Source | null, currentSnapshot: Snapshot | null) => Snapshot, ...rest: [subscribe: (source: Source, onChange: () => void) => () => void] | [(source: Source, onChange: () => void) => () => void, (DependencyList | undefined)?] | [DependencyList | undefined, (source: Source, onChange: () => void) => () => void] | [DependencyList | undefined, (source: Source, onChange: () => void) => () => void, (DependencyList | undefined)?]) => Snapshot, () => Source];
/**
* Generates a pure source, and allows to use it safely inside any component.
* The source is re-created every time a dependency changes.
* The source generation should be side-effect free.
*
* @param init - function that generates the source
* @param deps - dependency list that define the source lifecycle
* @returns an hook to derive snapshots and the source
*/
export declare function usePureSource<Source>(init: () => Source, deps?: DependencyList): readonly [<Snapshot>(getSnapshot: (source: Source, currentSnapshot: Snapshot | null) => Snapshot, ...rest: [subscribe: (source: Source, onChange: () => void) => () => void] | [(source: Source, onChange: () => void) => () => void, (DependencyList | undefined)?] | [DependencyList | undefined, (source: Source, onChange: () => void) => () => void] | [DependencyList | undefined, (source: Source, onChange: () => void) => () => void, (DependencyList | undefined)?]) => Snapshot, Source];