UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

90 lines (89 loc) 3.48 kB
import type { Context } from 'react'; export type SharedStateId = string | (() => void) | Promise<() => void> | Context<any> | Record<string, unknown>; /** * The shared state will be deleted when all components have been unmounted. */ export declare function useWeakSharedState<Data>(id: SharedStateId | undefined, /** The initial data for the shared state. */ initialData?: Data, /** Optional callback function to be called when the shared state is set from another instance/component. */ onChange?: any): { get: () => Data; data: Data; hadInitialData: boolean; update: (newData: Data | Partial<Data>, opts?: Options) => void; set: (newData: Data | Partial<Data>, opts?: Options) => boolean; extend: (newData: Data | Partial<Data>, opts?: Options) => boolean; }; /** * Custom hook that provides shared state functionality. */ export declare function useSharedState<Data>( /** The identifier for the shared state. */ id: SharedStateId | undefined, /** The initial data for the shared state. */ initialData?: Data, /** Optional callback function to be called when the shared state is set from another instance/component. */ onChange?: any, /** Optional configuration options. */ { /** When set to `true`, the shared state will be deleted when all components have been unmounted. */ weak, }?: { weak?: boolean; }): { get: () => Data; data: Data; hadInitialData: boolean; update: (newData: Data | Partial<Data>, opts?: Options) => void; set: (newData: Data | Partial<Data>, opts?: Options) => boolean; extend: (newData: Data | Partial<Data>, opts?: Options) => boolean; }; type Subscriber = (() => void) & { ref?: unknown; }; export type SharedStateReturn<Data = undefined> = { data: Data; get: () => Data; set: (newData: Data | Partial<Data>, opts?: Options) => boolean; extend: (newData: Data | Partial<Data>, opts?: Options) => boolean; update: (newData: Data | Partial<Data>, opts?: Options) => void; subscribersRef?: { current: Subscriber[]; }; }; type SharedStateInstance<Data> = { subscribe: (subscriber: Subscriber) => void; unsubscribe: (subscriber: Subscriber) => void; hadInitialData: boolean; } & SharedStateReturn<Data>; type Options = { preventSyncOfSameInstance?: boolean; callerRef?: unknown; forceSync?: boolean; silent?: boolean; }; /** * Creates a shared state instance with the specified ID and initial data. */ export declare function createSharedState<Data>( /** The identifier for the shared state. */ id: SharedStateId, /** The initial data for the shared state. */ initialData?: Data): SharedStateInstance<Data>; /** * Pre-seed a shared state with data silently — without notifying subscribers, * without triggering useSyncExternalStore subscriptions, and crucially without * setting `hadInitialData = true`. This lets subsequent `Form.useData(id, data)` * calls still merge their own initialData, which `createSharedState(id, data)` * would prevent by setting `hadInitialData = true`. * * Safe to call during the render phase when no subscribers exist yet. */ export declare function preSeedSharedState<Data>(id: SharedStateId, data: Data): void; /** * Creates a reference key for the shared state. * You can pass any JavaScript instance as the reference. */ export declare function createReferenceKey(ref1: any, ref2: any): any; export declare function shallowEqual(a: unknown, b: unknown): boolean; export {};