Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 34x 34x 45x 45x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 31x 31x 2x 2x 2x 2x 2x 2x 2x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 31x 26x 26x 29x 29x 31x 45x 45x 45x 45x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 6x 6x 9x 45x | import {
Action,
DispatchArgs,
DomainContext,
MutableStore,
OnDispatch,
Reducer,
StoreContext,
Thunk,
} from "../types";
import { withUse } from "../withUse";
import { emitter } from "../emitter";
/**
* Internal implementation of a mutable store.
*
* Stores hold state, accept actions via dispatch, and notify listeners
* when state changes. They also receive domain-level actions.
*
* @internal
*/
export class StoreImpl<
TState,
TAction extends Action,
TDomainAction extends Action
> implements MutableStore<TState, TAction, TDomainAction>
{
name: string;
private state: TState;
private reducer: Reducer<TState, TAction>;
private changeEmitter = emitter<void>();
private dispatchEmitter =
emitter<
DispatchArgs<
TAction | TDomainAction,
StoreContext<TState, TAction, TDomainAction>
>
>();
private domainContext: DomainContext<TDomainAction>;
constructor(
name: string,
initial: TState,
reducer: Reducer<TState, TAction>,
domainContext: DomainContext<TDomainAction>,
private notifyParent?: OnDispatch
) {
this.name = name;
this.state = initial;
this.reducer = reducer;
this.domainContext = domainContext;
withUse(this);
}
use: any; // Handled by createPipeable
getState = () => this.state;
onChange = (listener: () => void) => {
return this.changeEmitter.on(listener);
};
onDispatch = (
listener: (
args: DispatchArgs<
TAction | TDomainAction,
StoreContext<TState, TAction, TDomainAction>
>
) => void
) => {
return this.dispatchEmitter.on(listener);
};
dispatch = (actionOrThunk: TAction | Thunk<any>) => {
// Thunk Handling
if (typeof actionOrThunk === "function") {
const context: StoreContext<TState, TAction, TDomainAction> = {
dispatch: this.dispatch as any,
domain: this.domainContext,
getState: this.getState,
};
return actionOrThunk(context);
}
const action = actionOrThunk as TAction;
// Notify Action Listeners (Before Reduce)
const context: StoreContext<TState, TAction, TDomainAction> = {
dispatch: this.dispatch as any,
domain: this.domainContext,
getState: this.getState,
};
this.dispatchEmitter.emit({ action, source: this.name, context });
// Reduce
const oldState = this.state;
this.state = this.reducer(this.state, action);
// Notify State Listeners
if (this.state !== oldState) {
this.changeEmitter.emit();
}
this.notifyParent?.({ action, source: this.name, context });
};
// Internal: Called by Domain to inject global actions
// @internal
_receiveDomainAction(action: TDomainAction) {
// BroadCast to this store
// 1. Notify listeners
const context: StoreContext<TState, TAction, TDomainAction> = {
dispatch: this.dispatch as any,
domain: this.domainContext,
getState: this.getState,
};
this.dispatchEmitter.emit({ action, source: this.name, context });
// 2. Reduce (State might respond to Global Action)
const oldState = this.state;
// @ts-ignore - We allow reducer to handle TAction | TDomainAction
this.state = this.reducer(this.state, action as any);
if (this.state !== oldState) {
this.changeEmitter.emit();
}
}
}
|