kiss-for-react 1.1.0 → 2.0.1
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/README.md +699 -156
- package/lib/cjs/ClassPersistor.js +177 -112
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
- package/lib/cjs/Esserializer/builtins.js +258 -0
- package/lib/cjs/Esserializer/constant.js +39 -131
- package/lib/cjs/Esserializer/deserializer.js +293 -313
- package/lib/cjs/Esserializer/general.js +91 -39
- package/lib/cjs/Esserializer/index.js +47 -43
- package/lib/cjs/Esserializer/serializer.js +168 -133
- package/lib/cjs/Evt.js +132 -0
- package/lib/cjs/Hooks.js +1474 -969
- package/lib/cjs/KissAction.js +4762 -1333
- package/lib/cjs/LoadPages.js +336 -0
- package/lib/cjs/Persistor.js +189 -120
- package/lib/cjs/ProcessPersistence.js +579 -262
- package/lib/cjs/Store.js +4342 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +166 -0
- package/lib/cjs/UserException.js +212 -218
- package/lib/cjs/Wait.js +309 -309
- package/lib/cjs/index.js +69 -42
- package/lib/cjs/server.js +54 -0
- package/lib/cjs/types/ClassPersistor.d.ts +143 -112
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
- package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/general.d.ts +28 -4
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/index.d.ts +33 -29
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/cjs/types/Evt.d.ts +124 -0
- package/lib/cjs/types/Evt.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +1174 -837
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +5257 -1398
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/LoadPages.d.ts +287 -0
- package/lib/cjs/types/LoadPages.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +187 -118
- package/lib/cjs/types/Persistor.d.ts.map +1 -1
- package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +2266 -1042
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreException.d.ts +97 -28
- package/lib/cjs/types/StoreException.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +95 -0
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +178 -169
- package/lib/cjs/types/UserException.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +12 -9
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/server.d.ts +12 -0
- package/lib/cjs/types/server.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +15 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +45 -7
- package/lib/esm/ClassPersistor.mjs +177 -0
- package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
- package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
- package/lib/esm/Esserializer/builtins.mjs +254 -0
- package/lib/esm/Esserializer/constant.mjs +28 -0
- package/lib/esm/Esserializer/deserializer.mjs +288 -0
- package/lib/esm/Esserializer/general.mjs +81 -0
- package/lib/esm/Esserializer/index.mjs +43 -0
- package/lib/esm/Esserializer/serializer.mjs +164 -0
- package/lib/esm/Evt.mjs +151 -0
- package/lib/esm/Hooks.mjs +1448 -0
- package/lib/esm/KissAction.mjs +4816 -0
- package/lib/esm/LoadPages.mjs +340 -0
- package/lib/esm/Persistor.mjs +180 -0
- package/lib/esm/ProcessPersistence.mjs +581 -0
- package/lib/esm/Store.mjs +4464 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +162 -0
- package/lib/esm/{UserException.js → UserException.mjs} +240 -246
- package/lib/esm/{Wait.js → Wait.mjs} +309 -309
- package/lib/esm/index.mjs +12 -9
- package/lib/esm/server.mjs +23 -0
- package/lib/esm/types/ClassPersistor.d.ts +143 -112
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
- package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +25 -49
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/general.d.ts +28 -4
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/index.d.ts +33 -29
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/esm/types/Evt.d.ts +124 -0
- package/lib/esm/types/Evt.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +1174 -837
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +5257 -1398
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/LoadPages.d.ts +287 -0
- package/lib/esm/types/LoadPages.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +187 -118
- package/lib/esm/types/Persistor.d.ts.map +1 -1
- package/lib/esm/types/ProcessPersistence.d.ts +197 -99
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +2266 -1042
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreException.d.ts +97 -28
- package/lib/esm/types/StoreException.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +95 -0
- package/lib/esm/types/StoreProvider.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +178 -169
- package/lib/esm/types/UserException.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +12 -9
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/server.d.ts +12 -0
- package/lib/esm/types/server.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +15 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.mjs +38 -0
- package/package.json +54 -30
- package/lib/cjs/UnmodifiableSetView.js +0 -53
- package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/ClassPersistor.js +0 -112
- package/lib/esm/Esserializer/constant.js +0 -81
- package/lib/esm/Esserializer/deserializer.js +0 -307
- package/lib/esm/Esserializer/general.js +0 -34
- package/lib/esm/Esserializer/index.js +0 -39
- package/lib/esm/Esserializer/serializer.js +0 -130
- package/lib/esm/Hooks.js +0 -955
- package/lib/esm/KissAction.js +0 -1459
- package/lib/esm/Persistor.js +0 -113
- package/lib/esm/ProcessPersistence.js +0 -262
- package/lib/esm/Store.js +0 -1606
- package/lib/esm/StoreException.js +0 -69
- package/lib/esm/UnmodifiableSetView.js +0 -50
- package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/utils.js +0 -3
|
@@ -1,313 +1,293 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
3
|
-
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.
|
|
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
|
-
return
|
|
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
|
-
|
|
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
|
-
return
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const className = c.name;
|
|
295
|
-
const previousClass = classMapping[className];
|
|
296
|
-
if (previousClass && previousClass !== c) {
|
|
297
|
-
console.warn('WARNING: Found class definition with the same name: ' + className);
|
|
298
|
-
}
|
|
299
|
-
// @ts-ignore
|
|
300
|
-
classMapping[className] = c;
|
|
301
|
-
});
|
|
302
|
-
return classMapping;
|
|
303
|
-
}
|
|
304
|
-
exports.getClassMappingFromClassArray = getClassMappingFromClassArray;
|
|
305
|
-
/**
|
|
306
|
-
*
|
|
307
|
-
* @param classObj It's a Class definition. "any" is used in code only
|
|
308
|
-
* because there is no TypeScript type definition for Class.
|
|
309
|
-
*/
|
|
310
|
-
function getParentClassName(classObj) {
|
|
311
|
-
return classObj.prototype.__proto__.constructor.name;
|
|
312
|
-
}
|
|
313
|
-
exports.getParentClassName = getParentClassName;
|
|
1
|
+
"use strict";
|
|
2
|
+
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
3
|
+
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.deserializeFromParsedObj = deserializeFromParsedObj;
|
|
6
|
+
exports.deserializeFromParsedObjWithClassMapping = deserializeFromParsedObjWithClassMapping;
|
|
7
|
+
exports.getClassMappingFromClassArray = getClassMappingFromClassArray;
|
|
8
|
+
exports.getParentClassName = getParentClassName;
|
|
9
|
+
const general_1 = require("./general");
|
|
10
|
+
const constant_1 = require("./constant");
|
|
11
|
+
const builtins_1 = require("./builtins");
|
|
12
|
+
const REGEXP_BEGIN_WITH_CLASS = /^\s*class\s+/;
|
|
13
|
+
/**
|
|
14
|
+
* Values tried, in order, for the constructor parameters that have no saved value.
|
|
15
|
+
* Some constructors throw for some values (for example, `{}` where a string is expected),
|
|
16
|
+
* so we try each one until the constructor works.
|
|
17
|
+
*/
|
|
18
|
+
const FALLBACK_CONSTRUCTOR_PARAMETERS = [{}, 0, '', null];
|
|
19
|
+
/**
|
|
20
|
+
* Turns a parsed JSON value back into the original value, creating instances of the
|
|
21
|
+
* given `classes` where the JSON has their names in the `*type` field.
|
|
22
|
+
*/
|
|
23
|
+
function deserializeFromParsedObj(parsedObj, classes, options) {
|
|
24
|
+
//
|
|
25
|
+
return deserializeFromParsedObjWithClassMapping(parsedObj, getClassMappingFromClassArray(classes), options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Same as `deserializeFromParsedObj`, but with the classes already mapped by name.
|
|
29
|
+
* The `options` apply only to the top-level object, not to nested ones.
|
|
30
|
+
*/
|
|
31
|
+
function deserializeFromParsedObjWithClassMapping(parsedObj, classMapping, options = {}) {
|
|
32
|
+
if ((0, general_1.notObject)(parsedObj)) {
|
|
33
|
+
return parsedObj;
|
|
34
|
+
}
|
|
35
|
+
const deserializeNested = (value) => deserializeFromParsedObjWithClassMapping(value, classMapping);
|
|
36
|
+
if (Array.isArray(parsedObj)) {
|
|
37
|
+
return _deserializeArray(parsedObj, deserializeNested);
|
|
38
|
+
}
|
|
39
|
+
const type = parsedObj[constant_1.TYPE_FIELD];
|
|
40
|
+
// A plain object.
|
|
41
|
+
if (type === undefined) {
|
|
42
|
+
return _copyDeserializedValues({}, parsedObj, classMapping, options);
|
|
43
|
+
}
|
|
44
|
+
// A built-in type.
|
|
45
|
+
if (type.startsWith(constant_1.BUILTIN_PREFIX)) {
|
|
46
|
+
const value = parsedObj[constant_1.VALUE_FIELD];
|
|
47
|
+
switch (type) {
|
|
48
|
+
case constant_1.TYPE_UNDEFINED:
|
|
49
|
+
return undefined;
|
|
50
|
+
case constant_1.TYPE_NUMBER:
|
|
51
|
+
return (0, general_1.getValueFromToStringResult)(value);
|
|
52
|
+
case constant_1.TYPE_BIGINT:
|
|
53
|
+
return BigInt(value);
|
|
54
|
+
case constant_1.TYPE_NULL_PROTOTYPE:
|
|
55
|
+
return _copyDeserializedValues(Object.create(null), parsedObj, classMapping, options);
|
|
56
|
+
}
|
|
57
|
+
const builtinType = (0, builtins_1.getBuiltinTypeBySavedType)(type);
|
|
58
|
+
if (!builtinType) {
|
|
59
|
+
throw new Error(`Unknown built-in type "${type}" found during deserialization.`);
|
|
60
|
+
}
|
|
61
|
+
const instance = builtinType.decode(value, deserializeNested);
|
|
62
|
+
return _copyDeserializedValues(instance, parsedObj, classMapping, options);
|
|
63
|
+
}
|
|
64
|
+
// A user class.
|
|
65
|
+
const classObj = Object.prototype.hasOwnProperty.call(classMapping, type) ? classMapping[type] : undefined;
|
|
66
|
+
if (!classObj) {
|
|
67
|
+
throw new Error(`Class "${type}" not found during deserialization. You must register it with: "ESSerializer.registerClass(${type});"`);
|
|
68
|
+
}
|
|
69
|
+
const constructorParameters = (options.fieldsForConstructorParameters ?? []).map((field) => {
|
|
70
|
+
const savedKey = (0, general_1.escapeKey)(field);
|
|
71
|
+
// Pass `{}` for missing fields, instead of undefined.
|
|
72
|
+
return savedKey in parsedObj ? parsedObj[savedKey] : {};
|
|
73
|
+
});
|
|
74
|
+
const instance = _createInstance(classObj, constructorParameters);
|
|
75
|
+
// A user class that extends a built-in class also has the built-in data, like the entries of a Map.
|
|
76
|
+
if (constant_1.VALUE_FIELD in parsedObj) {
|
|
77
|
+
(0, builtins_1.findInheritedBuiltinType)(instance)?.fill?.(instance, parsedObj[constant_1.VALUE_FIELD], deserializeNested);
|
|
78
|
+
}
|
|
79
|
+
return _copyDeserializedValues(instance, parsedObj, classMapping, options);
|
|
80
|
+
}
|
|
81
|
+
/** Deserializes the items of an array. Holes saved as `{"*type": "@hole"}` are kept as holes. */
|
|
82
|
+
function _deserializeArray(parsedArray, deserializeNested) {
|
|
83
|
+
const result = new Array(parsedArray.length);
|
|
84
|
+
parsedArray.forEach((item, index) => {
|
|
85
|
+
if (item?.[constant_1.TYPE_FIELD] !== constant_1.TYPE_HOLE) {
|
|
86
|
+
result[index] = deserializeNested(item);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Creates an instance of `classObj`, to receive the
|
|
93
|
+
* saved values. Constructor parameters not given in `constructorParameters` are filled with
|
|
94
|
+
* fallback values. If the constructor throws for all of them, the instance is created without
|
|
95
|
+
* calling the constructor.
|
|
96
|
+
*/
|
|
97
|
+
function _createInstance(classObj, constructorParameters) {
|
|
98
|
+
// Not `classObj.toString()`, which may be a static method of the class.
|
|
99
|
+
const isEs6Class = REGEXP_BEGIN_WITH_CLASS.test(Function.prototype.toString.call(classObj));
|
|
100
|
+
const missingParameterCount = Math.max(classObj.length - constructorParameters.length, 0);
|
|
101
|
+
// When no parameter is missing, the fallback values are not used, so we try only once.
|
|
102
|
+
const fallbacks = missingParameterCount === 0
|
|
103
|
+
? FALLBACK_CONSTRUCTOR_PARAMETERS.slice(0, 1)
|
|
104
|
+
: FALLBACK_CONSTRUCTOR_PARAMETERS;
|
|
105
|
+
for (const fallback of fallbacks) {
|
|
106
|
+
const parameters = constructorParameters.concat(new Array(missingParameterCount).fill(fallback));
|
|
107
|
+
const instance = _tryToConstruct(classObj, isEs6Class, parameters);
|
|
108
|
+
if (instance) {
|
|
109
|
+
return instance;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return Object.create(classObj.prototype);
|
|
113
|
+
}
|
|
114
|
+
/** Calls the constructor. Returns `null` if it throws. */
|
|
115
|
+
function _tryToConstruct(classObj, isEs6Class, parameters) {
|
|
116
|
+
try {
|
|
117
|
+
if (isEs6Class) {
|
|
118
|
+
return new classObj(...parameters);
|
|
119
|
+
}
|
|
120
|
+
// A function-style class.
|
|
121
|
+
let constructor = classObj.prototype.constructor;
|
|
122
|
+
if (constructor === Object) {
|
|
123
|
+
// In case the constructor is not in the prototype, such as Big in big.js.
|
|
124
|
+
constructor = classObj;
|
|
125
|
+
}
|
|
126
|
+
const instance = Object.create(constructor.prototype);
|
|
127
|
+
constructor.apply(instance, parameters);
|
|
128
|
+
return instance;
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Copies the deserialized values of `parsedObj` into `instance`, and returns it.
|
|
136
|
+
*
|
|
137
|
+
* Properties with a getter or a setter (own or inherited from the class), and read-only
|
|
138
|
+
* properties inherited from the class, keep the value the constructor gave them. Own read-only
|
|
139
|
+
* properties with object values are updated in place (see `_copyInPlace`).
|
|
140
|
+
*
|
|
141
|
+
* Some saved values can't be set in `instance`: a primitive value in an own read-only property
|
|
142
|
+
* (for example, when the constructor calls `Object.freeze(this)`), or a new property in an
|
|
143
|
+
* object that can't get new properties. Then a copy of `instance` with these values is returned
|
|
144
|
+
* instead (see `_copyWithValues`).
|
|
145
|
+
*/
|
|
146
|
+
function _copyDeserializedValues(instance, parsedObj, classMapping, options) {
|
|
147
|
+
const { ignoreProperties, rawProperties } = options;
|
|
148
|
+
// The values that can't be set in `instance`.
|
|
149
|
+
const valuesForCopy = new Map();
|
|
150
|
+
const setValue = (key, value) => {
|
|
151
|
+
const own = Object.getOwnPropertyDescriptor(instance, key);
|
|
152
|
+
const canSet = own ? own.writable !== false : Object.isExtensible(instance);
|
|
153
|
+
if (canSet) {
|
|
154
|
+
(0, general_1.setOwnProperty)(instance, key, value);
|
|
155
|
+
}
|
|
156
|
+
else if (!own || !Object.is(own.value, value)) {
|
|
157
|
+
valuesForCopy.set(key, value);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
for (const savedKey in parsedObj) {
|
|
161
|
+
if (savedKey === constant_1.TYPE_FIELD || savedKey === constant_1.VALUE_FIELD) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const key = (0, general_1.unescapeKey)(savedKey);
|
|
165
|
+
if (ignoreProperties?.includes(key)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const value = parsedObj[savedKey];
|
|
169
|
+
if (rawProperties?.includes(key)) {
|
|
170
|
+
setValue(key, JSON.stringify(value));
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const descriptor = _findPropertyDescriptor(instance, key);
|
|
174
|
+
if (descriptor) {
|
|
175
|
+
// A getter or a setter. Setting a getter-only property would throw.
|
|
176
|
+
if (descriptor.get || descriptor.set) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (descriptor.writable === false) {
|
|
180
|
+
// An inherited value is shared by all instances, so it's not changed.
|
|
181
|
+
if (Object.prototype.hasOwnProperty.call(instance, key)) {
|
|
182
|
+
const current = instance[key];
|
|
183
|
+
setValue(key, (0, general_1.notObject)(current) || _isSavedPrimitive(value)
|
|
184
|
+
? deserializeFromParsedObjWithClassMapping(value, classMapping)
|
|
185
|
+
: _copyInPlace(current, value, classMapping));
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
setValue(key, deserializeFromParsedObjWithClassMapping(value, classMapping));
|
|
191
|
+
}
|
|
192
|
+
return valuesForCopy.size === 0 ? instance : _copyWithValues(instance, valuesForCopy);
|
|
193
|
+
}
|
|
194
|
+
/** Returns true if the saved `value` is a primitive, or a primitive JSON can't represent, like `NaN`. */
|
|
195
|
+
function _isSavedPrimitive(value) {
|
|
196
|
+
if ((0, general_1.notObject)(value)) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
const type = value[constant_1.TYPE_FIELD];
|
|
200
|
+
return type === constant_1.TYPE_UNDEFINED || type === constant_1.TYPE_NUMBER || type === constant_1.TYPE_BIGINT;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Copies the saved `source` into the existing object `target`, in place: the built-in data
|
|
204
|
+
* (like the entries of a Map), and the fields, like `_copyDeserializedValues` does.
|
|
205
|
+
* Returns `target`, or a copy of it, if some saved values can't be set in it
|
|
206
|
+
* (see `_copyDeserializedValues`).
|
|
207
|
+
*/
|
|
208
|
+
function _copyInPlace(target, source, classMapping) {
|
|
209
|
+
if (constant_1.VALUE_FIELD in source) {
|
|
210
|
+
const deserializeNested = (value) => deserializeFromParsedObjWithClassMapping(value, classMapping);
|
|
211
|
+
(0, builtins_1.findInheritedBuiltinType)(target)?.fill?.(target, source[constant_1.VALUE_FIELD], deserializeNested);
|
|
212
|
+
}
|
|
213
|
+
return _copyDeserializedValues(target, source, classMapping, {});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Returns a copy of `instance`, with the given `values` in place of its own properties, or as
|
|
217
|
+
* new properties. The copy has the same prototype and the same property attributes, and it's
|
|
218
|
+
* frozen, sealed, or can't get new properties, if `instance` is.
|
|
219
|
+
*
|
|
220
|
+
* The copy is created without calling the constructor, as `_createInstance` does when the
|
|
221
|
+
* constructor throws. So it doesn't have the private fields (`#field`) of the class.
|
|
222
|
+
*
|
|
223
|
+
* An instance of a class that extends a built-in class (like a Map) keeps its built-in data
|
|
224
|
+
* where a copy can't have it. So it is returned as is, without the `values`.
|
|
225
|
+
*/
|
|
226
|
+
function _copyWithValues(instance, values) {
|
|
227
|
+
if ((0, builtins_1.findInheritedBuiltinType)(instance)) {
|
|
228
|
+
return instance;
|
|
229
|
+
}
|
|
230
|
+
const copy = Object.create(Object.getPrototypeOf(instance));
|
|
231
|
+
const descriptors = Object.getOwnPropertyDescriptors(instance);
|
|
232
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
233
|
+
const descriptor = descriptors[key];
|
|
234
|
+
if (typeof key === 'string' && values.has(key)) {
|
|
235
|
+
descriptor.value = values.get(key);
|
|
236
|
+
}
|
|
237
|
+
Object.defineProperty(copy, key, descriptor);
|
|
238
|
+
}
|
|
239
|
+
for (const [key, value] of values) {
|
|
240
|
+
if (!Object.prototype.hasOwnProperty.call(descriptors, key)) {
|
|
241
|
+
Object.defineProperty(copy, key, { value, writable: true, enumerable: true, configurable: true });
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (Object.isFrozen(instance)) {
|
|
245
|
+
Object.freeze(copy);
|
|
246
|
+
}
|
|
247
|
+
else if (Object.isSealed(instance)) {
|
|
248
|
+
Object.seal(copy);
|
|
249
|
+
}
|
|
250
|
+
else if (!Object.isExtensible(instance)) {
|
|
251
|
+
Object.preventExtensions(copy);
|
|
252
|
+
}
|
|
253
|
+
return copy;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Returns the descriptor of the property `key` of `obj`, which may be its own, or inherited
|
|
257
|
+
* from its class. `Object.prototype` is not searched, so that its `__proto__` accessor doesn't
|
|
258
|
+
* hide a saved `__proto__` key.
|
|
259
|
+
*/
|
|
260
|
+
function _findPropertyDescriptor(obj, key) {
|
|
261
|
+
for (let current = obj; current !== null && current !== Object.prototype; current = Object.getPrototypeOf(current)) {
|
|
262
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
263
|
+
if (descriptor) {
|
|
264
|
+
return descriptor;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Maps each class to the name it's saved under. Items that are not classes (like enums) are
|
|
271
|
+
* ignored. Warns if two different classes are saved under the same name.
|
|
272
|
+
*
|
|
273
|
+
* @param classes Class definitions. They are typed as `any` because TypeScript has no type for classes.
|
|
274
|
+
*/
|
|
275
|
+
function getClassMappingFromClassArray(classes = []) {
|
|
276
|
+
const classMapping = {};
|
|
277
|
+
for (const classObj of classes) {
|
|
278
|
+
if (!(0, general_1.isClass)(classObj)) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const className = (0, general_1.getClassKey)(classObj);
|
|
282
|
+
const previousClass = classMapping[className];
|
|
283
|
+
if (previousClass && previousClass !== classObj) {
|
|
284
|
+
console.warn('WARNING: Found class definition with the same name: ' + className);
|
|
285
|
+
}
|
|
286
|
+
classMapping[className] = classObj;
|
|
287
|
+
}
|
|
288
|
+
return classMapping;
|
|
289
|
+
}
|
|
290
|
+
/** Returns the name of the parent class of `classObj` (or `Object`, if it has none). */
|
|
291
|
+
function getParentClassName(classObj) {
|
|
292
|
+
return Object.getPrototypeOf(classObj.prototype).constructor.name;
|
|
293
|
+
}
|