UNPKG

use-mutable-source

Version:

Minimal and elegant way to integrate any library with React

22 lines (21 loc) 1.63 kB
import { type DependencyList } from 'react'; /** * Generates a source, and allows to use it safely inside any component. * * @param init - function that generates the source, it can optionally returns also a "destroy" function * @param getSnapshot - function that returns an immutable atomic snapshot of the source * @param subscribe - function to register a callback that is called whenever the source changes * @param subscribeDeps - dependency list that defines the "subscribe" lifecycle * @returns the snapshot and a function to lazily initialize the source */ export declare function useSource<Source, Snapshot>(init: () => [source: Source, destroy?: (source: Source) => void], getSnapshot: (source: Source | null, currentSnapshot: Snapshot | null) => Snapshot, subscribe: (source: Source, onChange: () => void) => () => void, subscribeDeps?: DependencyList): readonly [Snapshot, () => Source]; /** * Generates a pure source, and allows to use it safely inside any component. * The source generation should be side-effect free. * * @param init - function that generates the source * @param getSnapshot - function that returns an immutable atomic snapshot of the source * @param subscribe - function to register a callback that is called whenever the source changes * @returns the snapshot and the source */ export declare function usePureSource<Source, Snapshot>(init: () => Source, getSnapshot: (source: Source, currentSnapshot: Snapshot | null) => Snapshot, subscribe: (source: Source, onChange: () => void) => () => void, subscribeDeps?: DependencyList): readonly [Snapshot, Source];