react-hooks-global-states
Version:
This is a package to easily handling global-state across your react-components using hooks.
118 lines (117 loc) • 6.42 kB
TypeScript
import type { ActionCollectionConfig, StateSetter, GlobalStoreCallbacks, ActionCollectionResult, MetadataSetter, UseHookConfig, StateGetter, SelectorCallback, SubscriberParameters, MetadataGetter, StateHook, BaseMetadata, StateChanges, StoreTools, ObservableFragment } from './types';
import { UniqueSymbol } from './uniqueSymbol';
/**
* The GlobalStore class is the main class of the library and it is used to create a GlobalStore instances
* */
export declare class GlobalStore<State, Metadata extends BaseMetadata | unknown, ActionsConfig extends ActionCollectionConfig<State, Metadata> | undefined | unknown, PublicStateMutator = keyof ActionsConfig extends never | undefined ? StateSetter<State> : ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>> {
protected _name: string;
protected wasDisposed: boolean;
actionsConfig: ActionsConfig | null;
callbacks: GlobalStoreCallbacks<State, Metadata> | null;
metadata: Metadata;
actions: ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>> | null;
subscribers: Map<string, SubscriberParameters>;
stateWrapper: {
state: State;
};
constructor(state: State);
constructor(state: State, args: {
metadata?: Metadata;
callbacks?: GlobalStoreCallbacks<State, Metadata>;
actions?: ActionsConfig;
name?: string;
});
protected onInit?: (args: StoreTools<State, Metadata>) => void;
protected onStateChanged?: (args: StoreTools<State, Metadata> & StateChanges<State>) => void;
protected onSubscribed?: (args: StoreTools<State, Metadata>) => void;
protected computePreventStateChange?: (parameters: StoreTools<State, Metadata> & StateChanges<State>) => boolean;
protected initialize: () => Promise<void>;
protected executeSetStateForSubscriber: (subscription: SubscriberParameters, args: {
forceUpdate: boolean | undefined;
newRootState: State;
currentRootState: State | UniqueSymbol;
identifier: string | undefined;
}) => {
didUpdate: boolean;
};
/**
* set the state and update all the subscribers
* @param {StateSetter<State>} setter - The setter function or the value to set
* */
protected setState: ({ state: newRootState, }: {
state: State;
}, { forceUpdate, identifier }: {
forceUpdate?: boolean;
identifier?: string;
}) => void;
/**
* Set the value of the metadata property, this is no reactive and will not trigger a re-render
* @param {MetadataSetter<Metadata>} setter - The setter function or the value to set
* */
protected setMetadata: MetadataSetter<Metadata>;
protected getMetadata: () => Metadata;
getState: StateGetter<State>;
/**
* get the parameters object to pass to the callback functions:
* onInit, onStateChanged, onSubscribed, computePreventStateChange
* */
getConfigCallbackParam: () => StoreTools<State, Metadata>;
protected lastSubscriptionId: string | null;
protected setOrUpdateSubscription: (subscription: SubscriberParameters) => {
isNewSubscription?: boolean;
};
protected partialUpdateSubscription: (subscriptionId: string, values: Partial<SubscriberParameters>) => void;
protected executeOnSubscribed: () => void;
/**
* Returns a custom hook that allows to handle a global state
* @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
* */
getHook: () => StateHook<State, PublicStateMutator, Metadata>;
protected computeSelectedState: ({ selector, subscriptionId, config, currentDependencies, computeChildState, stateWrapperRef, }: {
selector: SelectorCallback<unknown, unknown> | undefined;
subscriptionId: string;
config: UseHookConfig<unknown, unknown>;
currentDependencies: unknown[] | undefined;
computeChildState: () => {
state: unknown;
};
stateWrapperRef: {
state: unknown;
};
}) => unknown;
/**
* @description
* Use this function to create a custom global hook which contains a fragment of the state of another hook
*/
createSelectorHook: <RootState, StateMutator, Metadata extends BaseMetadata, RootSelectorResult, RootDerivate = RootSelectorResult extends never ? RootState : RootSelectorResult>(mainSelector: (state: RootState) => RootSelectorResult, { isEqualRoot: mainIsEqualRoot, isEqual: mainIsEqualFun, name: selectorName, }?: {
isEqual?: (current: RootDerivate, next: RootDerivate) => boolean;
isEqualRoot?: (current: RootState, next: RootState) => boolean;
name?: string;
}) => StateHook<RootDerivate, StateMutator, Metadata_1>;
stateControls: () => [retriever: StateGetter<State>, mutator: PublicStateMutator, metadata: MetadataGetter<Metadata>];
/**
* Returns the state setter or the actions map
* @returns {StateMutator} - The state setter or the actions map
* */
protected getStateOrchestrator: () => PublicStateMutator;
/**
* This is responsible for defining whenever or not the state change should be allowed or prevented
* the function also execute the functions:
* - onStateChanged (if defined) - this function is executed after the state change
* - computePreventStateChange (if defined) - this function is executed before the state change and it should return a boolean value that will be used to determine if the state change should be prevented or not
*/
protected setStateWrapper: StateSetter<State>;
/**
* This creates a map of actions that can be used to modify or interact with the state
* @returns {ActionCollectionResult<State, Metadata, StateMutator>} - The actions map result of the configuration object passed to the constructor
* */
getStoreActionsMap: () => null | ActionCollectionResult<State, Metadata, NonNullable<ActionsConfig>>;
createObservable<Fragment>(mainSelector: (state: State) => Fragment, { isEqualRoot: mainIsEqualRoot, isEqual: mainIsEqualFun, name: selectorName, }?: {
isEqual?: (current: Fragment, next: Fragment) => boolean;
isEqualRoot?: (current: State, next: State) => boolean;
name?: string;
}): ObservableFragment<Fragment>;
removeSubscriptions: () => void;
dispose: () => void;
}
export default GlobalStore;