/
createSerializeEnhancer.js
173 lines (148 loc) 路 4.33 KB
/
createSerializeEnhancer.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// @ts-check
/** @typedef {import('redux').Reducer} Reducer */
/** @typedef {import('redux').AnyAction} AnyAction */
/** @typedef {import('redux').Action} Action */
/** @typedef {import('./createSerializeReducer').CuserStoreSerializeReducerOptions} CuserStoreSerializeReducerOptions */
const createSerializeReducer = require('./createSerializeReducer');
const debug = require('debug')('cuser:store:serializer');
const {
TYPE_ACTION_REHYDRATE,
TYPE_ACTION_SEAL
} = require('../types/actions');
const phases = {
IDLE: 0,
SEALING: 1,
REHYDRATING: 2
}
/**
* @callback CuserSerializeCheck
* @param {any} state
* @returns {Boolean}
*/
/**
* @typedef {object} CuserSerializeOptions
* @prop {CuserSerializeCheck} [serializable]
* @prop {(state: object) => Promise<string>} [serialize]
*/
/**
* @typedef {object} CuserDeserializeOptions
* @prop {CuserSerializeCheck} [deserializable]
* @prop {(state: string) => Promise<any>} [deserialize]
*/
/**
* @callback CuserSerializeProcessMap
* @param {string} pointer
* @param {object} action
* @returns {string}
*/
/**
* @callback CuserSerializeReducer
* @param {any} state
* @param {AnyAction} action
* @param {object} options
*/
/**
* @typedef {Array<string | any>} CuserSerializeMappingEntry
*/
/**
* @typedef {string|CuserSerializeMappingEntry} CuserSerializeMappingAlias
*/
/**
* @typedef {Record<string, CuserSerializeMappingAlias>} CuserSerializeMapping
*/
/**
* @typedef {Record<string, CuserSerializeReducer>} CuserSerializeAliases
*/
/**
* @typedef {CuserStoreSerializeReducerOptions} CuserSerializeEnhancerOptions
*/
/**
* @param {CuserDeserializeOptions} opts
*/
const createRehydrateReducer = ({ deserialize, deserializable = () => true }) => {
/**
* @param {any} state
* @param {AnyAction} action
*/
const rehydrateReducer = (state, { type }) => {
if (deserialize && type === TYPE_ACTION_REHYDRATE && deserializable(state)) {
return deserialize(state);
}
return state;
}
return rehydrateReducer;
}
/**
* @param {CuserSerializeOptions} opts
*/
const createSealReducer = ({ serialize, serializable = () => true }) => {
/**
* @param {any} state
* @param {AnyAction} action
*/
const sealReducer = (state, { type }) => {
if (serialize && type === TYPE_ACTION_SEAL && serializable(state)) {
return serialize(state);
}
return state;
}
return sealReducer;
}
/**
* @param {Array<String>} patterns
* @param {CuserSerializeEnhancerOptions & CuserSerializeOptions & CuserDeserializeOptions} opts
*/
const createSerializeEnhancer = (patterns, opts) => {
const {
...restOpts
} = {
deserializable: () => true,
serializable: () => true,
serialize: (state) => state,
deserialize: (state) => state,
...opts
};
return createStore => (
rootReducer,
initialState,
enhancer,
) => {
let store;
const sealReducer = createSealReducer(restOpts);
const rehydrateReducer = createRehydrateReducer(restOpts);
const serializeReducer = createSerializeReducer(sealReducer, patterns, {
...restOpts,
promise: true,
});
const deserializeReducer = createSerializeReducer(rehydrateReducer, patterns, {
...restOpts,
deeper: true,
promise: true,
});
const enhancedSerializeReducer = async (state, action) => {
state = await state;
debug(`performing "${TYPE_ACTION_REHYDRATE}" on / %s`, state);
state = await rehydrateReducer(state, { type: TYPE_ACTION_REHYDRATE });
state = await deserializeReducer(state, { type: TYPE_ACTION_REHYDRATE });
state = await rootReducer(state, action);
debug(`performing "${TYPE_ACTION_SEAL}" on / %s`, state);
state = await serializeReducer(state, { type: TYPE_ACTION_SEAL });
state = await sealReducer(state, { type: TYPE_ACTION_SEAL });
return state;
}
store = createStore(enhancedSerializeReducer, initialState, enhancer);
const dispatch = store.dispatch;
/**
* Dispatch an action agains the store and returns a promise
* with serialized state when sealed complete
* @param {AnyAction} action
* @returns {Promise<any>}
*/
const deferredDispatch = async (action) => {
dispatch(action);
return store.getState();
};
return Object.assign(store, { dispatch: deferredDispatch });
}
}
module.exports = createSerializeEnhancer;