@o3r/store-sync 15.3.0-prerelease.16 → 15.3.0-prerelease.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fesm2022/o3r-store-sync.mjs +206 -435
- package/fesm2022/o3r-store-sync.mjs.map +1 -1
- package/package.json +6 -6
- package/types/o3r-store-sync.d.ts +94 -103
- package/types/o3r-store-sync.d.ts.map +1 -1
|
@@ -1,458 +1,229 @@
|
|
|
1
|
-
import { INIT, UPDATE, createAction, props } from
|
|
2
|
-
import equal from
|
|
3
|
-
import { utils } from
|
|
4
|
-
import { deepFill } from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
condition: (data) => data instanceof utils.Date,
|
|
13
|
-
construct: (data) => new utils.Date(data)
|
|
14
|
-
}
|
|
15
|
-
];
|
|
16
|
-
/**
|
|
17
|
-
* Deep fill of base object using source
|
|
18
|
-
* It will do a deep merge of the objects, overriding arrays
|
|
19
|
-
* All properties not present in source, but present in base, will remain
|
|
20
|
-
* @param base
|
|
21
|
-
* @param source
|
|
22
|
-
*/
|
|
1
|
+
import { INIT, UPDATE, createAction, props } from "@ngrx/store";
|
|
2
|
+
import equal from "fast-deep-equal";
|
|
3
|
+
import { utils } from "@ama-sdk/core";
|
|
4
|
+
import { deepFill } from "@o3r/core";
|
|
5
|
+
const mapper = [{
|
|
6
|
+
condition: (data) => data instanceof utils.DateTime,
|
|
7
|
+
construct: (data) => new utils.DateTime(data)
|
|
8
|
+
}, {
|
|
9
|
+
condition: (data) => data instanceof utils.Date,
|
|
10
|
+
construct: (data) => new utils.Date(data)
|
|
11
|
+
}];
|
|
23
12
|
function deepFillWithDate(base, source) {
|
|
24
|
-
|
|
13
|
+
return deepFill(base, source, mapper);
|
|
25
14
|
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise
|
|
29
|
-
* @param _key JSON item key name
|
|
30
|
-
* @param value JSON item value
|
|
31
|
-
*/
|
|
32
15
|
const dateReviver = (_key, value) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return value;
|
|
16
|
+
if (typeof value === "string" && /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) return new Date(value);
|
|
17
|
+
return value;
|
|
37
18
|
};
|
|
38
|
-
/**
|
|
39
|
-
* Dummy reviver returning the value of the field as it is
|
|
40
|
-
* @param _key JSON item key name
|
|
41
|
-
* @param value JSON item value
|
|
42
|
-
*/
|
|
43
19
|
const dummyReviver = (_key, value) => value;
|
|
44
|
-
/**
|
|
45
|
-
* Determine if the code is run in a browser
|
|
46
|
-
*/
|
|
47
20
|
const checkIsBrowserEnv = () => {
|
|
48
|
-
|
|
21
|
+
return typeof window !== "undefined";
|
|
49
22
|
};
|
|
50
|
-
/**
|
|
51
|
-
* Validate the keys structure of the registered stores
|
|
52
|
-
* @param keys map of store to synchronize
|
|
53
|
-
*/
|
|
54
23
|
const validateStateKeys = (keys) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
throw new TypeError('localStorageSync Unknown Parameter Type: ' + `Expected type of string, got ${typeof attr}`);
|
|
62
|
-
}
|
|
63
|
-
return key;
|
|
64
|
-
});
|
|
24
|
+
return keys.map((key) => {
|
|
25
|
+
let attr = key;
|
|
26
|
+
if (typeof key === "object") attr = Object.keys(key)[0];
|
|
27
|
+
if (typeof attr !== "string") throw new TypeError(`localStorageSync Unknown Parameter Type: Expected type of string, got ${typeof attr}`);
|
|
28
|
+
return key;
|
|
29
|
+
});
|
|
65
30
|
};
|
|
66
|
-
/**
|
|
67
|
-
* Rehydrate the state of the whole application store
|
|
68
|
-
* @param keys Keys of the store to rehydrate
|
|
69
|
-
* @param storage storage when getting the data from
|
|
70
|
-
* @param storageKeySerializer storage key transform function
|
|
71
|
-
* @param restoreDates determine if the date need to be restored
|
|
72
|
-
* @param logger
|
|
73
|
-
*/
|
|
74
31
|
const rehydrateApplicationState = (keys, storage, storageKeySerializer, restoreDates, logger = console) => {
|
|
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
|
-
else if (curr[key].encrypt || curr[key].decrypt) {
|
|
108
|
-
// Let know that one of the encryption functions is not provided
|
|
109
|
-
logger.error(`Either encrypt or decrypt function is not present on '${curr[key]}' key object.`);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (storage !== undefined) {
|
|
113
|
-
let stateSlice = storage.getItem(storageKeySerializer(key));
|
|
114
|
-
if (stateSlice) {
|
|
115
|
-
// Use provided decrypt function
|
|
116
|
-
if (decrypt) {
|
|
117
|
-
stateSlice = decrypt(stateSlice);
|
|
118
|
-
}
|
|
119
|
-
const isObjectRegex = new RegExp('{|\\[');
|
|
120
|
-
let raw = stateSlice;
|
|
121
|
-
if (stateSlice === 'null' || stateSlice === 'true' || stateSlice === 'false' || isObjectRegex.test(stateSlice.charAt(0))) {
|
|
122
|
-
raw = JSON.parse(stateSlice, reviver);
|
|
123
|
-
}
|
|
124
|
-
const rehydratedState = deserialize ? deserialize(raw) : raw;
|
|
125
|
-
return syncForFeature
|
|
126
|
-
? rehydratedState
|
|
127
|
-
: Object.assign({}, acc, {
|
|
128
|
-
[key]: rehydratedState
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return acc;
|
|
133
|
-
}, {});
|
|
32
|
+
return keys.reduce((acc, curr) => {
|
|
33
|
+
let key = curr;
|
|
34
|
+
let reviver = restoreDates ? dateReviver : dummyReviver;
|
|
35
|
+
let deserialize;
|
|
36
|
+
let decrypt;
|
|
37
|
+
let syncForFeature = false;
|
|
38
|
+
if (typeof key === "object") {
|
|
39
|
+
key = Object.keys(key)[0];
|
|
40
|
+
syncForFeature = !!curr?.[key]?.syncForFeature;
|
|
41
|
+
if (typeof curr[key] === "function") reviver = curr[key];
|
|
42
|
+
else {
|
|
43
|
+
if (curr[key].reviver) reviver = curr[key].reviver;
|
|
44
|
+
if (curr[key].deserialize) deserialize = curr[key].deserialize;
|
|
45
|
+
}
|
|
46
|
+
if (curr[key].encrypt && curr[key].decrypt) {
|
|
47
|
+
if (typeof curr[key].encrypt === "function" && typeof curr[key].decrypt === "function") decrypt = curr[key].decrypt;
|
|
48
|
+
else logger.error(`Either encrypt or decrypt is not a function on '${curr[key]}' key object.`);
|
|
49
|
+
} else if (curr[key].encrypt || curr[key].decrypt) logger.error(`Either encrypt or decrypt function is not present on '${curr[key]}' key object.`);
|
|
50
|
+
}
|
|
51
|
+
if (storage !== void 0) {
|
|
52
|
+
let stateSlice = storage.getItem(storageKeySerializer(key));
|
|
53
|
+
if (stateSlice) {
|
|
54
|
+
if (decrypt) stateSlice = decrypt(stateSlice);
|
|
55
|
+
const isObjectRegex = /* @__PURE__ */ new RegExp("{|\\[");
|
|
56
|
+
let raw = stateSlice;
|
|
57
|
+
if (stateSlice === "null" || stateSlice === "true" || stateSlice === "false" || isObjectRegex.test(stateSlice.charAt(0))) raw = JSON.parse(stateSlice, reviver);
|
|
58
|
+
const rehydratedState = deserialize ? deserialize(raw) : raw;
|
|
59
|
+
return syncForFeature ? rehydratedState : Object.assign({}, acc, { [key]: rehydratedState });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return acc;
|
|
63
|
+
}, {});
|
|
134
64
|
};
|
|
135
|
-
// Recursively traverse all properties of the existing slice as defined by the `filter` argument,
|
|
136
|
-
// and output the new object with extraneous properties removed.
|
|
137
65
|
function createStateSlice(existingSlice, filter) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const element = attr[key];
|
|
149
|
-
memo[key] = createStateSlice(existingSlice[key], element);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return memo;
|
|
154
|
-
}, {});
|
|
66
|
+
return filter.reduce((memo, attr) => {
|
|
67
|
+
if (typeof attr === "string" || typeof attr === "number") {
|
|
68
|
+
const value = existingSlice?.[attr];
|
|
69
|
+
if (value !== void 0) memo[attr] = value;
|
|
70
|
+
} else for (const key in attr) if (Object.prototype.hasOwnProperty.call(attr, key)) {
|
|
71
|
+
const element = attr[key];
|
|
72
|
+
memo[key] = createStateSlice(existingSlice[key], element);
|
|
73
|
+
}
|
|
74
|
+
return memo;
|
|
75
|
+
}, {});
|
|
155
76
|
}
|
|
156
|
-
/**
|
|
157
|
-
* Update the state of the store after reviving from storage
|
|
158
|
-
* Note: this function is mainly use for internal process of store synchronization
|
|
159
|
-
* @param state store state
|
|
160
|
-
* @param keys key of the store
|
|
161
|
-
* @param storage storage to use for the synchronization
|
|
162
|
-
* @param storageKeySerializer callback to serialize the store key
|
|
163
|
-
* @param removeOnUndefined Define if the store should be clean in the storage if undefined
|
|
164
|
-
* @param syncCondition callback to define if the synchronization should be process
|
|
165
|
-
* @param logger Logger to report messages
|
|
166
|
-
*/
|
|
167
77
|
const syncStateUpdate = (state, keys, storage, storageKeySerializer, removeOnUndefined = false, syncCondition, logger = console) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
encrypt = key[name].encrypt;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
else if (key[name].encrypt || key[name].decrypt) {
|
|
216
|
-
// If one of those is not present, then let know that one is missing
|
|
217
|
-
logger.error(`Either encrypt or decrypt function is not present on '${key[name]}' key object.`);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
/*
|
|
221
|
-
Replacer and space arguments to pass to JSON.stringify.
|
|
222
|
-
If these fields don't exist, undefined will be passed.
|
|
223
|
-
*/
|
|
224
|
-
replacer = key[name].replacer;
|
|
225
|
-
space = key[name].space;
|
|
226
|
-
}
|
|
227
|
-
key = name;
|
|
228
|
-
}
|
|
229
|
-
else if (typeof key === 'string') {
|
|
230
|
-
stateSlice = syncForFeature ? state : state?.[key];
|
|
231
|
-
}
|
|
232
|
-
if (typeof stateSlice !== 'undefined' && storage !== undefined) {
|
|
233
|
-
try {
|
|
234
|
-
if (encrypt) {
|
|
235
|
-
// ensure that a string message is passed
|
|
236
|
-
stateSlice = encrypt(typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space));
|
|
237
|
-
}
|
|
238
|
-
storage.setItem(storageKeySerializer(key), typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space));
|
|
239
|
-
}
|
|
240
|
-
catch (e) {
|
|
241
|
-
logger.warn('Unable to save state to localStorage:', e);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
else if (typeof stateSlice === 'undefined' && removeOnUndefined && storage !== undefined) {
|
|
245
|
-
try {
|
|
246
|
-
storage.removeItem(storageKeySerializer(key));
|
|
247
|
-
}
|
|
248
|
-
catch (e) {
|
|
249
|
-
logger.warn(`Exception on removing/cleaning undefined '${key}' state`, e);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
});
|
|
78
|
+
if (syncCondition) try {
|
|
79
|
+
if (syncCondition(state) !== true) return;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
if (e instanceof TypeError) return;
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
keys.forEach((key) => {
|
|
85
|
+
let stateSlice = state?.[key];
|
|
86
|
+
let replacer;
|
|
87
|
+
let space;
|
|
88
|
+
let encrypt;
|
|
89
|
+
let syncForFeature = false;
|
|
90
|
+
if (typeof key === "object") {
|
|
91
|
+
const name = Object.keys(key)[0];
|
|
92
|
+
syncForFeature = !!key?.[name]?.syncForFeature;
|
|
93
|
+
stateSlice = syncForFeature ? state : state?.[name];
|
|
94
|
+
if (typeof stateSlice !== "undefined" && key[name]) {
|
|
95
|
+
if (key[name].serialize) stateSlice = key[name].serialize(stateSlice);
|
|
96
|
+
else {
|
|
97
|
+
let filter;
|
|
98
|
+
if (key[name].reduce) filter = key[name];
|
|
99
|
+
else if (key[name].filter) filter = key[name].filter;
|
|
100
|
+
if (filter) stateSlice = createStateSlice(stateSlice, filter);
|
|
101
|
+
if (key[name].encrypt && key[name].decrypt) {
|
|
102
|
+
if (typeof key[name].encrypt === "function") encrypt = key[name].encrypt;
|
|
103
|
+
} else if (key[name].encrypt || key[name].decrypt) logger.error(`Either encrypt or decrypt function is not present on '${key[name]}' key object.`);
|
|
104
|
+
}
|
|
105
|
+
replacer = key[name].replacer;
|
|
106
|
+
space = key[name].space;
|
|
107
|
+
}
|
|
108
|
+
key = name;
|
|
109
|
+
} else if (typeof key === "string") stateSlice = syncForFeature ? state : state?.[key];
|
|
110
|
+
if (typeof stateSlice !== "undefined" && storage !== void 0) try {
|
|
111
|
+
if (encrypt) stateSlice = encrypt(typeof stateSlice === "string" ? stateSlice : JSON.stringify(stateSlice, replacer, space));
|
|
112
|
+
storage.setItem(storageKeySerializer(key), typeof stateSlice === "string" ? stateSlice : JSON.stringify(stateSlice, replacer, space));
|
|
113
|
+
} catch (e) {
|
|
114
|
+
logger.warn("Unable to save state to localStorage:", e);
|
|
115
|
+
}
|
|
116
|
+
else if (typeof stateSlice === "undefined" && removeOnUndefined && storage !== void 0) try {
|
|
117
|
+
storage.removeItem(storageKeySerializer(key));
|
|
118
|
+
} catch (e) {
|
|
119
|
+
logger.warn(`Exception on removing/cleaning undefined '${key}' state`, e);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
253
122
|
};
|
|
254
123
|
const defaultMergeReducer = (state, rehydratedState, action) => {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
return [key, rehydratedState[key]];
|
|
267
|
-
}
|
|
268
|
-
}));
|
|
269
|
-
}
|
|
270
|
-
return state;
|
|
124
|
+
if ((action.type === INIT || action.type === UPDATE) && rehydratedState) {
|
|
125
|
+
const allKeys = Array.from(/* @__PURE__ */ new Set([...Object.keys(state), ...Object.keys(rehydratedState)]));
|
|
126
|
+
state = Object.fromEntries(allKeys.map((key) => {
|
|
127
|
+
if (!rehydratedState[key]) return [key, state[key]];
|
|
128
|
+
else if (state[key]) return [key, deepFill(state[key], rehydratedState[key])];
|
|
129
|
+
else return [key, rehydratedState[key]];
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
return state;
|
|
271
133
|
};
|
|
272
|
-
/**
|
|
273
|
-
* Local and Session storage synchronization helper
|
|
274
|
-
* @param config
|
|
275
|
-
*/
|
|
276
134
|
const syncStorage = (config) => (reducer) => {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
return (state, action) => {
|
|
298
|
-
let nextState;
|
|
299
|
-
// If state arrives undefined, we need to let it through the supplied reducer
|
|
300
|
-
// in order to get a complete state as defined by user
|
|
301
|
-
nextState = action.type === INIT && !state ? reducer(state, action) : { ...state };
|
|
302
|
-
// Merge the store state with the rehydrated state using
|
|
303
|
-
// either a user-defined reducer or the default.
|
|
304
|
-
nextState = mergeReducer(nextState, rehydratedState, action);
|
|
305
|
-
nextState = reducer(nextState, action);
|
|
306
|
-
if (action.type !== INIT) {
|
|
307
|
-
syncStateUpdate(nextState, (typeof config.syncKeyCondition === 'function')
|
|
308
|
-
? stateKeys.filter((key) => {
|
|
309
|
-
const keyName = typeof key === 'object' ? Object.keys(key)[0] : key;
|
|
310
|
-
return config.syncKeyCondition(keyName, nextState);
|
|
311
|
-
})
|
|
312
|
-
: stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined, config.syncCondition, logger);
|
|
313
|
-
}
|
|
314
|
-
if (config.postProcess) {
|
|
315
|
-
config.postProcess(nextState);
|
|
316
|
-
}
|
|
317
|
-
return nextState;
|
|
318
|
-
};
|
|
135
|
+
if (config.storage === void 0 && !config.checkStorageAvailability || config.checkStorageAvailability && checkIsBrowserEnv()) config.storage = localStorage || window.localStorage;
|
|
136
|
+
if (config.storageKeySerializer === void 0) config.storageKeySerializer = (key) => key;
|
|
137
|
+
if (config.restoreDates === void 0) config.restoreDates = true;
|
|
138
|
+
let mergeReducer = config.mergeReducer;
|
|
139
|
+
if (mergeReducer === void 0 || typeof mergeReducer !== "function") mergeReducer = defaultMergeReducer;
|
|
140
|
+
const logger = config.logger || console;
|
|
141
|
+
const stateKeys = validateStateKeys(config.keys);
|
|
142
|
+
const rehydratedState = config.rehydrate ? rehydrateApplicationState(stateKeys, config.storage, config.storageKeySerializer, config.restoreDates, logger) : void 0;
|
|
143
|
+
return (state, action) => {
|
|
144
|
+
let nextState;
|
|
145
|
+
nextState = action.type === INIT && !state ? reducer(state, action) : { ...state };
|
|
146
|
+
nextState = mergeReducer(nextState, rehydratedState, action);
|
|
147
|
+
nextState = reducer(nextState, action);
|
|
148
|
+
if (action.type !== INIT) syncStateUpdate(nextState, typeof config.syncKeyCondition === "function" ? stateKeys.filter((key) => {
|
|
149
|
+
const keyName = typeof key === "object" ? Object.keys(key)[0] : key;
|
|
150
|
+
return config.syncKeyCondition(keyName, nextState);
|
|
151
|
+
}) : stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined, config.syncCondition, logger);
|
|
152
|
+
if (config.postProcess) config.postProcess(nextState);
|
|
153
|
+
return nextState;
|
|
154
|
+
};
|
|
319
155
|
};
|
|
320
|
-
|
|
321
|
-
/*
|
|
322
|
-
* This file is a fork of the package ngrx-store-localstorage (https://github.com/btroncone/ngrx-store-localstorage) - MIT
|
|
323
|
-
* The following update as been added:
|
|
324
|
-
* - properly export dateReviver function
|
|
325
|
-
* - add missing comments
|
|
326
|
-
* - explicit dependency to @ngrx/store for action names
|
|
327
|
-
* - change interface names to prefix with "sync"
|
|
328
|
-
* - add logger support
|
|
329
|
-
* - fix lint according to eslint rules
|
|
330
|
-
* - split code in several files
|
|
331
|
-
* - migrate tests to Jest
|
|
332
|
-
*/
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Defines if an object is a Serializer
|
|
336
|
-
* @param obj Object to test
|
|
337
|
-
* @returns True if the object is a Serializer
|
|
338
|
-
*/
|
|
339
156
|
const isSerializer = (obj) => !!(obj.serialize || obj.deserialize || obj.reviver || obj.replacer || obj.initialState);
|
|
340
|
-
|
|
341
|
-
* Label of the action to rehydrate the store
|
|
342
|
-
*/
|
|
343
|
-
const REHYDRATE_ACTION_LABEL = '[storeSync] rehydrate';
|
|
344
|
-
/**
|
|
345
|
-
* Action to rehydrate the store
|
|
346
|
-
*/
|
|
157
|
+
const REHYDRATE_ACTION_LABEL = "[storeSync] rehydrate";
|
|
347
158
|
const rehydrateAction = createAction(REHYDRATE_ACTION_LABEL, props());
|
|
348
|
-
/**
|
|
349
|
-
* Defines if the storage is a non-async storage
|
|
350
|
-
* @param options The storage config
|
|
351
|
-
* @returns in case the storage used is a non-async storage
|
|
352
|
-
*/
|
|
353
159
|
const isLocalStorageConfig = (options) => !options.storage || options.storage instanceof Storage;
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
return acc;
|
|
423
|
-
}, {});
|
|
424
|
-
}
|
|
425
|
-
if (Object.keys(overrides).length > 0) {
|
|
426
|
-
hydratedState = { ...state, ...overrides };
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
return base(reducer)(hydratedState, action);
|
|
430
|
-
};
|
|
431
|
-
};
|
|
432
|
-
};
|
|
433
|
-
this.options = {
|
|
434
|
-
keys: [],
|
|
435
|
-
...(extraOptions?.disableSmartSync
|
|
436
|
-
? {}
|
|
437
|
-
: {
|
|
438
|
-
syncKeyCondition: (key, state) => !equal(this.storeImage[key], state[key]),
|
|
439
|
-
postProcess: (state) => {
|
|
440
|
-
this.options.keys.forEach((key) => {
|
|
441
|
-
const keyName = typeof key === 'object' ? Object.keys(key)[0] : key;
|
|
442
|
-
this.storeImage[keyName] = state[keyName];
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
}),
|
|
446
|
-
...options,
|
|
447
|
-
storage: options?.storage,
|
|
448
|
-
mergeReducer: typeof options?.mergeReducer === 'function' ? options.mergeReducer : this.mergeReducer
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* Generated bundle index. Do not edit.
|
|
455
|
-
*/
|
|
456
|
-
|
|
160
|
+
var StorageSync = class {
|
|
161
|
+
constructor(options, extraOptions) {
|
|
162
|
+
this.alreadyHydratedStoreSlices = /* @__PURE__ */ new Set();
|
|
163
|
+
this.hasHydrated = false;
|
|
164
|
+
this.storeImage = {};
|
|
165
|
+
this.mergeReducer = (state, rehydratedState, action) => {
|
|
166
|
+
if (rehydratedState) {
|
|
167
|
+
if (action.type === INIT) state = deepFillWithDate(state, rehydratedState);
|
|
168
|
+
if (action.type === UPDATE && Array.isArray(action.features)) action.features.filter((featName) => !this.alreadyHydratedStoreSlices.has(featName)).filter((featName) => !!rehydratedState[featName]).forEach((fName) => {
|
|
169
|
+
state[fName] = state[fName] ? deepFillWithDate(state[fName], rehydratedState[fName]) : rehydratedState[fName];
|
|
170
|
+
this.alreadyHydratedStoreSlices.add(fName);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return state;
|
|
174
|
+
};
|
|
175
|
+
this.localStorageSync = () => {
|
|
176
|
+
const base = (reducer) => syncStorage({
|
|
177
|
+
...this.options,
|
|
178
|
+
rehydrate: false,
|
|
179
|
+
storage: this.options.storage,
|
|
180
|
+
syncCondition: () => this.hasHydrated
|
|
181
|
+
})(reducer);
|
|
182
|
+
return (reducer) => {
|
|
183
|
+
if (isLocalStorageConfig(this.options)) return syncStorage(this.options)(reducer);
|
|
184
|
+
return (state, action) => {
|
|
185
|
+
let hydratedState = state;
|
|
186
|
+
if (action.type === rehydrateAction.type) {
|
|
187
|
+
const hasAlreadyHydrated = this.hasHydrated;
|
|
188
|
+
this.hasHydrated = true;
|
|
189
|
+
const initialStates = this.options.keys.reduce((acc, key) => {
|
|
190
|
+
const storeName = Object.keys(key)[0];
|
|
191
|
+
const storeSynchronizer = key[storeName];
|
|
192
|
+
if (isSerializer(storeSynchronizer)) acc[storeName] = storeSynchronizer.initialState;
|
|
193
|
+
return acc;
|
|
194
|
+
}, {});
|
|
195
|
+
let overrides = {};
|
|
196
|
+
if (!hasAlreadyHydrated) overrides = Object.entries(action.payload).reduce((acc, [storeName, value]) => {
|
|
197
|
+
if (!initialStates[storeName] || initialStates[storeName] === state[storeName]) acc[storeName] = value;
|
|
198
|
+
else if (initialStates[storeName] !== state[storeName]) (this.options.logger || console).error(`Unable to rehydrate store "${storeName}", it has been modified before being hydrated`);
|
|
199
|
+
return acc;
|
|
200
|
+
}, {});
|
|
201
|
+
if (Object.keys(overrides).length > 0) hydratedState = {
|
|
202
|
+
...state,
|
|
203
|
+
...overrides
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
return base(reducer)(hydratedState, action);
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
this.options = {
|
|
211
|
+
keys: [],
|
|
212
|
+
...extraOptions?.disableSmartSync ? {} : {
|
|
213
|
+
syncKeyCondition: (key, state) => !equal(this.storeImage[key], state[key]),
|
|
214
|
+
postProcess: (state) => {
|
|
215
|
+
this.options.keys.forEach((key) => {
|
|
216
|
+
const keyName = typeof key === "object" ? Object.keys(key)[0] : key;
|
|
217
|
+
this.storeImage[keyName] = state[keyName];
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
...options,
|
|
222
|
+
storage: options?.storage,
|
|
223
|
+
mergeReducer: typeof options?.mergeReducer === "function" ? options.mergeReducer : this.mergeReducer
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
};
|
|
457
227
|
export { REHYDRATE_ACTION_LABEL, StorageSync, dateReviver, isLocalStorageConfig, isSerializer, rehydrateAction, rehydrateApplicationState, syncStateUpdate, syncStorage };
|
|
458
|
-
|
|
228
|
+
|
|
229
|
+
//# sourceMappingURL=o3r-store-sync.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o3r-store-sync.mjs","sources":["../../src/deep-fill/deep-fill.ts","../../src/sync-storage/storage-sync.ts","../../src/sync-storage/index.ts","../../src/core/storage-sync-helpers.ts","../../src/core/storage-sync.ts","../../src/o3r-store-sync.ts"],"sourcesContent":["import {\n utils,\n} from '@ama-sdk/core';\nimport {\n deepFill,\n type PrimitiveReviverMapper,\n} from '@o3r/core';\n\nconst mapper: PrimitiveReviverMapper[] = [\n {\n condition: (data) => data instanceof utils.DateTime,\n construct: (data) => new utils.DateTime(data)\n },\n {\n condition: (data) => data instanceof utils.Date,\n construct: (data) => new utils.Date(data)\n }\n];\n\n/**\n * Deep fill of base object using source\n * It will do a deep merge of the objects, overriding arrays\n * All properties not present in source, but present in base, will remain\n * @param base\n * @param source\n */\nexport function deepFillWithDate<T extends { [x: string]: any }>(base: T, source?: { [x: string]: any }): T {\n return deepFill(base, source, mapper);\n}\n","import {\n INIT,\n UPDATE,\n} from '@ngrx/store';\nimport {\n deepFill,\n} from '@o3r/core';\nimport type {\n Logger,\n} from '@o3r/core';\nimport type {\n StorageKeyConfiguration,\n StorageKeys,\n SyncStorageConfig,\n SyncStorageSyncOptions,\n} from './interfaces';\n\n/**\n * Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise\n * @param _key JSON item key name\n * @param value JSON item value\n */\nexport const dateReviver = (_key: string, value: any) => {\n if (typeof value === 'string' && /\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z/.test(value)) {\n return new Date(value);\n }\n return value;\n};\n\n/**\n * Dummy reviver returning the value of the field as it is\n * @param _key JSON item key name\n * @param value JSON item value\n */\nconst dummyReviver = (_key: string, value: any) => value;\n\n/**\n * Determine if the code is run in a browser\n */\nconst checkIsBrowserEnv = () => {\n return typeof window !== 'undefined';\n};\n\n/**\n * Validate the keys structure of the registered stores\n * @param keys map of store to synchronize\n */\nconst validateStateKeys = (keys: StorageKeys) => {\n return (keys as any[]).map((key) => {\n let attr = key;\n\n if (typeof key === 'object') {\n attr = Object.keys(key)[0];\n }\n\n if (typeof attr !== 'string') {\n throw new TypeError(\n 'localStorageSync Unknown Parameter Type: ' + `Expected type of string, got ${typeof attr}`\n );\n }\n return key;\n });\n};\n\n/**\n * Rehydrate the state of the whole application store\n * @param keys Keys of the store to rehydrate\n * @param storage storage when getting the data from\n * @param storageKeySerializer storage key transform function\n * @param restoreDates determine if the date need to be restored\n * @param logger\n */\nexport const rehydrateApplicationState = (\n keys: StorageKeys,\n storage: Storage | undefined,\n storageKeySerializer: (key: string) => string,\n restoreDates: boolean,\n logger: Logger = console\n) => {\n return (keys as any[]).reduce((acc, curr) => {\n let key = curr;\n let reviver = restoreDates ? dateReviver : dummyReviver;\n let deserialize: ((arg0: string) => any) | undefined;\n let decrypt: ((arg0: string) => string) | undefined;\n let syncForFeature = false;\n\n if (typeof key === 'object') {\n key = Object.keys(key)[0];\n syncForFeature = !!(curr?.[key] as SyncStorageSyncOptions)?.syncForFeature;\n\n // use the custom reviver function\n if (typeof curr[key] === 'function') {\n reviver = curr[key];\n } else {\n // use custom reviver function if available\n if (curr[key].reviver) {\n reviver = curr[key].reviver;\n }\n // use custom serialize function if available\n if (curr[key].deserialize) {\n deserialize = curr[key].deserialize;\n }\n }\n\n // Ensure that encrypt and decrypt functions are both present\n if (curr[key].encrypt && curr[key].decrypt) {\n if (typeof curr[key].encrypt === 'function' && typeof curr[key].decrypt === 'function') {\n decrypt = curr[key].decrypt;\n } else {\n logger.error(`Either encrypt or decrypt is not a function on '${curr[key] as string}' key object.`);\n }\n } else if (curr[key].encrypt || curr[key].decrypt) {\n // Let know that one of the encryption functions is not provided\n logger.error(`Either encrypt or decrypt function is not present on '${curr[key] as string}' key object.`);\n }\n }\n if (storage !== undefined) {\n let stateSlice = storage.getItem(storageKeySerializer(key));\n if (stateSlice) {\n // Use provided decrypt function\n if (decrypt) {\n stateSlice = decrypt(stateSlice);\n }\n\n const isObjectRegex = new RegExp('{|\\\\[');\n let raw = stateSlice;\n\n if (stateSlice === 'null' || stateSlice === 'true' || stateSlice === 'false' || isObjectRegex.test(stateSlice.charAt(0))) {\n raw = JSON.parse(stateSlice, reviver);\n }\n\n const rehydratedState = deserialize ? deserialize(raw) : raw;\n\n return syncForFeature\n ? rehydratedState\n : Object.assign({}, acc, {\n [key]: rehydratedState\n });\n }\n }\n return acc;\n }, {});\n};\n\n// Recursively traverse all properties of the existing slice as defined by the `filter` argument,\n// and output the new object with extraneous properties removed.\nfunction createStateSlice(existingSlice: any, filter: (string | number | StorageKeyConfiguration | SyncStorageSyncOptions)[]) {\n return filter.reduce(\n (memo: { [x: string]: any; [x: number]: any }, attr: string | number | StorageKeyConfiguration | SyncStorageSyncOptions) => {\n if (typeof attr === 'string' || typeof attr === 'number') {\n const value = existingSlice?.[attr];\n if (value !== undefined) {\n memo[attr] = value;\n }\n } else {\n for (const key in attr) {\n if (Object.prototype.hasOwnProperty.call(attr, key)) {\n const element: any = attr[key as keyof typeof attr];\n memo[key] = createStateSlice(existingSlice[key], element);\n }\n }\n }\n return memo;\n },\n {}\n );\n}\n\n/**\n * Update the state of the store after reviving from storage\n * Note: this function is mainly use for internal process of store synchronization\n * @param state store state\n * @param keys key of the store\n * @param storage storage to use for the synchronization\n * @param storageKeySerializer callback to serialize the store key\n * @param removeOnUndefined Define if the store should be clean in the storage if undefined\n * @param syncCondition callback to define if the synchronization should be process\n * @param logger Logger to report messages\n */\nexport const syncStateUpdate = (\n state: any,\n keys: StorageKeys,\n storage: Storage | undefined,\n storageKeySerializer: (key: string | number) => string,\n removeOnUndefined = false,\n syncCondition?: (state: any) => any,\n logger: Logger = console\n) => {\n if (syncCondition) {\n try {\n if (syncCondition(state) !== true) {\n return;\n }\n } catch (e) {\n // Treat TypeError as do not sync\n if (e instanceof TypeError) {\n return;\n }\n throw e;\n }\n }\n\n keys.forEach((key: string | StorageKeyConfiguration | SyncStorageSyncOptions | ((key: string, value: any) => any)): void => {\n let stateSlice = state?.[key as string];\n let replacer;\n let space: string | number | undefined;\n let encrypt;\n let syncForFeature = false;\n\n if (typeof key === 'object') {\n const name = (Object.keys(key) as (keyof typeof key)[])[0];\n syncForFeature = !!(key?.[name] as SyncStorageSyncOptions)?.syncForFeature;\n stateSlice = syncForFeature ? state : state?.[name];\n\n if (typeof stateSlice !== 'undefined' && key[name]) {\n // use serialize function if specified.\n if ((key[name] as any).serialize) {\n stateSlice = (key[name] as SyncStorageSyncOptions).serialize!(stateSlice);\n } else {\n // if serialize function is not specified filter on fields if an array has been provided.\n let filter: StorageKeyConfiguration[] | undefined;\n if ((key[name] as any).reduce) {\n filter = key[name] as StorageKeyConfiguration[];\n } else if ((key[name] as any).filter) {\n filter = (key[name] as any).filter as StorageKeyConfiguration[];\n }\n if (filter) {\n stateSlice = createStateSlice(stateSlice, filter);\n }\n\n // Check if encrypt and decrypt are present, also checked at this#rehydrateApplicationState()\n if ((key[name] as any).encrypt && (key[name] as any).decrypt) {\n if (typeof (key[name] as any).encrypt === 'function') {\n encrypt = (key[name] as any).encrypt;\n }\n } else if ((key[name] as any).encrypt || (key[name] as any).decrypt) {\n // If one of those is not present, then let know that one is missing\n logger.error(\n `Either encrypt or decrypt function is not present on '${key[name] as string}' key object.`\n );\n }\n }\n\n /*\n Replacer and space arguments to pass to JSON.stringify.\n If these fields don't exist, undefined will be passed.\n */\n replacer = (key[name] as any).replacer;\n space = (key[name] as any).space;\n }\n\n key = name;\n } else if (typeof key === 'string') {\n stateSlice = syncForFeature ? state : state?.[key];\n }\n\n if (typeof stateSlice !== 'undefined' && storage !== undefined) {\n try {\n if (encrypt) {\n // ensure that a string message is passed\n stateSlice = encrypt(\n typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space)\n );\n }\n storage.setItem(\n storageKeySerializer(key as string),\n typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space)\n );\n } catch (e) {\n logger.warn('Unable to save state to localStorage:', e);\n }\n } else if (typeof stateSlice === 'undefined' && removeOnUndefined && storage !== undefined) {\n try {\n storage.removeItem(storageKeySerializer(key as string));\n } catch (e) {\n logger.warn(`Exception on removing/cleaning undefined '${key as string}' state`, e);\n }\n }\n });\n};\n\nconst defaultMergeReducer = (state: any, rehydratedState: any, action: any) => {\n if ((action.type === INIT || action.type === UPDATE) && rehydratedState) {\n const allKeys = Array.from(new Set([...Object.keys(state), ...Object.keys(rehydratedState)]));\n state = Object.fromEntries(allKeys.map((key) => {\n // No need to create new object reference if no merge is actually needed\n if (!rehydratedState[key]) {\n return [key, state[key]];\n } else if (state[key]) {\n return [key, deepFill(state[key], rehydratedState[key])];\n } else {\n return [key, rehydratedState[key]];\n }\n }));\n }\n\n return state;\n};\n\n/**\n * Local and Session storage synchronization helper\n * @param config\n */\nexport const syncStorage = (config: SyncStorageConfig) => (reducer: any) => {\n if (\n (config.storage === undefined && !config.checkStorageAvailability)\n || (config.checkStorageAvailability && checkIsBrowserEnv())\n ) {\n config.storage = localStorage || window.localStorage;\n }\n\n if (config.storageKeySerializer === undefined) {\n config.storageKeySerializer = (key) => key;\n }\n\n if (config.restoreDates === undefined) {\n config.restoreDates = true;\n }\n\n // Use default merge reducer.\n let mergeReducer = config.mergeReducer;\n\n if (mergeReducer === undefined || typeof mergeReducer !== 'function') {\n mergeReducer = defaultMergeReducer;\n }\n\n const logger = config.logger || console;\n\n const stateKeys = validateStateKeys(config.keys);\n const rehydratedState = config.rehydrate\n ? rehydrateApplicationState(stateKeys, config.storage, config.storageKeySerializer, config.restoreDates, logger)\n : undefined;\n\n return (state: any, action: any) => {\n let nextState: any;\n\n // If state arrives undefined, we need to let it through the supplied reducer\n // in order to get a complete state as defined by user\n nextState = action.type === INIT && !state ? reducer(state, action) : { ...state };\n\n // Merge the store state with the rehydrated state using\n // either a user-defined reducer or the default.\n nextState = mergeReducer(nextState, rehydratedState, action);\n\n nextState = reducer(nextState, action);\n\n if (action.type !== INIT) {\n syncStateUpdate(\n nextState,\n (typeof config.syncKeyCondition === 'function')\n ? stateKeys.filter((key) => {\n const keyName = typeof key === 'object' ? Object.keys(key)[0] : key;\n return config.syncKeyCondition!(keyName, nextState);\n })\n : stateKeys,\n config.storage,\n config.storageKeySerializer as (key: string | number) => string,\n config.removeOnUndefined,\n config.syncCondition,\n logger\n );\n }\n if (config.postProcess) {\n config.postProcess(nextState);\n }\n return nextState;\n };\n};\n","/*\n * This file is a fork of the package ngrx-store-localstorage (https://github.com/btroncone/ngrx-store-localstorage) - MIT\n * The following update as been added:\n * - properly export dateReviver function\n * - add missing comments\n * - explicit dependency to @ngrx/store for action names\n * - change interface names to prefix with \"sync\"\n * - add logger support\n * - fix lint according to eslint rules\n * - split code in several files\n * - migrate tests to Jest\n */\n\nexport * from './interfaces';\nexport * from './storage-sync';\n","import {\n createAction,\n props,\n} from '@ngrx/store';\nimport type {\n Serializer,\n} from '@o3r/core';\nimport type {\n SyncStorageConfig,\n} from '../sync-storage';\nimport type {\n StorageSyncOptions,\n} from './interfaces';\n\n/**\n * Defines if an object is a Serializer\n * @param obj Object to test\n * @returns True if the object is a Serializer\n */\nexport const isSerializer = (obj: any): obj is Serializer<any> => !!(obj.serialize || obj.deserialize || obj.reviver || obj.replacer || obj.initialState);\n\n/**\n * Label of the action to rehydrate the store\n */\nexport const REHYDRATE_ACTION_LABEL = '[storeSync] rehydrate';\n\n/**\n * Action to rehydrate the store\n */\nexport const rehydrateAction = createAction(REHYDRATE_ACTION_LABEL, props<{\n payload: Record<string, any>;\n}>());\n\n/**\n * Defines if the storage is a non-async storage\n * @param options The storage config\n * @returns in case the storage used is a non-async storage\n */\nexport const isLocalStorageConfig = (options: StorageSyncOptions): options is SyncStorageConfig => !options.storage || options.storage instanceof Storage;\n","import {\n INIT,\n UPDATE,\n} from '@ngrx/store';\nimport equal from 'fast-deep-equal';\nimport {\n deepFillWithDate,\n} from '../deep-fill/deep-fill';\nimport {\n syncStorage,\n} from '../sync-storage';\nimport type {\n StorageSyncOptions,\n} from './interfaces';\nimport {\n StorageSyncConstructorOptions,\n} from './interfaces';\nimport {\n isLocalStorageConfig,\n isSerializer,\n rehydrateAction,\n} from './storage-sync-helpers';\n\n/**\n * Storage synchronizer\n */\nexport class StorageSync {\n private readonly alreadyHydratedStoreSlices: Set<string> = new Set();\n private hasHydrated = false;\n private readonly storeImage: { [key: string]: any } = {};\n\n public options: StorageSyncOptions;\n\n constructor(options?: StorageSyncConstructorOptions, extraOptions?: { disableSmartSync: boolean }) {\n this.options = {\n keys: [],\n ...(extraOptions?.disableSmartSync\n ? {}\n : {\n syncKeyCondition: (key, state) => !equal(this.storeImage[key], state[key]),\n postProcess: (state) => {\n this.options.keys.forEach((key) => {\n const keyName: string = typeof key === 'object' ? Object.keys(key)[0] : key;\n this.storeImage[keyName] = state[keyName];\n });\n }\n }),\n ...options,\n storage: options?.storage as unknown as Storage,\n mergeReducer: typeof options?.mergeReducer === 'function' ? options.mergeReducer : this.mergeReducer\n };\n }\n\n /**\n * Handle state manipulation in case of store storage sync\n * Merge strategy is a full merge at init; When a new `store slice` is registered, its initial state is merged with its corresponding storage content\n * Pass it as mergeReducer callback when calling localStorageSync function from 'ngrx-store-localstorage', in the app\n * @param state actual state\n * @param rehydratedState state from storage\n * @param action Triggered action\n */\n public readonly mergeReducer = (state: any, rehydratedState: any, action: any) => {\n if (rehydratedState) {\n if (action.type === INIT) {\n state = deepFillWithDate(state, rehydratedState);\n }\n if (action.type === UPDATE && Array.isArray(action.features)) {\n const notHandledFeatures = action.features.filter((featName: string) => !this.alreadyHydratedStoreSlices.has(featName));\n\n notHandledFeatures.filter((featName: string) => !!rehydratedState[featName]).forEach((fName: string) => {\n state[fName] = state[fName] ? deepFillWithDate(state[fName], rehydratedState[fName]) : rehydratedState[fName];\n this.alreadyHydratedStoreSlices.add(fName);\n });\n }\n }\n return state;\n };\n\n /**\n * Returns a meta reducer that handles storage sync\n */\n public localStorageSync = () => {\n const base = (reducer: any) => syncStorage({\n ...this.options,\n rehydrate: false,\n storage: this.options.storage as unknown as Storage,\n syncCondition: () => this.hasHydrated\n })(reducer);\n\n return (reducer: any) => {\n if (isLocalStorageConfig(this.options)) {\n return syncStorage(this.options)(reducer);\n }\n\n return (state: any, action: any) => {\n let hydratedState = state;\n if (action.type === rehydrateAction.type) {\n const hasAlreadyHydrated = this.hasHydrated;\n this.hasHydrated = true;\n const initialStates = this.options.keys.reduce((acc: Record<string, any>, key) => {\n const storeName = Object.keys(key)[0];\n const storeSynchronizer = (key as any)[storeName];\n if (isSerializer(storeSynchronizer)) {\n acc[storeName] = storeSynchronizer.initialState;\n }\n return acc;\n }, {});\n let overrides = {};\n if (!hasAlreadyHydrated) {\n overrides = Object.entries(action.payload).reduce<Record<string, any>>((acc, [storeName, value]) => {\n if (!initialStates[storeName] || initialStates[storeName] === state[storeName]) {\n acc[storeName] = value;\n } else if (initialStates[storeName] !== state[storeName]) {\n (this.options.logger || console).error(`Unable to rehydrate store \"${storeName}\", it has been modified before being hydrated`);\n }\n return acc;\n }, {});\n }\n if (Object.keys(overrides).length > 0) {\n hydratedState = { ...state, ...overrides };\n }\n }\n return base(reducer)(hydratedState, action);\n };\n };\n };\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;AAQA,MAAM,MAAM,GAA6B;AACvC,IAAA;QACE,SAAS,EAAE,CAAC,IAAI,KAAK,IAAI,YAAY,KAAK,CAAC,QAAQ;AACnD,QAAA,SAAS,EAAE,CAAC,IAAI,KAAK,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI;AAC7C,KAAA;AACD,IAAA;QACE,SAAS,EAAE,CAAC,IAAI,KAAK,IAAI,YAAY,KAAK,CAAC,IAAI;AAC/C,QAAA,SAAS,EAAE,CAAC,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI;AACzC;CACF;AAED;;;;;;AAMG;AACG,SAAU,gBAAgB,CAAiC,IAAO,EAAE,MAA6B,EAAA;IACrG,OAAO,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC;AACvC;;ACXA;;;;AAIG;MACU,WAAW,GAAG,CAAC,IAAY,EAAE,KAAU,KAAI;AACtD,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,4CAA4C,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;AACzF,QAAA,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC;IACxB;AACA,IAAA,OAAO,KAAK;AACd;AAEA;;;;AAIG;AACH,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,KAAU,KAAK,KAAK;AAExD;;AAEG;AACH,MAAM,iBAAiB,GAAG,MAAK;AAC7B,IAAA,OAAO,OAAO,MAAM,KAAK,WAAW;AACtC,CAAC;AAED;;;AAGG;AACH,MAAM,iBAAiB,GAAG,CAAC,IAAiB,KAAI;AAC9C,IAAA,OAAQ,IAAc,CAAC,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GAAG,KAAI;4BAChC,MAAM,OAAO,GAAW,OAAO,GAAG,KAAK,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG;4BAC3E,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC;AAC3C,wBAAA,CAAC,CAAC;oBACJ;iBACD,CAAC;AACJ,YAAA,GAAG,OAAO;YACV,OAAO,EAAE,OAAO,EAAE,OAA6B;AAC/C,YAAA,YAAY,EAAE,OAAO,OAAO,EAAE,YAAY,KAAK,UAAU,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;SACzF;IACH;AA2ED;;AC9HD;;AAEG;;"}
|
|
1
|
+
{"version":3,"file":"o3r-store-sync.mjs","names":[],"sources":["../../src/deep-fill/deep-fill.ts","../../src/sync-storage/storage-sync.ts","../../src/core/storage-sync-helpers.ts","../../src/core/storage-sync.ts"],"sourcesContent":["import {\n utils,\n} from '@ama-sdk/core';\nimport {\n deepFill,\n type PrimitiveReviverMapper,\n} from '@o3r/core';\n\nconst mapper: PrimitiveReviverMapper[] = [\n {\n condition: (data) => data instanceof utils.DateTime,\n construct: (data) => new utils.DateTime(data)\n },\n {\n condition: (data) => data instanceof utils.Date,\n construct: (data) => new utils.Date(data)\n }\n];\n\n/**\n * Deep fill of base object using source\n * It will do a deep merge of the objects, overriding arrays\n * All properties not present in source, but present in base, will remain\n * @param base\n * @param source\n */\nexport function deepFillWithDate<T extends { [x: string]: any }>(base: T, source?: { [x: string]: any }): T {\n return deepFill(base, source, mapper);\n}\n","import {\n INIT,\n UPDATE,\n} from '@ngrx/store';\nimport {\n deepFill,\n} from '@o3r/core';\nimport type {\n Logger,\n} from '@o3r/core';\nimport type {\n StorageKeyConfiguration,\n StorageKeys,\n SyncStorageConfig,\n SyncStorageSyncOptions,\n} from './interfaces';\n\n/**\n * Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise\n * @param _key JSON item key name\n * @param value JSON item value\n */\nexport const dateReviver = (_key: string, value: any) => {\n if (typeof value === 'string' && /\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z/.test(value)) {\n return new Date(value);\n }\n return value;\n};\n\n/**\n * Dummy reviver returning the value of the field as it is\n * @param _key JSON item key name\n * @param value JSON item value\n */\nconst dummyReviver = (_key: string, value: any) => value;\n\n/**\n * Determine if the code is run in a browser\n */\nconst checkIsBrowserEnv = () => {\n return typeof window !== 'undefined';\n};\n\n/**\n * Validate the keys structure of the registered stores\n * @param keys map of store to synchronize\n */\nconst validateStateKeys = (keys: StorageKeys) => {\n return (keys as any[]).map((key) => {\n let attr = key;\n\n if (typeof key === 'object') {\n attr = Object.keys(key)[0];\n }\n\n if (typeof attr !== 'string') {\n throw new TypeError(\n 'localStorageSync Unknown Parameter Type: ' + `Expected type of string, got ${typeof attr}`\n );\n }\n return key;\n });\n};\n\n/**\n * Rehydrate the state of the whole application store\n * @param keys Keys of the store to rehydrate\n * @param storage storage when getting the data from\n * @param storageKeySerializer storage key transform function\n * @param restoreDates determine if the date need to be restored\n * @param logger\n */\nexport const rehydrateApplicationState = (\n keys: StorageKeys,\n storage: Storage | undefined,\n storageKeySerializer: (key: string) => string,\n restoreDates: boolean,\n logger: Logger = console\n) => {\n return (keys as any[]).reduce((acc, curr) => {\n let key = curr;\n let reviver = restoreDates ? dateReviver : dummyReviver;\n let deserialize: ((arg0: string) => any) | undefined;\n let decrypt: ((arg0: string) => string) | undefined;\n let syncForFeature = false;\n\n if (typeof key === 'object') {\n key = Object.keys(key)[0];\n syncForFeature = !!(curr?.[key] as SyncStorageSyncOptions)?.syncForFeature;\n\n // use the custom reviver function\n if (typeof curr[key] === 'function') {\n reviver = curr[key];\n } else {\n // use custom reviver function if available\n if (curr[key].reviver) {\n reviver = curr[key].reviver;\n }\n // use custom serialize function if available\n if (curr[key].deserialize) {\n deserialize = curr[key].deserialize;\n }\n }\n\n // Ensure that encrypt and decrypt functions are both present\n if (curr[key].encrypt && curr[key].decrypt) {\n if (typeof curr[key].encrypt === 'function' && typeof curr[key].decrypt === 'function') {\n decrypt = curr[key].decrypt;\n } else {\n logger.error(`Either encrypt or decrypt is not a function on '${curr[key] as string}' key object.`);\n }\n } else if (curr[key].encrypt || curr[key].decrypt) {\n // Let know that one of the encryption functions is not provided\n logger.error(`Either encrypt or decrypt function is not present on '${curr[key] as string}' key object.`);\n }\n }\n if (storage !== undefined) {\n let stateSlice = storage.getItem(storageKeySerializer(key));\n if (stateSlice) {\n // Use provided decrypt function\n if (decrypt) {\n stateSlice = decrypt(stateSlice);\n }\n\n const isObjectRegex = new RegExp('{|\\\\[');\n let raw = stateSlice;\n\n if (stateSlice === 'null' || stateSlice === 'true' || stateSlice === 'false' || isObjectRegex.test(stateSlice.charAt(0))) {\n raw = JSON.parse(stateSlice, reviver);\n }\n\n const rehydratedState = deserialize ? deserialize(raw) : raw;\n\n return syncForFeature\n ? rehydratedState\n : Object.assign({}, acc, {\n [key]: rehydratedState\n });\n }\n }\n return acc;\n }, {});\n};\n\n// Recursively traverse all properties of the existing slice as defined by the `filter` argument,\n// and output the new object with extraneous properties removed.\nfunction createStateSlice(existingSlice: any, filter: (string | number | StorageKeyConfiguration | SyncStorageSyncOptions)[]) {\n return filter.reduce(\n (memo: { [x: string]: any; [x: number]: any }, attr: string | number | StorageKeyConfiguration | SyncStorageSyncOptions) => {\n if (typeof attr === 'string' || typeof attr === 'number') {\n const value = existingSlice?.[attr];\n if (value !== undefined) {\n memo[attr] = value;\n }\n } else {\n for (const key in attr) {\n if (Object.prototype.hasOwnProperty.call(attr, key)) {\n const element: any = attr[key as keyof typeof attr];\n memo[key] = createStateSlice(existingSlice[key], element);\n }\n }\n }\n return memo;\n },\n {}\n );\n}\n\n/**\n * Update the state of the store after reviving from storage\n * Note: this function is mainly use for internal process of store synchronization\n * @param state store state\n * @param keys key of the store\n * @param storage storage to use for the synchronization\n * @param storageKeySerializer callback to serialize the store key\n * @param removeOnUndefined Define if the store should be clean in the storage if undefined\n * @param syncCondition callback to define if the synchronization should be process\n * @param logger Logger to report messages\n */\nexport const syncStateUpdate = (\n state: any,\n keys: StorageKeys,\n storage: Storage | undefined,\n storageKeySerializer: (key: string | number) => string,\n removeOnUndefined = false,\n syncCondition?: (state: any) => any,\n logger: Logger = console\n) => {\n if (syncCondition) {\n try {\n if (syncCondition(state) !== true) {\n return;\n }\n } catch (e) {\n // Treat TypeError as do not sync\n if (e instanceof TypeError) {\n return;\n }\n throw e;\n }\n }\n\n keys.forEach((key: string | StorageKeyConfiguration | SyncStorageSyncOptions | ((key: string, value: any) => any)): void => {\n let stateSlice = state?.[key as string];\n let replacer;\n let space: string | number | undefined;\n let encrypt;\n let syncForFeature = false;\n\n if (typeof key === 'object') {\n const name = (Object.keys(key) as (keyof typeof key)[])[0];\n syncForFeature = !!(key?.[name] as SyncStorageSyncOptions)?.syncForFeature;\n stateSlice = syncForFeature ? state : state?.[name];\n\n if (typeof stateSlice !== 'undefined' && key[name]) {\n // use serialize function if specified.\n if ((key[name] as any).serialize) {\n stateSlice = (key[name] as SyncStorageSyncOptions).serialize!(stateSlice);\n } else {\n // if serialize function is not specified filter on fields if an array has been provided.\n let filter: StorageKeyConfiguration[] | undefined;\n if ((key[name] as any).reduce) {\n filter = key[name] as StorageKeyConfiguration[];\n } else if ((key[name] as any).filter) {\n filter = (key[name] as any).filter as StorageKeyConfiguration[];\n }\n if (filter) {\n stateSlice = createStateSlice(stateSlice, filter);\n }\n\n // Check if encrypt and decrypt are present, also checked at this#rehydrateApplicationState()\n if ((key[name] as any).encrypt && (key[name] as any).decrypt) {\n if (typeof (key[name] as any).encrypt === 'function') {\n encrypt = (key[name] as any).encrypt;\n }\n } else if ((key[name] as any).encrypt || (key[name] as any).decrypt) {\n // If one of those is not present, then let know that one is missing\n logger.error(\n `Either encrypt or decrypt function is not present on '${key[name] as string}' key object.`\n );\n }\n }\n\n /*\n Replacer and space arguments to pass to JSON.stringify.\n If these fields don't exist, undefined will be passed.\n */\n replacer = (key[name] as any).replacer;\n space = (key[name] as any).space;\n }\n\n key = name;\n } else if (typeof key === 'string') {\n stateSlice = syncForFeature ? state : state?.[key];\n }\n\n if (typeof stateSlice !== 'undefined' && storage !== undefined) {\n try {\n if (encrypt) {\n // ensure that a string message is passed\n stateSlice = encrypt(\n typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space)\n );\n }\n storage.setItem(\n storageKeySerializer(key as string),\n typeof stateSlice === 'string' ? stateSlice : JSON.stringify(stateSlice, replacer, space)\n );\n } catch (e) {\n logger.warn('Unable to save state to localStorage:', e);\n }\n } else if (typeof stateSlice === 'undefined' && removeOnUndefined && storage !== undefined) {\n try {\n storage.removeItem(storageKeySerializer(key as string));\n } catch (e) {\n logger.warn(`Exception on removing/cleaning undefined '${key as string}' state`, e);\n }\n }\n });\n};\n\nconst defaultMergeReducer = (state: any, rehydratedState: any, action: any) => {\n if ((action.type === INIT || action.type === UPDATE) && rehydratedState) {\n const allKeys = Array.from(new Set([...Object.keys(state), ...Object.keys(rehydratedState)]));\n state = Object.fromEntries(allKeys.map((key) => {\n // No need to create new object reference if no merge is actually needed\n if (!rehydratedState[key]) {\n return [key, state[key]];\n } else if (state[key]) {\n return [key, deepFill(state[key], rehydratedState[key])];\n } else {\n return [key, rehydratedState[key]];\n }\n }));\n }\n\n return state;\n};\n\n/**\n * Local and Session storage synchronization helper\n * @param config\n */\nexport const syncStorage = (config: SyncStorageConfig) => (reducer: any) => {\n if (\n (config.storage === undefined && !config.checkStorageAvailability)\n || (config.checkStorageAvailability && checkIsBrowserEnv())\n ) {\n config.storage = localStorage || window.localStorage;\n }\n\n if (config.storageKeySerializer === undefined) {\n config.storageKeySerializer = (key) => key;\n }\n\n if (config.restoreDates === undefined) {\n config.restoreDates = true;\n }\n\n // Use default merge reducer.\n let mergeReducer = config.mergeReducer;\n\n if (mergeReducer === undefined || typeof mergeReducer !== 'function') {\n mergeReducer = defaultMergeReducer;\n }\n\n const logger = config.logger || console;\n\n const stateKeys = validateStateKeys(config.keys);\n const rehydratedState = config.rehydrate\n ? rehydrateApplicationState(stateKeys, config.storage, config.storageKeySerializer, config.restoreDates, logger)\n : undefined;\n\n return (state: any, action: any) => {\n let nextState: any;\n\n // If state arrives undefined, we need to let it through the supplied reducer\n // in order to get a complete state as defined by user\n nextState = action.type === INIT && !state ? reducer(state, action) : { ...state };\n\n // Merge the store state with the rehydrated state using\n // either a user-defined reducer or the default.\n nextState = mergeReducer(nextState, rehydratedState, action);\n\n nextState = reducer(nextState, action);\n\n if (action.type !== INIT) {\n syncStateUpdate(\n nextState,\n (typeof config.syncKeyCondition === 'function')\n ? stateKeys.filter((key) => {\n const keyName = typeof key === 'object' ? Object.keys(key)[0] : key;\n return config.syncKeyCondition!(keyName, nextState);\n })\n : stateKeys,\n config.storage,\n config.storageKeySerializer as (key: string | number) => string,\n config.removeOnUndefined,\n config.syncCondition,\n logger\n );\n }\n if (config.postProcess) {\n config.postProcess(nextState);\n }\n return nextState;\n };\n};\n","import {\n createAction,\n props,\n} from '@ngrx/store';\nimport type {\n Serializer,\n} from '@o3r/core';\nimport type {\n SyncStorageConfig,\n} from '../sync-storage';\nimport type {\n StorageSyncOptions,\n} from './interfaces';\n\n/**\n * Defines if an object is a Serializer\n * @param obj Object to test\n * @returns True if the object is a Serializer\n */\nexport const isSerializer = (obj: any): obj is Serializer<any> => !!(obj.serialize || obj.deserialize || obj.reviver || obj.replacer || obj.initialState);\n\n/**\n * Label of the action to rehydrate the store\n */\nexport const REHYDRATE_ACTION_LABEL = '[storeSync] rehydrate';\n\n/**\n * Action to rehydrate the store\n */\nexport const rehydrateAction = createAction(REHYDRATE_ACTION_LABEL, props<{\n payload: Record<string, any>;\n}>());\n\n/**\n * Defines if the storage is a non-async storage\n * @param options The storage config\n * @returns in case the storage used is a non-async storage\n */\nexport const isLocalStorageConfig = (options: StorageSyncOptions): options is SyncStorageConfig => !options.storage || options.storage instanceof Storage;\n","import {\n INIT,\n UPDATE,\n} from '@ngrx/store';\nimport equal from 'fast-deep-equal';\nimport {\n deepFillWithDate,\n} from '../deep-fill/deep-fill';\nimport {\n syncStorage,\n} from '../sync-storage';\nimport type {\n StorageSyncOptions,\n} from './interfaces';\nimport {\n StorageSyncConstructorOptions,\n} from './interfaces';\nimport {\n isLocalStorageConfig,\n isSerializer,\n rehydrateAction,\n} from './storage-sync-helpers';\n\n/**\n * Storage synchronizer\n */\nexport class StorageSync {\n private readonly alreadyHydratedStoreSlices: Set<string> = new Set();\n private hasHydrated = false;\n private readonly storeImage: { [key: string]: any } = {};\n\n public options: StorageSyncOptions;\n\n constructor(options?: StorageSyncConstructorOptions, extraOptions?: { disableSmartSync: boolean }) {\n this.options = {\n keys: [],\n ...(extraOptions?.disableSmartSync\n ? {}\n : {\n syncKeyCondition: (key, state) => !equal(this.storeImage[key], state[key]),\n postProcess: (state) => {\n this.options.keys.forEach((key) => {\n const keyName: string = typeof key === 'object' ? Object.keys(key)[0] : key;\n this.storeImage[keyName] = state[keyName];\n });\n }\n }),\n ...options,\n storage: options?.storage as unknown as Storage,\n mergeReducer: typeof options?.mergeReducer === 'function' ? options.mergeReducer : this.mergeReducer\n };\n }\n\n /**\n * Handle state manipulation in case of store storage sync\n * Merge strategy is a full merge at init; When a new `store slice` is registered, its initial state is merged with its corresponding storage content\n * Pass it as mergeReducer callback when calling localStorageSync function from 'ngrx-store-localstorage', in the app\n * @param state actual state\n * @param rehydratedState state from storage\n * @param action Triggered action\n */\n public readonly mergeReducer = (state: any, rehydratedState: any, action: any) => {\n if (rehydratedState) {\n if (action.type === INIT) {\n state = deepFillWithDate(state, rehydratedState);\n }\n if (action.type === UPDATE && Array.isArray(action.features)) {\n const notHandledFeatures = action.features.filter((featName: string) => !this.alreadyHydratedStoreSlices.has(featName));\n\n notHandledFeatures.filter((featName: string) => !!rehydratedState[featName]).forEach((fName: string) => {\n state[fName] = state[fName] ? deepFillWithDate(state[fName], rehydratedState[fName]) : rehydratedState[fName];\n this.alreadyHydratedStoreSlices.add(fName);\n });\n }\n }\n return state;\n };\n\n /**\n * Returns a meta reducer that handles storage sync\n */\n public localStorageSync = () => {\n const base = (reducer: any) => syncStorage({\n ...this.options,\n rehydrate: false,\n storage: this.options.storage as unknown as Storage,\n syncCondition: () => this.hasHydrated\n })(reducer);\n\n return (reducer: any) => {\n if (isLocalStorageConfig(this.options)) {\n return syncStorage(this.options)(reducer);\n }\n\n return (state: any, action: any) => {\n let hydratedState = state;\n if (action.type === rehydrateAction.type) {\n const hasAlreadyHydrated = this.hasHydrated;\n this.hasHydrated = true;\n const initialStates = this.options.keys.reduce((acc: Record<string, any>, key) => {\n const storeName = Object.keys(key)[0];\n const storeSynchronizer = (key as any)[storeName];\n if (isSerializer(storeSynchronizer)) {\n acc[storeName] = storeSynchronizer.initialState;\n }\n return acc;\n }, {});\n let overrides = {};\n if (!hasAlreadyHydrated) {\n overrides = Object.entries(action.payload).reduce<Record<string, any>>((acc, [storeName, value]) => {\n if (!initialStates[storeName] || initialStates[storeName] === state[storeName]) {\n acc[storeName] = value;\n } else if (initialStates[storeName] !== state[storeName]) {\n (this.options.logger || console).error(`Unable to rehydrate store \"${storeName}\", it has been modified before being hydrated`);\n }\n return acc;\n }, {});\n }\n if (Object.keys(overrides).length > 0) {\n hydratedState = { ...state, ...overrides };\n }\n }\n return base(reducer)(hydratedState, action);\n };\n };\n };\n}\n"],"mappings":";;;;AAQA,MAAM,SAAmC,CACvC;CACE,YAAY,SAAS,gBAAgB,MAAM;CAC3C,YAAY,SAAS,IAAI,MAAM,SAAS,IAAI;AAC7C,GACD;CACE,YAAY,SAAS,gBAAgB,MAAM;CAC3C,YAAY,SAAS,IAAI,MAAM,KAAK,IAAI;AACzC,CACF;AASD,SAAgB,iBAAiD,MAAS,QAA6B;CACrG,OAAO,SAAS,MAAM,QAAQ,MAAM;AACtC;ACNA,MAAa,eAAe,MAAc,UAAc;CACtD,IAAI,OAAO,UAAU,YAAY,6CAA6C,K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AK,GAAG,CAAC,CAAC,KAAK;MACxE,KAAK,WAAW,WAAW,MAAM;KACnC,CAAC;IACH;GACD;GACH,GAAG;GACH,SAAS,SAAS;GAClB,cAAc,OAAO,SAAS,iBAAiB,aAAa,QAAQ,eAAe,KAAK;EACzF;CACH;AA2ED"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@o3r/store-sync",
|
|
3
|
-
"version": "15.3.0-prerelease.
|
|
3
|
+
"version": "15.3.0-prerelease.17",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
"otter-module"
|
|
14
14
|
],
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"@ama-sdk/core": "~15.3.0-prerelease.
|
|
16
|
+
"@ama-sdk/core": "~15.3.0-prerelease.17",
|
|
17
17
|
"@angular-devkit/core": "^22.0.0",
|
|
18
18
|
"@angular-devkit/schematics": "^22.0.0",
|
|
19
19
|
"@ngrx/entity": "^22.0.0",
|
|
20
20
|
"@ngrx/store": "^22.0.0",
|
|
21
|
-
"@o3r/core": "~15.3.0-prerelease.
|
|
22
|
-
"@o3r/logger": "~15.3.0-prerelease.
|
|
23
|
-
"@o3r/schematics": "~15.3.0-prerelease.
|
|
21
|
+
"@o3r/core": "~15.3.0-prerelease.17",
|
|
22
|
+
"@o3r/logger": "~15.3.0-prerelease.17",
|
|
23
|
+
"@o3r/schematics": "~15.3.0-prerelease.17",
|
|
24
24
|
"@schematics/angular": "^22.0.0",
|
|
25
25
|
"fast-deep-equal": "^3.1.3",
|
|
26
26
|
"rxjs": "^7.8.1",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@o3r/schematics": "~15.3.0-prerelease.
|
|
47
|
+
"@o3r/schematics": "~15.3.0-prerelease.17",
|
|
48
48
|
"tslib": "^2.6.2"
|
|
49
49
|
},
|
|
50
50
|
"schematics": "./collection.json",
|
|
@@ -1,71 +1,68 @@
|
|
|
1
|
-
import { Logger, Serializer } from
|
|
2
|
-
import * as _ngrx_store from '@ngrx/store';
|
|
3
|
-
|
|
1
|
+
import { Logger, Serializer } from "@o3r/core";
|
|
4
2
|
/**
|
|
5
3
|
* Options to provide to the synchronous store synchronization mechanism
|
|
6
4
|
*/
|
|
7
|
-
interface SyncStorageSyncOptions {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
5
|
+
export interface SyncStorageSyncOptions {
|
|
6
|
+
/** Callback to define the serialization strategy of the store */
|
|
7
|
+
serialize?: (state: any) => any;
|
|
8
|
+
/** Callback to define the deserialization strategy of the store */
|
|
9
|
+
deserialize?: (state: any) => any;
|
|
10
|
+
/** Callback to define the reviving strategy of the store */
|
|
11
|
+
reviver?: (key: string, value: any) => any;
|
|
12
|
+
/** replacer callback function provided to the parser */
|
|
13
|
+
replacer?: ((key: string, value: any) => any) | string[];
|
|
14
|
+
/** Filter to apply to the store fields */
|
|
15
|
+
filter?: string[];
|
|
16
|
+
/** Spacing for serializer */
|
|
17
|
+
space?: string | number;
|
|
18
|
+
/** Defines whether the store is loaded as forFeature */
|
|
19
|
+
syncForFeature?: boolean;
|
|
22
20
|
}
|
|
23
21
|
/**
|
|
24
22
|
* Interface of the configurations for a store
|
|
25
23
|
*/
|
|
26
|
-
interface StorageKeyConfiguration {
|
|
27
|
-
|
|
24
|
+
export interface StorageKeyConfiguration {
|
|
25
|
+
[key: string]: string[] | number[] | StorageKeyConfiguration[] | SyncStorageSyncOptions | ((key: string, value: any) => any);
|
|
28
26
|
}
|
|
29
27
|
/**
|
|
30
28
|
* Definition of a store to to synchronize
|
|
31
29
|
*/
|
|
32
|
-
type StorageKeys = (StorageKeyConfiguration | SyncStorageSyncOptions | string)[];
|
|
30
|
+
export type StorageKeys = (StorageKeyConfiguration | SyncStorageSyncOptions | string)[];
|
|
33
31
|
/**
|
|
34
32
|
* Configuration to define the way the stores should be synchronize to the storage
|
|
35
33
|
*/
|
|
36
|
-
interface SyncStorageConfig {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
34
|
+
export interface SyncStorageConfig {
|
|
35
|
+
/** Map of the store to synchronize */
|
|
36
|
+
keys: StorageKeys;
|
|
37
|
+
/** Should the stores be initially rehydrated */
|
|
38
|
+
rehydrate?: boolean;
|
|
39
|
+
/** Storage reference */
|
|
40
|
+
storage?: Storage;
|
|
41
|
+
/** Should be removed from storage if undefined */
|
|
42
|
+
removeOnUndefined?: boolean;
|
|
43
|
+
/** Should apply restore date to the restored stores */
|
|
44
|
+
restoreDates?: boolean;
|
|
45
|
+
/** Callback to define the serialization strategy for the store keys */
|
|
46
|
+
storageKeySerializer?: (key: string) => string;
|
|
47
|
+
/** Callback to define the condition to the synchronization */
|
|
48
|
+
syncCondition?: (state: any) => any;
|
|
49
|
+
/** Callback to define the condition to the key synchronization */
|
|
50
|
+
syncKeyCondition?: (key: string, state: any) => boolean;
|
|
51
|
+
/** check the availability of the storage */
|
|
52
|
+
checkStorageAvailability?: boolean;
|
|
53
|
+
/** Merge reducer to use to deserialize the store */
|
|
54
|
+
mergeReducer?: (state: any, rehydratedState: any, action: any) => any;
|
|
55
|
+
/** Logger to report messages */
|
|
56
|
+
logger?: Logger;
|
|
57
|
+
/** Post process after sync storage execution */
|
|
58
|
+
postProcess?: (state: any) => void;
|
|
61
59
|
}
|
|
62
|
-
|
|
63
60
|
/**
|
|
64
61
|
* Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise
|
|
65
62
|
* @param _key JSON item key name
|
|
66
63
|
* @param value JSON item value
|
|
67
64
|
*/
|
|
68
|
-
declare const dateReviver: (_key: string, value: any) => any;
|
|
65
|
+
export declare const dateReviver: (_key: string, value: any) => any;
|
|
69
66
|
/**
|
|
70
67
|
* Rehydrate the state of the whole application store
|
|
71
68
|
* @param keys Keys of the store to rehydrate
|
|
@@ -74,7 +71,7 @@ declare const dateReviver: (_key: string, value: any) => any;
|
|
|
74
71
|
* @param restoreDates determine if the date need to be restored
|
|
75
72
|
* @param logger
|
|
76
73
|
*/
|
|
77
|
-
declare const rehydrateApplicationState: (keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string) => string, restoreDates: boolean, logger?: Logger) => any;
|
|
74
|
+
export declare const rehydrateApplicationState: (keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string) => string, restoreDates: boolean, logger?: Logger) => any;
|
|
78
75
|
/**
|
|
79
76
|
* Update the state of the store after reviving from storage
|
|
80
77
|
* Note: this function is mainly use for internal process of store synchronization
|
|
@@ -86,103 +83,97 @@ declare const rehydrateApplicationState: (keys: StorageKeys, storage: Storage |
|
|
|
86
83
|
* @param syncCondition callback to define if the synchronization should be process
|
|
87
84
|
* @param logger Logger to report messages
|
|
88
85
|
*/
|
|
89
|
-
declare const syncStateUpdate: (state: any, keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string | number) => string, removeOnUndefined?: boolean, syncCondition?: (state: any) => any, logger?: Logger) => void;
|
|
86
|
+
export declare const syncStateUpdate: (state: any, keys: StorageKeys, storage: Storage | undefined, storageKeySerializer: (key: string | number) => string, removeOnUndefined?: boolean, syncCondition?: (state: any) => any, logger?: Logger) => void;
|
|
90
87
|
/**
|
|
91
88
|
* Local and Session storage synchronization helper
|
|
92
89
|
* @param config
|
|
93
90
|
*/
|
|
94
|
-
declare const syncStorage: (config: SyncStorageConfig) => (reducer: any) => (state: any, action: any) => any;
|
|
95
|
-
|
|
91
|
+
export declare const syncStorage: (config: SyncStorageConfig) => (reducer: any) => (state: any, action: any) => any;
|
|
96
92
|
/**
|
|
97
93
|
* Format of a key in `StoreSyncConfig`
|
|
98
94
|
*/
|
|
99
|
-
interface StoreSyncSerializers {
|
|
100
|
-
|
|
95
|
+
export interface StoreSyncSerializers {
|
|
96
|
+
[storeName: string]: Serializer<any>;
|
|
101
97
|
}
|
|
102
98
|
/**
|
|
103
99
|
* An interface defining the configuration attributes to bootstrap `storeSyncMetaReducer`
|
|
104
100
|
*/
|
|
105
|
-
interface StoreSyncConfig {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
export interface StoreSyncConfig {
|
|
102
|
+
/** State keys to sync with storage */
|
|
103
|
+
keys: StoreSyncSerializers[];
|
|
104
|
+
/** Pull initial state from storage on startup */
|
|
105
|
+
rehydrate?: boolean;
|
|
106
|
+
/** Specify an object that conforms to the Storage interface to use, this will default to localStorage */
|
|
107
|
+
storage?: Storage;
|
|
108
|
+
/** When set, sync to storage will only occur when this function returns a true boolean */
|
|
109
|
+
syncCondition?: (state: any) => any;
|
|
114
110
|
}
|
|
115
111
|
/**
|
|
116
112
|
* Typings for async storage
|
|
117
113
|
*/
|
|
118
|
-
type AsyncStorage = Omit<Storage, 'getItem'> & {
|
|
119
|
-
|
|
114
|
+
export type AsyncStorage = Omit<Storage, 'getItem'> & {
|
|
115
|
+
getItem(key: string): Promise<string | null>;
|
|
120
116
|
};
|
|
121
117
|
/**
|
|
122
118
|
* Options for async storage sync
|
|
123
119
|
*/
|
|
124
|
-
interface AsyncStorageSyncOptions extends Omit<SyncStorageConfig, 'storage'> {
|
|
125
|
-
|
|
120
|
+
export interface AsyncStorageSyncOptions extends Omit<SyncStorageConfig, 'storage'> {
|
|
121
|
+
storage?: AsyncStorage;
|
|
126
122
|
}
|
|
127
123
|
/** Options for storage sync method */
|
|
128
|
-
type StorageSyncOptions = SyncStorageConfig | AsyncStorageSyncOptions;
|
|
124
|
+
export type StorageSyncOptions = SyncStorageConfig | AsyncStorageSyncOptions;
|
|
129
125
|
/**
|
|
130
126
|
* Options that can be set on the Storage sync constructor
|
|
131
127
|
* postProcess and syncKeyCondition will be handled by the StorageSync class if smart sync is activated
|
|
132
128
|
*/
|
|
133
|
-
type StorageSyncConstructorOptions = Omit<StorageSyncOptions, 'postProcess' | 'syncKeyCondition'>;
|
|
134
|
-
|
|
129
|
+
export type StorageSyncConstructorOptions = Omit<StorageSyncOptions, 'postProcess' | 'syncKeyCondition'>;
|
|
135
130
|
/**
|
|
136
131
|
* Storage synchronizer
|
|
137
132
|
*/
|
|
138
|
-
declare class StorageSync {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
133
|
+
export declare class StorageSync {
|
|
134
|
+
private readonly alreadyHydratedStoreSlices;
|
|
135
|
+
private hasHydrated;
|
|
136
|
+
private readonly storeImage;
|
|
137
|
+
options: StorageSyncOptions;
|
|
138
|
+
constructor(options?: StorageSyncConstructorOptions, extraOptions?: {
|
|
139
|
+
disableSmartSync: boolean;
|
|
140
|
+
});
|
|
141
|
+
/**
|
|
142
|
+
* Handle state manipulation in case of store storage sync
|
|
143
|
+
* Merge strategy is a full merge at init; When a new `store slice` is registered, its initial state is merged with its corresponding storage content
|
|
144
|
+
* Pass it as mergeReducer callback when calling localStorageSync function from 'ngrx-store-localstorage', in the app
|
|
145
|
+
* @param state actual state
|
|
146
|
+
* @param rehydratedState state from storage
|
|
147
|
+
* @param action Triggered action
|
|
148
|
+
*/
|
|
149
|
+
readonly mergeReducer: (state: any, rehydratedState: any, action: any) => any;
|
|
150
|
+
/**
|
|
151
|
+
* Returns a meta reducer that handles storage sync
|
|
152
|
+
*/
|
|
153
|
+
localStorageSync: () => (reducer: any) => (state: any, action: any) => any;
|
|
159
154
|
}
|
|
160
|
-
|
|
161
155
|
/**
|
|
162
156
|
* Defines if an object is a Serializer
|
|
163
157
|
* @param obj Object to test
|
|
164
158
|
* @returns True if the object is a Serializer
|
|
165
159
|
*/
|
|
166
|
-
declare const isSerializer: (obj: any) => obj is Serializer<any>;
|
|
160
|
+
export declare const isSerializer: (obj: any) => obj is Serializer<any>;
|
|
167
161
|
/**
|
|
168
162
|
* Label of the action to rehydrate the store
|
|
169
163
|
*/
|
|
170
|
-
declare const REHYDRATE_ACTION_LABEL = "[storeSync] rehydrate";
|
|
164
|
+
export declare const REHYDRATE_ACTION_LABEL = "[storeSync] rehydrate";
|
|
171
165
|
/**
|
|
172
166
|
* Action to rehydrate the store
|
|
173
167
|
*/
|
|
174
|
-
declare const rehydrateAction:
|
|
175
|
-
|
|
168
|
+
export declare const rehydrateAction: import("@ngrx/store").ActionCreator<"[storeSync] rehydrate", (props: {
|
|
169
|
+
payload: Record<string, any>;
|
|
176
170
|
}) => {
|
|
177
|
-
|
|
178
|
-
} &
|
|
171
|
+
payload: Record<string, any>;
|
|
172
|
+
} & import("@ngrx/store").Action<"[storeSync] rehydrate">>;
|
|
179
173
|
/**
|
|
180
174
|
* Defines if the storage is a non-async storage
|
|
181
175
|
* @param options The storage config
|
|
182
176
|
* @returns in case the storage used is a non-async storage
|
|
183
177
|
*/
|
|
184
|
-
declare const isLocalStorageConfig: (options: StorageSyncOptions) => options is SyncStorageConfig;
|
|
185
|
-
|
|
186
|
-
export { REHYDRATE_ACTION_LABEL, StorageSync, dateReviver, isLocalStorageConfig, isSerializer, rehydrateAction, rehydrateApplicationState, syncStateUpdate, syncStorage };
|
|
187
|
-
export type { AsyncStorage, AsyncStorageSyncOptions, StorageKeyConfiguration, StorageKeys, StorageSyncConstructorOptions, StorageSyncOptions, StoreSyncConfig, StoreSyncSerializers, SyncStorageConfig, SyncStorageSyncOptions };
|
|
188
|
-
//# sourceMappingURL=o3r-store-sync.d.ts.map
|
|
178
|
+
export declare const isLocalStorageConfig: (options: StorageSyncOptions) => options is SyncStorageConfig;
|
|
179
|
+
//# sourceMappingURL=o3r-store-sync.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o3r-store-sync.d.ts","sources":["../../src/sync-storage/interfaces.ts","../../src/sync-storage/storage-sync.ts","../../src/core/interfaces.ts","../../src/core/storage-sync.ts","../../src/core/storage-sync-helpers.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"o3r-store-sync.d.ts","names":[],"sources":["../../src/sync-storage/interfaces.ts","../../src/sync-storage/storage-sync.ts","../../src/core/interfaces.ts","../../src/core/storage-sync.ts","../../src/core/storage-sync-helpers.ts"],"mappings":";;;;iBAOiB;;EAEf,aAAa;;EAEb,eAAe;;EAEf,WAAW,aAAa;;EAExB,aAAa,aAAa;;EAE1B;;EAEA;;EAEA;;;;;iBAMe;GACd,oCAAoC,4BAA4B,2BAA2B,aAAa;;;;;YAM/F,eAAe,0BAA0B;;;;iBAKpC;;EAEf,MAAM;;EAEN;;EAEA,UAAU;;EAEV;;EAEA;;EAEA,wBAAwB;;EAExB,iBAAiB;;EAEjB,oBAAoB,aAAa;;EAEjC;;EAEA,gBAAgB,YAAY,sBAAsB;;EAElD,SAAS;;EAET,eAAe;;;;;;;qBCzCJ,cAAe,cAAc;;;;;;;;;qBAkD7B,4BACX,MAAM,aACN,SAAS,qBACT,uBAAuB,wBACvB,uBACA,SAAQ;;;;;;;;;;;;qBAsGG,kBACX,YACA,MAAM,aACN,SAAS,qBACT,uBAAuB,iCACvB,6BACA,iBAAiB,oBACjB,SAAQ;;;;;qBAqHG,cAAe,QAAQ,uBAAuB,kBA8BjD,YAAY;;;;iBCnUL;GACd,oBAAoB;;;;;iBAMN;;EAEf,MAAM;;EAEN;;EAEA,UAAU;;EAEV,iBAAiB;;;;;YAMP,eAAe,KAAK;EAC9B,QAAQ,cAAc;;;;;iBAMP,gCAAgC,KAAK;EACpD,UAAU;;;YAIA,qBAAqB,oBAAoB;;;;;YAMzC,gCAAgC,KAAK;;;;qBCvBpC;mBACM;UACT;mBACS;EAEV,SAAS;EAEJ,YAAA,UAAU,+BAA+B;IAAiB;;;;;;;;;;WA4BtD,eAAgB,YAAY,sBAAsB;;;;EAoB3D,yBAQG,kBAAY,YAAA;;;;;;;qBCtEX,eAAgB,aAAW,OAAO;;;;qBAKlC;;;;qBAKA,uCAAe,wCAAA;EACjB,SAAA;;EAAA,SAAA;0BACN;;;;;;qBAOQ,uBAAwB,SAAS,uBAAqB,WAAW"}
|