@dr.pogodin/react-global-state
Version:
Hook-based global state for React
108 lines (107 loc) • 5.21 kB
TypeScript
import type SsrContext from './SsrContext';
import { type CallbackT, type ForceT, type LockT, type TypeLock, type ValueAtPathT, type ValueOrInitializerT } from './utils';
type GetOptsT<T> = {
initialState?: boolean;
initialValue?: ValueOrInitializerT<T>;
};
export default class GlobalState<StateT, SsrContextT extends SsrContext<StateT> = SsrContext<StateT>> {
#private;
readonly ssrContext?: SsrContextT;
/**
* Creates a new global state object.
* @param initialState Intial global state content.
* @param ssrContext Server-side rendering context.
*/
constructor(initialState: StateT, ssrContext?: SsrContextT);
/**
* Returns the number of currently registered async data abort callbacks,
* just for the sake of testing the library.
*/
get numAsyncDataAbortCallbacks(): number;
/**
* If `aborted` is "true" and there is an abort callback registered for
* the specified operation, it triggers the callback. Then, in any case,
* it drops the callback.
*/
asyncDataLoadDone(opid: string, aborted: boolean): void;
/**
* Drops the record of dependencies, if any, for the given path.
*/
dropDependencies(path: string): void;
/**
* Checks if given `deps` are different from previously recorded ones for
* the given `path`. If they are, `deps` are recorded as the new deps for
* the `path`, and also the array is frozen, to prevent it from being
* modified.
*
* TODO: This may not work as expected if path string is not normalized,
* and the for the same path different alternative ways to spell it down
* are used. We should normalize given path here, I guess, or on a higher
* level in the logic?
*/
hasChangedDependencies(path: string, deps: unknown[]): boolean;
/**
* Gets entire state, the same way as .get(null, opts) would do.
* @param opts.initialState
* @param opts.initialValue
*/
getEntireState(opts?: GetOptsT<StateT>): StateT;
/**
* Notifies all connected state watchers that a state update has happened.
*/
private notifyStateUpdate;
/**
* Registers an abort callback for an async data retrieval operation with
* the given operation ID. Throws if already registered.
*/
setAsyncDataAbortCallback(opid: string, cb: () => void): void;
/**
* Sets entire state, the same way as .set(null, value) would do.
* @param value
*/
setEntireState(value: StateT): StateT;
/**
* Gets current or initial value at the specified "path" of the global state.
* @param path Dot-delimitered state path.
* @param options Additional options.
* @param options.initialState If "true" the value will be read
* from the initial state instead of the current one.
* @param options.initialValue If the value read from the "path" is
* "undefined", this "initialValue" will be returned instead. In such case
* "initialValue" will also be written to the "path" of the current global
* state (no matter "initialState" flag), if "undefined" is stored there.
* @return Retrieved value.
*/
get(): StateT;
get<PathT extends null | string | undefined, ValueArgT extends ValueAtPathT<StateT, PathT, never>, ValueResT extends ValueAtPathT<StateT, PathT, void>>(path: PathT, opts?: GetOptsT<ValueArgT>): ValueResT;
get<Forced extends ForceT | LockT = LockT, ValueT = void>(path?: null | string, opts?: GetOptsT<TypeLock<Forced, never, ValueT>>): TypeLock<Forced, void, ValueT>;
/**
* Writes the `value` to given global state `path`.
* @param path Dot-delimitered state path. If not given, entire
* global state content is replaced by the `value`.
* @param value The value.
* @return Given `value` itself.
*/
set<PathT extends null | string | undefined, ValueArgT extends ValueAtPathT<StateT, PathT, never>, ValueResT extends ValueAtPathT<StateT, PathT, void>>(path: PathT, value: ValueArgT): ValueResT;
set<Forced extends ForceT | LockT = LockT, ValueT = never>(path: null | string | undefined, value: TypeLock<Forced, never, ValueT>): TypeLock<Forced, void, ValueT>;
/**
* Unsubscribes `callback` from watching state updates; no operation if
* `callback` is not subscribed to the state updates.
* @param callback
* @throws if {@link SsrContext} is attached to the state instance: the state
* watching functionality is intended for client-side (non-SSR) only.
*/
unWatch(callback: CallbackT): void;
/**
* Subscribes `callback` to watch state updates; no operation if
* `callback` is already subscribed to this state instance.
* @param callback It will be called without any arguments every
* time the state content changes (note, howhever, separate state updates can
* be applied to the state at once, and watching callbacks will be called once
* after such bulk update).
* @throws if {@link SsrContext} is attached to the state instance: the state
* watching functionality is intended for client-side (non-SSR) only.
*/
watch(callback: CallbackT): void;
}
export {};