kiss-for-react 1.1.0 → 2.0.0
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 +226 -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 +1472 -969
- package/lib/cjs/KissAction.js +4635 -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 +4278 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +88 -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 +5199 -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 +2253 -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 +80 -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 +221 -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 +1446 -0
- package/lib/esm/KissAction.mjs +4689 -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 +4400 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +80 -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 +5199 -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 +2253 -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 +80 -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,112 +1,177 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ClassPersistor = void 0;
|
|
4
|
-
const Esserializer_1 = require("./Esserializer");
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* async () => window.localStorage.
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* async () => AsyncStorage.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClassPersistor = void 0;
|
|
4
|
+
const Esserializer_1 = require("./Esserializer");
|
|
5
|
+
const general_1 = require("./Esserializer/general");
|
|
6
|
+
const Persistor_1 = require("./Persistor");
|
|
7
|
+
const StoreException_1 = require("./StoreException");
|
|
8
|
+
/**
|
|
9
|
+
* Use it like this:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const store = createStore<AppState>({
|
|
13
|
+
* initialState: initialState,
|
|
14
|
+
* persistor: new MyPersistor(),
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Example for web:
|
|
19
|
+
* ```
|
|
20
|
+
* return new ClassPersistor<State>(
|
|
21
|
+
* async () => window.localStorage.getItem('state'),
|
|
22
|
+
* async (serialized) => window.localStorage.setItem('state', serialized),
|
|
23
|
+
* async () => window.localStorage.clear(),
|
|
24
|
+
* [State, TodoList, TodoItem, Filter] // All state classes the app uses.
|
|
25
|
+
* );
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* Example for React Native:
|
|
29
|
+
* ```
|
|
30
|
+
* return new ClassPersistor<State>(
|
|
31
|
+
* async () => AsyncStorage.getItem('state'),
|
|
32
|
+
* async (serialized) => AsyncStorage.setItem('state', serialized),
|
|
33
|
+
* async () => AsyncStorage.clear(),
|
|
34
|
+
* [State, TodoList, TodoItem, Filter] // All state classes the app uses.
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Classes are saved by their class name. Production builds usually minify class names
|
|
39
|
+
* (`TodoItem` becomes `e`), which would make different classes collide, and change the saved
|
|
40
|
+
* names from one app version to the next. To prevent this, either turn off class name
|
|
41
|
+
* minification in your bundler (see kissforreact.org), or give each state class a stable
|
|
42
|
+
* name with a static `typeName`:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* class TodoItem {
|
|
46
|
+
* static readonly typeName = 'TodoItem';
|
|
47
|
+
* ...
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* A `typeName` is not inherited: subclasses need their own. The `ClassPersistor` throws a
|
|
52
|
+
* `StoreException` when it's created, if it finds class names are minified and some class has
|
|
53
|
+
* no `typeName`, or if two classes are saved under the same name. A class with no `typeName`
|
|
54
|
+
* whose name looks minified (one or two characters, starting with a lowercase letter, `_` or
|
|
55
|
+
* `$`, like `e` or `ab`) also makes it throw, even if the rest of the code is not minified.
|
|
56
|
+
* Built-in classes, like `Date`, are not checked, as they are not saved by their name.
|
|
57
|
+
*/
|
|
58
|
+
class ClassPersistor extends Persistor_1.Persistor {
|
|
59
|
+
constructor(
|
|
60
|
+
/**
|
|
61
|
+
* Returns the serialized state. You can use this to load from localStorage or IndexedDB
|
|
62
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
63
|
+
* It should return a Promise that resolves to the serialized state, or to null if the state
|
|
64
|
+
* is not yet persisted.
|
|
65
|
+
*/
|
|
66
|
+
loadSerialized,
|
|
67
|
+
//
|
|
68
|
+
/**
|
|
69
|
+
* Saves the given serialized state. You can use this to save to localStorage or IndexedDB
|
|
70
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
71
|
+
* It should return a Promise that resolves when the state is saved.
|
|
72
|
+
*/
|
|
73
|
+
saveSerialized,
|
|
74
|
+
//
|
|
75
|
+
/**
|
|
76
|
+
* Deletes the serialized state. You can use this to delete from the localStorage or IndexedDB
|
|
77
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
78
|
+
* It should return a Promise that resolves when the state is deleted.
|
|
79
|
+
*/
|
|
80
|
+
deleteSerialized,
|
|
81
|
+
//
|
|
82
|
+
/**
|
|
83
|
+
* You HAVE to list here all the custom classes that are part of your state, directly or
|
|
84
|
+
* indirectly. Note: You don't need to list native JavaScript classes, like Date.
|
|
85
|
+
* For example, suppose you have a class `State` that has a property `todoList` of
|
|
86
|
+
* type `TodoList`.
|
|
87
|
+
* And the class `TodoList` has a property `items` of type `Array<TodoItem>`.
|
|
88
|
+
* Then you have to list all three classes here: `[State, TodoList, TodoItem]`.
|
|
89
|
+
*/
|
|
90
|
+
classesToSerialize) {
|
|
91
|
+
super();
|
|
92
|
+
this.loadSerialized = loadSerialized;
|
|
93
|
+
this.saveSerialized = saveSerialized;
|
|
94
|
+
this.deleteSerialized = deleteSerialized;
|
|
95
|
+
this.classesToSerialize = classesToSerialize;
|
|
96
|
+
this.checkClassNames();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns true if the bundler is minifying class names. Since the bundler minifies this library
|
|
100
|
+
* together with the app, we know class names are minified if this class lost its own name.
|
|
101
|
+
*/
|
|
102
|
+
classNamesAreMinified() {
|
|
103
|
+
return ClassPersistor.name !== 'ClassPersistor';
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns true if the class name looks minified: one or two characters, starting with a
|
|
107
|
+
* lowercase letter, `_` or `$`, like `e`, `ab` or `$e`. Normal names, like `myState`, don't.
|
|
108
|
+
*/
|
|
109
|
+
static looksMinified(name) {
|
|
110
|
+
return /^[a-z_$][\w$]?$/.test(name ?? '');
|
|
111
|
+
}
|
|
112
|
+
checkClassNames() {
|
|
113
|
+
const minified = this.classNamesAreMinified();
|
|
114
|
+
const classByKey = new Map();
|
|
115
|
+
for (const c of this.classesToSerialize) {
|
|
116
|
+
if (typeof c !== 'function' || !(0, general_1.isClass)(c))
|
|
117
|
+
continue; // Enums.
|
|
118
|
+
if ((0, general_1.isNativeFunction)(c))
|
|
119
|
+
continue; // Built-in classes, like Date, are not saved by their name.
|
|
120
|
+
const hasTypeName = Object.prototype.hasOwnProperty.call(c, 'typeName');
|
|
121
|
+
const key = (0, general_1.getClassKey)(c);
|
|
122
|
+
if (hasTypeName) {
|
|
123
|
+
if (typeof key !== 'string' || key.length === 0)
|
|
124
|
+
throw new StoreException_1.StoreException(`The static typeName of class "${c.name}" must be a non-empty string.`);
|
|
125
|
+
}
|
|
126
|
+
//
|
|
127
|
+
else if (minified || ClassPersistor.looksMinified(key))
|
|
128
|
+
throw new StoreException_1.StoreException(`Class names are being minified (found class "${key}"), so the ClassPersistor can't ` +
|
|
129
|
+
`save the state by class name. Either turn off class name minification in your ` +
|
|
130
|
+
`bundler (see "Class names and minification" in the persistor docs at kissforreact.org), ` +
|
|
131
|
+
`or add a static typeName to each state class, ` +
|
|
132
|
+
`like: static readonly typeName = 'TodoItem';`);
|
|
133
|
+
const previous = classByKey.get(key);
|
|
134
|
+
if (previous !== undefined && previous !== c)
|
|
135
|
+
throw new StoreException_1.StoreException(`Two different state classes are saved under the same name "${key}". ` +
|
|
136
|
+
`Give one of them a different static typeName.`);
|
|
137
|
+
classByKey.set(key, c);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Read the saved state from the persistence. Returns null if the state is not yet persisted.
|
|
142
|
+
* Don't call it yourself: the store calls it once, when it's created, and if there is a
|
|
143
|
+
* saved state, it replaces the store's initial-state. If the saved state can't be read
|
|
144
|
+
* (for example, it's corrupted, or it has a class that is not in `classesToSerialize`),
|
|
145
|
+
* this throws. The store then logs the error, gives it to the `errorObserver`, deletes the
|
|
146
|
+
* saved state, and saves the initial-state instead. See `Persistor.readState`.
|
|
147
|
+
*/
|
|
148
|
+
async readState() {
|
|
149
|
+
// Reads the JSON file as a string.
|
|
150
|
+
const serializedString = await this.loadSerialized();
|
|
151
|
+
if (serializedString === null)
|
|
152
|
+
return null;
|
|
153
|
+
Esserializer_1.ESSerializer.registerClasses(this.classesToSerialize);
|
|
154
|
+
return Esserializer_1.ESSerializer.deserialize(serializedString);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Save an initial-state to the persistence.
|
|
158
|
+
*/
|
|
159
|
+
async saveInitialState(state) {
|
|
160
|
+
const serializedString = Esserializer_1.ESSerializer.serialize(state);
|
|
161
|
+
await this.saveSerialized(serializedString);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Delete the saved state from the persistence.
|
|
165
|
+
*/
|
|
166
|
+
async deleteState() {
|
|
167
|
+
return this.deleteSerialized();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* We assume the state is small, and we save `newState` everytime, ignoring `lastPersistedState`.
|
|
171
|
+
*/
|
|
172
|
+
async persistDifference(_lastPersistedState, newState) {
|
|
173
|
+
const serializedString = Esserializer_1.ESSerializer.serialize(newState);
|
|
174
|
+
await this.saveSerialized(serializedString);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.ClassPersistor = ClassPersistor;
|
|
@@ -1,4 +1,4 @@
|
|
|
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 });
|
|
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 });
|
|
@@ -1,4 +1,4 @@
|
|
|
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 });
|
|
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 });
|
|
@@ -0,0 +1,258 @@
|
|
|
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.getBuiltinTypeByConstructor = getBuiltinTypeByConstructor;
|
|
6
|
+
exports.getBuiltinTypeBySavedType = getBuiltinTypeBySavedType;
|
|
7
|
+
exports.findInheritedBuiltinType = findInheritedBuiltinType;
|
|
8
|
+
const constant_1 = require("./constant");
|
|
9
|
+
const general_1 = require("./general");
|
|
10
|
+
const Evt_1 = require("../Evt");
|
|
11
|
+
/** Copies the bytes of a buffer (or of part of it) into a plain array. */
|
|
12
|
+
function _bytesOf(buffer, offset, length) {
|
|
13
|
+
return Array.from(new Uint8Array(buffer, offset, length));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Copies an array subclass into a plain array, keeping the holes of sparse arrays.
|
|
17
|
+
* (We can't use `slice`, because it returns an instance of the subclass.)
|
|
18
|
+
*/
|
|
19
|
+
function _toPlainArray(arr) {
|
|
20
|
+
const result = new Array(arr.length);
|
|
21
|
+
arr.forEach((item, index) => result[index] = item); // `forEach` skips holes.
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
// ---------------------------------------------------------------------------------------------
|
|
25
|
+
// Errors.
|
|
26
|
+
/** Error properties saved in `*value`. */
|
|
27
|
+
const ERROR_KEYS = ['name', 'message', 'stack', 'cause', 'errors'];
|
|
28
|
+
function _encodeError(error, serialize) {
|
|
29
|
+
const saved = {};
|
|
30
|
+
// The name is only saved when it's not the default one of its class.
|
|
31
|
+
if (error.name !== Object.getPrototypeOf(error).name) {
|
|
32
|
+
saved.name = error.name;
|
|
33
|
+
}
|
|
34
|
+
// Always saved, even when empty, as the constructor may give the error a different message.
|
|
35
|
+
saved.message = error.message;
|
|
36
|
+
if (error.stack) {
|
|
37
|
+
saved.stack = error.stack;
|
|
38
|
+
}
|
|
39
|
+
if ('cause' in error) {
|
|
40
|
+
saved.cause = serialize(error.cause);
|
|
41
|
+
}
|
|
42
|
+
if (error instanceof AggregateError) {
|
|
43
|
+
saved.errors = serialize(error.errors);
|
|
44
|
+
}
|
|
45
|
+
return saved;
|
|
46
|
+
}
|
|
47
|
+
/** Puts the saved error data into `error`, as non-enumerable properties, like the originals. */
|
|
48
|
+
function _fillError(error, saved, deserialize) {
|
|
49
|
+
// Use the saved stack, not the one of this new error.
|
|
50
|
+
delete error.stack;
|
|
51
|
+
for (const key of ERROR_KEYS) {
|
|
52
|
+
if (key in saved) {
|
|
53
|
+
(0, general_1.setHiddenProperty)(error, key, deserialize(saved[key]));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function _errorType(ErrorClass) {
|
|
58
|
+
return {
|
|
59
|
+
constructor: ErrorClass,
|
|
60
|
+
name: ErrorClass.name,
|
|
61
|
+
encode: _encodeError,
|
|
62
|
+
decode(saved, deserialize) {
|
|
63
|
+
const error = ErrorClass === AggregateError ? new AggregateError([]) : new ErrorClass();
|
|
64
|
+
_fillError(error, saved, deserialize);
|
|
65
|
+
return error;
|
|
66
|
+
},
|
|
67
|
+
fill: _fillError,
|
|
68
|
+
keysSavedInValue: ERROR_KEYS,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// ---------------------------------------------------------------------------------------------
|
|
72
|
+
// Typed arrays and Intl.
|
|
73
|
+
function _typedArrayType(TypedArrayClass) {
|
|
74
|
+
return {
|
|
75
|
+
constructor: TypedArrayClass,
|
|
76
|
+
name: TypedArrayClass.name,
|
|
77
|
+
// Items may be non-finite numbers, -0 or BigInts, so they are serialized too.
|
|
78
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
79
|
+
decode: (saved, deserialize) => new TypedArrayClass(deserialize(saved)),
|
|
80
|
+
hasItemProperties: true,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Intl objects are rebuilt from their resolved options, which include the locale. */
|
|
84
|
+
function _intlType(name, IntlClass) {
|
|
85
|
+
return {
|
|
86
|
+
constructor: IntlClass,
|
|
87
|
+
name,
|
|
88
|
+
encode: (target) => target.resolvedOptions(),
|
|
89
|
+
decode: (saved) => {
|
|
90
|
+
const { locale, ...options } = saved;
|
|
91
|
+
return new IntlClass(locale, options);
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// ---------------------------------------------------------------------------------------------
|
|
96
|
+
// The table of built-in types.
|
|
97
|
+
const _BUILTIN_TYPES = [
|
|
98
|
+
{
|
|
99
|
+
constructor: Array, // Only used for subclasses. Plain arrays are saved as JSON arrays.
|
|
100
|
+
name: 'Array',
|
|
101
|
+
encode: (target, serialize) => serialize(_toPlainArray(target)),
|
|
102
|
+
decode: (saved, deserialize) => deserialize(saved),
|
|
103
|
+
fill(instance, saved, deserialize) {
|
|
104
|
+
const items = deserialize(saved);
|
|
105
|
+
instance.length = items.length;
|
|
106
|
+
items.forEach((item, index) => instance[index] = item); // `forEach` skips holes.
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
constructor: Map,
|
|
111
|
+
name: 'Map',
|
|
112
|
+
// Saved as an array of [key, value] pairs.
|
|
113
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
114
|
+
decode: (saved, deserialize) => new Map(deserialize(saved)),
|
|
115
|
+
fill(instance, saved, deserialize) {
|
|
116
|
+
instance.clear(); // Removes the entries the constructor may have added.
|
|
117
|
+
for (const [key, value] of deserialize(saved))
|
|
118
|
+
instance.set(key, value);
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
constructor: Set,
|
|
123
|
+
name: 'Set',
|
|
124
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
125
|
+
decode: (saved, deserialize) => new Set(deserialize(saved)),
|
|
126
|
+
fill(instance, saved, deserialize) {
|
|
127
|
+
instance.clear(); // Removes the items the constructor may have added.
|
|
128
|
+
for (const item of deserialize(saved))
|
|
129
|
+
instance.add(item);
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
constructor: Date,
|
|
134
|
+
name: 'Date',
|
|
135
|
+
// An invalid date has a NaN timestamp, which JSON saves as null.
|
|
136
|
+
encode: (target) => target.getTime(),
|
|
137
|
+
decode: (saved) => new Date(saved ?? NaN),
|
|
138
|
+
fill: (instance, saved) => instance.setTime(saved ?? NaN),
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
constructor: RegExp,
|
|
142
|
+
name: 'RegExp',
|
|
143
|
+
encode: (target) => ({ source: target.source, flags: target.flags }),
|
|
144
|
+
decode: (saved) => new RegExp(saved.source, saved.flags),
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
constructor: Boolean,
|
|
148
|
+
name: 'Boolean',
|
|
149
|
+
encode: (target) => target.valueOf(),
|
|
150
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
151
|
+
decode: (saved) => new Boolean(saved),
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
constructor: Number,
|
|
155
|
+
name: 'Number',
|
|
156
|
+
encode: (target, serialize) => serialize(target.valueOf()),
|
|
157
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
158
|
+
decode: (saved, deserialize) => new Number(deserialize(saved)),
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
constructor: String,
|
|
162
|
+
name: 'String',
|
|
163
|
+
encode: (target) => target.valueOf(),
|
|
164
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
165
|
+
decode: (saved) => new String(saved),
|
|
166
|
+
hasItemProperties: true,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
constructor: ArrayBuffer,
|
|
170
|
+
name: 'ArrayBuffer',
|
|
171
|
+
encode: (target) => _bytesOf(target),
|
|
172
|
+
decode: (saved) => new Uint8Array(saved).buffer,
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
constructor: DataView,
|
|
176
|
+
name: 'DataView',
|
|
177
|
+
// Only the bytes the DataView covers, not its whole buffer.
|
|
178
|
+
encode: (target) => _bytesOf(target.buffer, target.byteOffset, target.byteLength),
|
|
179
|
+
decode: (saved) => new DataView(new Uint8Array(saved).buffer),
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
constructor: Intl.Locale,
|
|
183
|
+
name: 'Locale',
|
|
184
|
+
encode: (target) => target.toString(),
|
|
185
|
+
decode: (saved) => new Intl.Locale(saved),
|
|
186
|
+
},
|
|
187
|
+
...[Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array,
|
|
188
|
+
Float32Array, Float64Array, BigInt64Array, BigUint64Array,
|
|
189
|
+
].map(_typedArrayType),
|
|
190
|
+
...[Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError,
|
|
191
|
+
].map(_errorType),
|
|
192
|
+
...Object.entries({
|
|
193
|
+
Collator: Intl.Collator,
|
|
194
|
+
DateTimeFormat: Intl.DateTimeFormat,
|
|
195
|
+
DisplayNames: Intl.DisplayNames,
|
|
196
|
+
ListFormat: Intl.ListFormat,
|
|
197
|
+
NumberFormat: Intl.NumberFormat,
|
|
198
|
+
PluralRules: Intl.PluralRules,
|
|
199
|
+
RelativeTimeFormat: Intl.RelativeTimeFormat,
|
|
200
|
+
Segmenter: Intl.Segmenter,
|
|
201
|
+
})
|
|
202
|
+
.filter(([, IntlClass]) => IntlClass !== undefined) // Not every environment has all of them.
|
|
203
|
+
.map(([name, IntlClass]) => _intlType(name, IntlClass)),
|
|
204
|
+
];
|
|
205
|
+
// `SharedArrayBuffer` is not available in every environment (for example, in browsers
|
|
206
|
+
// without cross-origin isolation).
|
|
207
|
+
if (typeof SharedArrayBuffer !== 'undefined') {
|
|
208
|
+
_BUILTIN_TYPES.push({
|
|
209
|
+
constructor: SharedArrayBuffer,
|
|
210
|
+
name: 'SharedArrayBuffer',
|
|
211
|
+
encode: (target) => _bytesOf(target),
|
|
212
|
+
decode(saved) {
|
|
213
|
+
const buffer = new SharedArrayBuffer(saved.length);
|
|
214
|
+
new Uint8Array(buffer).set(saved);
|
|
215
|
+
return buffer;
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
// Kiss events. They are always saved as spent, without their value, and restored as
|
|
220
|
+
// `Evt.spent()` and `EvtState.spent()`, so that an old event doesn't fire when the app starts.
|
|
221
|
+
// The value is not saved, as it may not be serializable (and a spent event has no value).
|
|
222
|
+
// Saved as built-in types, so that they don't need to be listed in the `ClassPersistor`.
|
|
223
|
+
_BUILTIN_TYPES.push({
|
|
224
|
+
constructor: Evt_1.Evt,
|
|
225
|
+
name: 'Evt',
|
|
226
|
+
encode: () => undefined,
|
|
227
|
+
decode: () => Evt_1.Evt.spent(),
|
|
228
|
+
hasItemProperties: true, // Nothing else is saved: not the value, nor `_spentAtCreation`.
|
|
229
|
+
}, {
|
|
230
|
+
constructor: Evt_1.EvtState,
|
|
231
|
+
name: 'EvtState',
|
|
232
|
+
encode: () => undefined,
|
|
233
|
+
decode: () => Evt_1.EvtState.spent(),
|
|
234
|
+
hasItemProperties: true,
|
|
235
|
+
});
|
|
236
|
+
const _BY_CONSTRUCTOR = new Map(_BUILTIN_TYPES.map((type) => [type.constructor, type]));
|
|
237
|
+
const _BY_SAVED_TYPE = new Map(_BUILTIN_TYPES.map((type) => [constant_1.BUILTIN_PREFIX + type.name, type]));
|
|
238
|
+
/** Returns the built-in type of instances of exactly `constructor` (not of subclasses). */
|
|
239
|
+
function getBuiltinTypeByConstructor(constructor) {
|
|
240
|
+
return _BY_CONSTRUCTOR.get(constructor);
|
|
241
|
+
}
|
|
242
|
+
/** Returns the built-in type saved as `savedType` (like `@Map`). */
|
|
243
|
+
function getBuiltinTypeBySavedType(savedType) {
|
|
244
|
+
return _BY_SAVED_TYPE.get(savedType);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Returns the nearest built-in class that `target` inherits from, or undefined if it
|
|
248
|
+
* inherits only from `Object`.
|
|
249
|
+
*/
|
|
250
|
+
function findInheritedBuiltinType(target) {
|
|
251
|
+
for (let proto = Object.getPrototypeOf(target); proto !== null; proto = Object.getPrototypeOf(proto)) {
|
|
252
|
+
const type = _BY_CONSTRUCTOR.get(proto.constructor);
|
|
253
|
+
if (type) {
|
|
254
|
+
return type;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|