1 | import {OpaqueToken} from '@angular/core';
|
2 |
|
3 | export const HMR_STATE = new OpaqueToken('hmrState');
|
4 |
|
5 | export class HmrStore {
|
6 | static dev = false;
|
7 | static _state = {};
|
8 | static _initialValues = {};
|
9 | static _states = [];
|
10 |
|
11 | static set(prop, value) {
|
12 | HmrStore._state[prop] = value;
|
13 | return HmrStore._state[prop];
|
14 | }
|
15 |
|
16 | static get(prop) {
|
17 | return HmrStore._state[prop];
|
18 | }
|
19 |
|
20 | static select(name, getState) {
|
21 | HmrStore._states.push({ name, getState });
|
22 | let defaultData = getState();
|
23 | let currentData = HmrStore.get(name);
|
24 |
|
25 | if (defaultData && !currentData) {
|
26 | return HmrStore.set(name, defaultData);
|
27 | } else if (defaultData && currentData) {
|
28 | return HmrStore.set(name, (<any>Object).assign({}, defaultData, currentData));
|
29 | } else {
|
30 | return HmrStore.set(name, currentData || defaultData);
|
31 | }
|
32 | }
|
33 |
|
34 | static dispose() {
|
35 | HmrStore._states = [];
|
36 | HmrStore._state = {};
|
37 | HmrStore._initialValues = {};
|
38 | }
|
39 |
|
40 | static getState() {
|
41 | let initialState = (<any>Object).assign({}, HmrStore._state);
|
42 | return HmrStore._states
|
43 | .reduce((memo, item) => {
|
44 | memo[item.name] = item.getState();
|
45 | return memo;
|
46 | }, initialState);
|
47 | }
|
48 | static toJSON() {
|
49 | return HmrStore.getState();
|
50 | }
|
51 | }
|