@zedux/core
Version:
A high-level, declarative, composable form of Redux
149 lines (126 loc) • 5.63 kB
TypeScript
import { ActionChain, Dispatchable, EffectsSubscriber, HierarchyConfig, HierarchyDescriptor, RecursivePartial, Settable, Subscriber, Scheduler, KnownHierarchyDescriptor } from '../types.js';
/**
Creates a new Zedux store.
*/
export declare const createStore: {
<State = any>(initialHierarchy?: HierarchyDescriptor<State>, initialState?: State): Store<State>;
<State = any>(initialHierarchy: null | undefined, initialState: State): Store<State>;
};
export declare class Store<State = any> {
/**
Used by the store's branch reducers in the generated reducer hierarchy to
interact with the hierarchical data type returned by the store's reducers.
This "hierarchical data type" is a plain object by default. But these
hierarchy config options can teach Zedux how to use an Immutable `Map` or
any recursive, map-like data structure.
*/
static readonly hierarchyConfig: HierarchyConfig;
static readonly $$typeof: symbol;
/**
* This is set by atom ecosystems to automaticallly tie stores created during
* atom evaluation to the ecosystem.
*/
static _scheduler?: Scheduler;
private _state;
private _isDispatching?;
/**
* We can optimize some things if this store is never composed. Disable optimizations as soon as this store is used as a parent or child store.
*/
private _isSolo;
private _parents?;
private _rootReducer?;
private _scheduler;
private _subscribers;
private _tree?;
constructor(initialHierarchy?: HierarchyDescriptor<State>, initialState?: State);
actionStream(): {
[Symbol.observable](): any;
'@@observable'(): any;
subscribe: (subscriber: {
complete?: (() => void) | undefined;
error?: ((error: unknown) => void) | undefined;
next?: ((action: ActionChain) => void) | undefined;
} | ((action: ActionChain) => void)) => {
unsubscribe: () => void;
};
};
/**
Dispatches an action to the store.
The action will be sent through this store's reducer hierarchy (if any) and
passed on to any child stores after being wrapped in `inherit` meta nodes
The resulting state will be returned synchronously from this call.
This is a bound function property. Every store recreates this small
function. But it's always bound and can be passed around easily.
*/
dispatch: (action: Dispatchable) => State;
/**
Returns the current state of the store.
Do not mutate the returned value.
*/
getState(): State;
/**
Applies a full hydration to the store.
Accepts either the new state or a function that accepts the current state
and returns the new state.
Dispatches the special `hydrate` action to the store's reducers. Effects
subscribers can inspect and record this action to implement time travel.
The `hydrate` action's `payload` property will be set to the new state. The
action's `meta` property will be set to the passed meta, if any.
Throws an error if called from the reducer layer.
Returns the new state.
Unlike setStateDeep, setState is a bound function property. Every store
recreates this small function. But it's always bound and can be passed
around easily.
*/
setState: (settable: Settable<State>, meta?: any) => State;
/**
Applies a partial state update to the store.
Accepts either a deep partial state object or a function that accepts the
current state and returns a deep partial state object.
This method only recursively traverses normal JS objects. If your store
deeply nests any other data structure, including arrays or maps, you'll have
to deeply merge them yourself using `store.setState()`.
Dispatches the special `merge` action to the store's reducers. This action's
`payload` property will be set to the resolved partial state update. Effects
subscribers can record this action to implement time travel.
IMPORTANT: Deep setting cannot remove properties from the state tree. Use
`store.setState()` for that.
Throws an error if called from the reducer layer.
Returns the new state.
Unlike setState, setStateDeep is not bound. You must call it with context -
e.g. by using dot-notation: `store.setStateDeep(...)`
*/
setStateDeep(settable: Settable<RecursivePartial<State>, State>, meta?: any): State;
/**
Registers a subscriber with the store.
The subscriber will be notified every time the store's state
changes.
Returns a subscription object. Calling `subscription.unsubscribe()`
unregisters the subscriber.
*/
subscribe(subscriber: Subscriber<State, this>): {
unsubscribe: () => void;
};
/**
Merges a hierarchy descriptor into the existing hierarchy descriptor.
Intelligently diffs the two hierarchies and only creates/recreates the
necessary reducers.
Dispatches the special `prime` action to the store.
*/
use(newHierarchy: KnownHierarchyDescriptor<State>): this;
/**
* Only for internal use.
*/
_register(effects: EffectsSubscriber): () => void;
[Symbol.observable](): this;
'@@observable'(): this;
private _dispatch;
private _dispatchAction;
private _dispatchHydration;
private _dispatchStateSetter;
private _doNotify;
private _notify;
private _registerChildStore;
private _routeAction;
private _setState;
}