use-app-events
Version:
Create custom events and reactive variables in vanilla JavaScript and React.
21 lines (20 loc) • 1.15 kB
TypeScript
export type UseGlobalHook = {
<Value, EventType extends string = string>(eventType: EventType, initialValue: Value, options?: UseGlobalOptions): [Value, UpdateValueAction<Value, true>];
<Value, EventType extends string = string>(eventType: EventType, initialValue?: undefined, options?: UseGlobalOptions): [Value | undefined, UpdateValueAction<Value, false>];
};
export type UpdateValueAction<Value, HasInitialValue extends boolean = boolean> = {
(nextValue: Value): void;
(nextValue: Partial<Value>): void;
(nextValue: (prevValue: HasInitialValue extends true ? Value : Value | undefined) => Value): void;
};
export type UseGlobalOptions = {
/** When true, enables a debug mode, resulting in additional logs in non-production environment. */
debug?: boolean;
/**
* When true, the initial value will be set to the latest value retrieved from the older instances of this hook.
*
* **_Note: the initial synchronization doesn't work if the initial value is provided, because the hook will prioritize it over the value from the older instances._**
* @default true
*/
synchronize?: boolean;
};