/
state-operations.ts
63 lines (55 loc) 路 1.86 KB
/
state-operations.ts
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
import { Injectable } from '@angular/core';
import { StateOperations, StatesAndDefaults } from '../internal/internals';
import { InternalDispatcher } from '../internal/dispatcher';
import { StateStream } from './state-stream';
import { NgxsConfig } from '../symbols';
import { deepFreeze } from '../utils/freeze';
import { ConfigValidator } from '../internal/config-validator';
/**
* State Context factory class
* @ignore
*/
@Injectable()
export class InternalStateOperations {
constructor(
private _stateStream: StateStream,
private _dispatcher: InternalDispatcher,
private _config: NgxsConfig,
configValidator: ConfigValidator
) {
configValidator.verifyDevMode();
}
/**
* Returns the root state operators.
*/
getRootStateOperations(): StateOperations<any> {
const rootStateOperations = {
getState: () => this._stateStream.getValue(),
setState: (newState: any) => this._stateStream.next(newState),
dispatch: (actions: any[]) => this._dispatcher.dispatch(actions)
};
if (this._config.developmentMode) {
return this.ensureStateAndActionsAreImmutable(rootStateOperations);
}
return rootStateOperations;
}
private ensureStateAndActionsAreImmutable(root: StateOperations<any>): StateOperations<any> {
return {
getState: () => root.getState(),
setState: value => {
const frozenValue = deepFreeze(value);
return root.setState(frozenValue);
},
dispatch: actions => {
return root.dispatch(actions);
}
};
}
setStateToTheCurrentWithNew(results: StatesAndDefaults): void {
const stateOperations: StateOperations<any> = this.getRootStateOperations();
// Get our current stream
const currentState = stateOperations.getState();
// Set the state to the current + new
stateOperations.setState({ ...currentState, ...results.defaults });
}
}