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 | 9x 8x 9x 9x 9x 23x 23x 19x 19x 17x 17x 17x 21x 17x 17x 10x 10x | import { Reducer, ActionLike } from './types';
import { Epic } from './Epic';
import { Notify } from './Notify';
type Listener = () => void;
export class Store<TState = any> {
public isEnabled: boolean = false;
public state: TState | undefined = undefined;
public reducer: Reducer<TState> | null = null;
public epic: Epic | null = null;
private listeners: Listener[] = [];
constructor(public name: symbol, public displayName: string) {}
initState() {
if (this.reducer) {
this.state = this.reducer(undefined, {
type: [Symbol('__INIT__'), 'init'],
});
}
}
enable({ epic, reducer }: { epic?: Epic; reducer?: Reducer<TState> }) {
this.epic = epic || null;
this.reducer = reducer || null;
this.isEnabled = true;
}
disable() {
this.epic = null;
this.reducer = null;
this.isEnabled = false;
}
dispatch(action: ActionLike, notify?: Notify) {
Iif (!this.isEnabled) {
return;
}
if (this.reducer) {
const nextState = this.reducer(this.state, action);
if (nextState !== this.state) {
this.state = nextState;
const notifyFn = () => {
for (const listener of this.listeners) {
listener();
}
};
Eif (notify) {
notify.add(notifyFn);
} else {
notifyFn();
}
}
}
}
subscribe(listener: Listener) {
this.listeners.push(listener);
return () => {
this.listeners.splice(this.listeners.indexOf(listener), 1);
};
}
}
|