UNPKG

6.34 kBTypeScriptView Raw
1import { Observable, Subscription } from "rxjs";
2import { CleanupState, Reducer, StateChangeNotification } from "./types";
3export declare class Store<S> {
4 /**
5 * Observable that emits when the store was destroyed using the .destroy() function.
6 */
7 readonly destroyed: Observable<void>;
8 private readonly state;
9 get currentState(): S;
10 /**
11 * All reducers always produce a state mutation of the original root store type R;
12 * However, we only now type R for the root store; all other stores may have different type
13 * so we use any here as the root type.
14 */
15 private readonly stateMutators;
16 /**
17 * A list of transformation functions that will transform the state to different projections
18 * and backwards. Use for scoped reducers.
19 */
20 private readonly forwardProjections;
21 private readonly backwardProjections;
22 /**
23 * Is completed when the slice is unsubscribed and no longer needed.
24 */
25 private readonly _destroyed;
26 /**
27 * Used for manual dispatches without observables
28 */
29 private readonly actionDispatch;
30 private readonly stateChangeNotificationSubject;
31 /**
32 * Only used for debugging purposes (so we can bridge Redux Devtools to the store)
33 * Note: Do not use in day-to-day code, use .select() instead.
34 */
35 readonly stateChangedNotification: Observable<StateChangeNotification>;
36 private constructor();
37 /**
38 * Create a new Store based on an initial state
39 */
40 static create<S>(initialState?: S): Store<S>;
41 /**
42 * Creates a new linked store, that Selects a slice on the main store.
43 * @deprecated
44 */
45 createSlice<K extends keyof S>(key: K, initialState?: S[K], cleanupState?: CleanupState<S[K]>): Store<S[K]>;
46 /**
47 * Create a clone of the store which holds the same state. This is an alias to createProjection with
48 * the identity functions as forward/backwards projection. Usefull to unsubscribe from select()/watch()
49 * subscriptions as the destroy() event is specific to the new cloned instance (=will not destroy the original)
50 * Also usefull to scope string-based action dispatches to .dispatch() as action/reducers pairs added to the
51 * clone can not be dispatched by the original and vice versa.
52 */
53 clone(): Store<S>;
54 /**
55 * Creates a new slice of the store. The slice holds a transformed state that is created by applying the
56 * forwardProjection function. To transform the slice state back to the parent state, a backward projection
57 * function must be given.
58 * @param forwardProjection - Projection function that transforms a State S to a new projected state TProjectedState
59 * @param backwardProjection - Back-Projection to obtain state S from already projected state TProjectedState
60 * @param initial - Function to be called initially with state S that must return an initial state to use for TProjected
61 * @param cleanup - Function to be called when the store is destroyed to return a cleanup state based on parent state S
62 */
63 createProjection<TProjectedState>(forwardProjection: (state: S) => TProjectedState, backwardProjection: (state: TProjectedState, parentState: S) => S, initial?: (state: S) => TProjectedState, cleanup?: (state: TProjectedState, parentState: S) => S): Store<TProjectedState>;
64 /**
65 * Adds an Action/Reducer pair. This will make the reducer become active whenever the action observable emits a
66 * value.
67 * @param action An observable whose payload will be passed to the reducer on each emit, or a string identifier
68 * as an action name. In the later case, .dispatch() can be used to manually dispatch actions based
69 * on their string name.
70 * @param reducer A reducer function. @see Reducer
71 * @param actionName An optional name (only used during development/debugging) to assign to the action when an
72 * Observable is passed as first argument. Must not be specified if the action argument is a string.
73 */
74 addReducer<P>(action: Observable<P> | string, reducer: Reducer<S, P>, actionName?: string): Subscription;
75 /**
76 * Selects a part of the state using a selector function. If no selector function is given, the identity function
77 * is used (which returns the state of type S).
78 * Note: The returned observable does only update when the result of the selector function changed
79 * compared to a previous emit. A shallow copy test is performed to detect changes.
80 * This requires that your reducers update all nested properties in
81 * an immutable way, which is required practice with Redux and also with reactive-state.
82 * To make the observable emit any time the state changes, use .select() instead
83 * For correct nested reducer updates, see:
84 * http://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html#updating-nested-objects
85 *
86 * @param selectorFn A selector function which returns a mapped/transformed object based on the state
87 * @returns An observable that emits the result of the selector function after a
88 * change of the return value of the selector function
89 */
90 watch<T = S>(selectorFn?: (state: S) => T): Observable<T>;
91 /**
92 * Same as .watch() except that EVERY state change is emitted. Use with care, you might want to pipe the output
93 * to your own implementation of .distinctUntilChanged() or use only for debugging purposes.
94 */
95 select<T = S>(selectorFn?: (state: S) => T): Observable<T>;
96 /**
97 * Destroys the Store/Slice. All Observables obtained via .select() will complete when called.
98 */
99 destroy(): void;
100 /**
101 * Manually dispatch an action by its actionName and actionPayload.
102 *
103 * This function exists for compatibility reasons, development and devtools. It is not adviced to use
104 * this function extensively.
105 *
106 * Note: While the observable-based actions
107 * dispatches only reducers registered for that slice, the string based action dispatch here will forward the
108 * action to ALL stores, (sub-)slice and parent alike so make sure you separate your actions based on the strings.
109 */
110 dispatch<P>(actionName: string, actionPayload: P): void;
111}