kiss-for-react 1.0.3 → 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 +55 -31
- 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
package/lib/cjs/types/index.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { PersistAction, PersistException, Persistor, PersistorDummy, PersistorPrinterDecorator } from './Persistor';
|
|
2
|
-
import { ClassPersistor } from './ClassPersistor';
|
|
3
|
-
import { ProcessPersistence } from './ProcessPersistence';
|
|
4
|
-
import { ActionStatus, AsyncReducer, AsyncReducerResult,
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
1
|
+
import { PersistAction, PersistException, Persistor, PersistorDummy, PersistorPrinterDecorator } from './Persistor';
|
|
2
|
+
import { ClassPersistor } from './ClassPersistor';
|
|
3
|
+
import { ProcessPersistence } from './ProcessPersistence';
|
|
4
|
+
import { ActionStatus, AsyncReducer, AsyncReducerResult, OptimisticCommand, OptimisticSync, OptimisticSyncWithPush, PushMetadata, ServerPush, KissAction, MergeServerStateAction, Poll, UserExceptionAction, ReduxReducer, Retry, RetryOptions, UnlimitedRetryCheckInternet, SyncReducer, UpdateStateAction, StatusOptions } from './KissAction';
|
|
5
|
+
import { LoadPages, PageDirection, Pages, PagesToLoad } from './LoadPages';
|
|
6
|
+
import { Store, createStore, ShowUserException, PropDispose, PropPredicate, SetPropOptions, AppSignalSource } from './Store';
|
|
7
|
+
import { StoreProvider, StoreProviderProps, StoreSlice, StoreSliceProps } from './StoreProvider';
|
|
8
|
+
import { useAllState, useIsFocused, useIsOnline, useClearExceptionFor, useConfiguration, useDispatch, useDispatchAll, useDispatchAndWait, useDispatchAndWaitAll, useDispatcher, useDispatchSync, useDispatchWhen, useEnvironment, useEvent, useExceptionFor, useIsFailed, useIsStoreReady, useIsWaiting, useLayoutEvent, useObject, useSelect, useSelector, useStore, useStoreEffect, UseStoreEffectOptions, useSuspense, SuspenseOptions } from './Hooks';
|
|
9
|
+
import { AbortDispatchException, StoreException, TimeoutException } from './StoreException';
|
|
10
|
+
import { Evt, EvtState } from './Evt';
|
|
11
|
+
import { UserException } from './UserException';
|
|
12
|
+
export { Persistor, PersistorPrinterDecorator, PersistorDummy, PersistException, PersistAction, UpdateStateAction, ClassPersistor, ProcessPersistence, KissAction, MergeServerStateAction, UserExceptionAction, ActionStatus, ReduxReducer, SyncReducer, AsyncReducer, AsyncReducerResult, StatusOptions, Store, createStore, PropDispose, PropPredicate, SetPropOptions, AppSignalSource, useStore, useStoreEffect, UseStoreEffectOptions, useAllState, useSelect, useSelector, useObject, StoreProvider, StoreProviderProps, StoreSlice, StoreSliceProps, ShowUserException, useIsStoreReady, useIsWaiting, useIsFailed, useExceptionFor, useClearExceptionFor, useSuspense, SuspenseOptions, useIsFocused, useIsOnline, useEnvironment, useConfiguration, Evt, EvtState, useEvent, useLayoutEvent, useDispatch, useDispatchAll, useDispatchAndWait, useDispatchAndWaitAll, useDispatchSync, useDispatchWhen, useDispatcher, StoreException, AbortDispatchException, TimeoutException, UserException, OptimisticCommand, OptimisticSync, OptimisticSyncWithPush, ServerPush, PushMetadata, Retry, RetryOptions, UnlimitedRetryCheckInternet, Poll, LoadPages, Pages, PagesToLoad, PageDirection, };
|
|
10
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,SAAS,EAAE,cAAc,EAAE,yBAAyB,EAAG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B,EACrI,IAAI,EACJ,SAAS,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,GAC7C,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PersistAction, PersistException, Persistor, PersistorDummy, PersistorPrinterDecorator } from './Persistor';
|
|
2
|
+
import { ClassPersistor } from './ClassPersistor';
|
|
3
|
+
import { ProcessPersistence } from './ProcessPersistence';
|
|
4
|
+
import { ActionStatus, AsyncReducer, AsyncReducerResult, OptimisticCommand, OptimisticSync, OptimisticSyncWithPush, PushMetadata, ServerPush, KissAction, MergeServerStateAction, Poll, UserExceptionAction, ReduxReducer, Retry, RetryOptions, UnlimitedRetryCheckInternet, SyncReducer, UpdateStateAction, StatusOptions } from './KissAction';
|
|
5
|
+
import { LoadPages, PageDirection, Pages, PagesToLoad } from './LoadPages';
|
|
6
|
+
import { Store, createStore, ShowUserException, PropDispose, PropPredicate, SetPropOptions, AppSignalSource } from './Store';
|
|
7
|
+
import { StoreProvider, StoreProviderProps, StoreSlice, StoreSliceProps } from './StoreProvider';
|
|
8
|
+
import { AbortDispatchException, StoreException, TimeoutException } from './StoreException';
|
|
9
|
+
import { Evt, EvtState } from './Evt';
|
|
10
|
+
import { UserException } from './UserException';
|
|
11
|
+
export { Persistor, PersistorPrinterDecorator, PersistorDummy, PersistException, PersistAction, UpdateStateAction, ClassPersistor, ProcessPersistence, KissAction, MergeServerStateAction, UserExceptionAction, ActionStatus, ReduxReducer, SyncReducer, AsyncReducer, AsyncReducerResult, StatusOptions, Store, createStore, PropDispose, PropPredicate, SetPropOptions, AppSignalSource, ShowUserException, StoreProvider, StoreProviderProps, StoreSlice, StoreSliceProps, Evt, EvtState, StoreException, AbortDispatchException, TimeoutException, UserException, OptimisticCommand, OptimisticSync, OptimisticSyncWithPush, ServerPush, PushMetadata, Retry, RetryOptions, UnlimitedRetryCheckInternet, Poll, LoadPages, Pages, PagesToLoad, PageDirection, };
|
|
12
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@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EAAE,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EACxF,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAE,iBAAiB,EAClG,aAAa,EAAE,kBAAkB,EAAE,UAAU,EAAE,eAAe,EAC9D,GAAG,EAAE,QAAQ,EACb,cAAc,EACd,sBAAsB,EACtB,gBAAgB,EAChB,aAAa,EACb,iBAAiB,EAAE,cAAc,EAAE,sBAAsB,EAAE,UAAU,EAAE,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,2BAA2B,EACrI,IAAI,EACJ,SAAS,EAAE,KAAK,EAAE,WAAW,EAAE,aAAa,GAC7C,CAAC"}
|
package/lib/cjs/types/utils.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
export declare function delayMillis(ms: number): Promise<void>;
|
|
1
|
+
export declare function delayMillis(ms: number): Promise<void>;
|
|
2
|
+
/** The largest delay `setTimeout` accepts (about 24.8 days). Larger delays fire after 1 ms. */
|
|
3
|
+
export declare const MAX_TIMEOUT_MILLIS: number;
|
|
4
|
+
/**
|
|
5
|
+
* Returns the given delay, capped at the largest delay `setTimeout` accepts (about 24.8 days).
|
|
6
|
+
* Without the cap, a larger delay would fire after 1 ms.
|
|
7
|
+
*/
|
|
8
|
+
export declare function capTimeoutMillis(millis: number): number;
|
|
9
|
+
/** True for an object literal, or an object with no prototype. Not for class instances or arrays. */
|
|
10
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Turns a value into a string, for logging. Never throws, so that a circular, BigInt,
|
|
13
|
+
* prototype-less or otherwise unserializable value can still be logged.
|
|
14
|
+
*/
|
|
15
|
+
export declare function describeValue(value: any): string;
|
|
2
16
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErD"}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErD;AAED,+FAA+F;AAC/F,eAAO,MAAM,kBAAkB,QAAc,CAAC;AAE9C;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED,qGAAqG;AACrG,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAI9E;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAWhD"}
|
package/lib/cjs/utils.js
CHANGED
|
@@ -1,7 +1,45 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports.
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_TIMEOUT_MILLIS = void 0;
|
|
4
|
+
exports.delayMillis = delayMillis;
|
|
5
|
+
exports.capTimeoutMillis = capTimeoutMillis;
|
|
6
|
+
exports.isPlainObject = isPlainObject;
|
|
7
|
+
exports.describeValue = describeValue;
|
|
8
|
+
function delayMillis(ms) {
|
|
9
|
+
return new Promise(resolve => setTimeout(resolve, capTimeoutMillis(ms)));
|
|
10
|
+
}
|
|
11
|
+
/** The largest delay `setTimeout` accepts (about 24.8 days). Larger delays fire after 1 ms. */
|
|
12
|
+
exports.MAX_TIMEOUT_MILLIS = 2 ** 31 - 1;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the given delay, capped at the largest delay `setTimeout` accepts (about 24.8 days).
|
|
15
|
+
* Without the cap, a larger delay would fire after 1 ms.
|
|
16
|
+
*/
|
|
17
|
+
function capTimeoutMillis(millis) {
|
|
18
|
+
return Math.min(millis, exports.MAX_TIMEOUT_MILLIS);
|
|
19
|
+
}
|
|
20
|
+
/** True for an object literal, or an object with no prototype. Not for class instances or arrays. */
|
|
21
|
+
function isPlainObject(value) {
|
|
22
|
+
if (value === null || typeof value !== 'object')
|
|
23
|
+
return false;
|
|
24
|
+
const proto = Object.getPrototypeOf(value);
|
|
25
|
+
return proto === Object.prototype || proto === null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns a value into a string, for logging. Never throws, so that a circular, BigInt,
|
|
29
|
+
* prototype-less or otherwise unserializable value can still be logged.
|
|
30
|
+
*/
|
|
31
|
+
function describeValue(value) {
|
|
32
|
+
if (typeof value === 'bigint')
|
|
33
|
+
return `${value}n`;
|
|
34
|
+
try {
|
|
35
|
+
return JSON.stringify(value, (_key, v) => typeof v === 'bigint' ? `${v}n` : v);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
try {
|
|
39
|
+
return String(value);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return '[object]';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { ESSerializer } from './Esserializer/index.mjs';
|
|
2
|
+
import { getClassKey, isClass, isNativeFunction } from './Esserializer/general.mjs';
|
|
3
|
+
import { Persistor } from './Persistor.mjs';
|
|
4
|
+
import { StoreException } from './StoreException.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Use it like this:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const store = createStore<AppState>({
|
|
10
|
+
* initialState: initialState,
|
|
11
|
+
* persistor: new MyPersistor(),
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Example for web:
|
|
16
|
+
* ```
|
|
17
|
+
* return new ClassPersistor<State>(
|
|
18
|
+
* async () => window.localStorage.getItem('state'),
|
|
19
|
+
* async (serialized) => window.localStorage.setItem('state', serialized),
|
|
20
|
+
* async () => window.localStorage.clear(),
|
|
21
|
+
* [State, TodoList, TodoItem, Filter] // All state classes the app uses.
|
|
22
|
+
* );
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* Example for React Native:
|
|
26
|
+
* ```
|
|
27
|
+
* return new ClassPersistor<State>(
|
|
28
|
+
* async () => AsyncStorage.getItem('state'),
|
|
29
|
+
* async (serialized) => AsyncStorage.setItem('state', serialized),
|
|
30
|
+
* async () => AsyncStorage.clear(),
|
|
31
|
+
* [State, TodoList, TodoItem, Filter] // All state classes the app uses.
|
|
32
|
+
* );
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* Classes are saved by their class name. Production builds usually minify class names
|
|
36
|
+
* (`TodoItem` becomes `e`), which would make different classes collide, and change the saved
|
|
37
|
+
* names from one app version to the next. To prevent this, either turn off class name
|
|
38
|
+
* minification in your bundler (see kissforreact.org), or give each state class a stable
|
|
39
|
+
* name with a static `typeName`:
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* class TodoItem {
|
|
43
|
+
* static readonly typeName = 'TodoItem';
|
|
44
|
+
* ...
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* A `typeName` is not inherited: subclasses need their own. The `ClassPersistor` throws a
|
|
49
|
+
* `StoreException` when it's created, if it finds class names are minified and some class has
|
|
50
|
+
* no `typeName`, or if two classes are saved under the same name. A class with no `typeName`
|
|
51
|
+
* whose name looks minified (one or two characters, starting with a lowercase letter, `_` or
|
|
52
|
+
* `$`, like `e` or `ab`) also makes it throw, even if the rest of the code is not minified.
|
|
53
|
+
* Built-in classes, like `Date`, are not checked, as they are not saved by their name.
|
|
54
|
+
*/
|
|
55
|
+
export class ClassPersistor extends Persistor {
|
|
56
|
+
loadSerialized;
|
|
57
|
+
saveSerialized;
|
|
58
|
+
deleteSerialized;
|
|
59
|
+
classesToSerialize;
|
|
60
|
+
constructor(
|
|
61
|
+
/**
|
|
62
|
+
* Returns the serialized state. You can use this to load from localStorage or IndexedDB
|
|
63
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
64
|
+
* It should return a Promise that resolves to the serialized state, or to null if the state
|
|
65
|
+
* is not yet persisted.
|
|
66
|
+
*/
|
|
67
|
+
loadSerialized,
|
|
68
|
+
//
|
|
69
|
+
/**
|
|
70
|
+
* Saves the given serialized state. You can use this to save to localStorage or IndexedDB
|
|
71
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
72
|
+
* It should return a Promise that resolves when the state is saved.
|
|
73
|
+
*/
|
|
74
|
+
saveSerialized,
|
|
75
|
+
//
|
|
76
|
+
/**
|
|
77
|
+
* Deletes the serialized state. You can use this to delete from the localStorage or IndexedDB
|
|
78
|
+
* (in React web), or AsyncStorage (in React Native) or other.
|
|
79
|
+
* It should return a Promise that resolves when the state is deleted.
|
|
80
|
+
*/
|
|
81
|
+
deleteSerialized,
|
|
82
|
+
//
|
|
83
|
+
/**
|
|
84
|
+
* You HAVE to list here all the custom classes that are part of your state, directly or
|
|
85
|
+
* indirectly. Note: You don't need to list native JavaScript classes, like Date.
|
|
86
|
+
* For example, suppose you have a class `State` that has a property `todoList` of
|
|
87
|
+
* type `TodoList`.
|
|
88
|
+
* And the class `TodoList` has a property `items` of type `Array<TodoItem>`.
|
|
89
|
+
* Then you have to list all three classes here: `[State, TodoList, TodoItem]`.
|
|
90
|
+
*/
|
|
91
|
+
classesToSerialize) {
|
|
92
|
+
super();
|
|
93
|
+
this.loadSerialized = loadSerialized;
|
|
94
|
+
this.saveSerialized = saveSerialized;
|
|
95
|
+
this.deleteSerialized = deleteSerialized;
|
|
96
|
+
this.classesToSerialize = classesToSerialize;
|
|
97
|
+
this.checkClassNames();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns true if the bundler is minifying class names. Since the bundler minifies this library
|
|
101
|
+
* together with the app, we know class names are minified if this class lost its own name.
|
|
102
|
+
*/
|
|
103
|
+
classNamesAreMinified() {
|
|
104
|
+
return ClassPersistor.name !== 'ClassPersistor';
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns true if the class name looks minified: one or two characters, starting with a
|
|
108
|
+
* lowercase letter, `_` or `$`, like `e`, `ab` or `$e`. Normal names, like `myState`, don't.
|
|
109
|
+
*/
|
|
110
|
+
static looksMinified(name) {
|
|
111
|
+
return /^[a-z_$][\w$]?$/.test(name ?? '');
|
|
112
|
+
}
|
|
113
|
+
checkClassNames() {
|
|
114
|
+
const minified = this.classNamesAreMinified();
|
|
115
|
+
const classByKey = new Map();
|
|
116
|
+
for (const c of this.classesToSerialize) {
|
|
117
|
+
if (typeof c !== 'function' || !isClass(c))
|
|
118
|
+
continue; // Enums.
|
|
119
|
+
if (isNativeFunction(c))
|
|
120
|
+
continue; // Built-in classes, like Date, are not saved by their name.
|
|
121
|
+
const hasTypeName = Object.prototype.hasOwnProperty.call(c, 'typeName');
|
|
122
|
+
const key = getClassKey(c);
|
|
123
|
+
if (hasTypeName) {
|
|
124
|
+
if (typeof key !== 'string' || key.length === 0)
|
|
125
|
+
throw new StoreException(`The static typeName of class "${c.name}" must be a non-empty string.`);
|
|
126
|
+
}
|
|
127
|
+
//
|
|
128
|
+
else if (minified || ClassPersistor.looksMinified(key))
|
|
129
|
+
throw new StoreException(`Class names are being minified (found class "${key}"), so the ClassPersistor can't ` +
|
|
130
|
+
`save the state by class name. Either turn off class name minification in your ` +
|
|
131
|
+
`bundler (see "Class names and minification" in the persistor docs at kissforreact.org), ` +
|
|
132
|
+
`or add a static typeName to each state class, ` +
|
|
133
|
+
`like: static readonly typeName = 'TodoItem';`);
|
|
134
|
+
const previous = classByKey.get(key);
|
|
135
|
+
if (previous !== undefined && previous !== c)
|
|
136
|
+
throw new StoreException(`Two different state classes are saved under the same name "${key}". ` +
|
|
137
|
+
`Give one of them a different static typeName.`);
|
|
138
|
+
classByKey.set(key, c);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Read the saved state from the persistence. Returns null if the state is not yet persisted.
|
|
143
|
+
* Don't call it yourself: the store calls it once, when it's created, and if there is a
|
|
144
|
+
* saved state, it replaces the store's initial-state. If the saved state can't be read
|
|
145
|
+
* (for example, it's corrupted, or it has a class that is not in `classesToSerialize`),
|
|
146
|
+
* this throws. The store then logs the error, gives it to the `errorObserver`, deletes the
|
|
147
|
+
* saved state, and saves the initial-state instead. See `Persistor.readState`.
|
|
148
|
+
*/
|
|
149
|
+
async readState() {
|
|
150
|
+
// Reads the JSON file as a string.
|
|
151
|
+
const serializedString = await this.loadSerialized();
|
|
152
|
+
if (serializedString === null)
|
|
153
|
+
return null;
|
|
154
|
+
ESSerializer.registerClasses(this.classesToSerialize);
|
|
155
|
+
return ESSerializer.deserialize(serializedString);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Save an initial-state to the persistence.
|
|
159
|
+
*/
|
|
160
|
+
async saveInitialState(state) {
|
|
161
|
+
const serializedString = ESSerializer.serialize(state);
|
|
162
|
+
await this.saveSerialized(serializedString);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Delete the saved state from the persistence.
|
|
166
|
+
*/
|
|
167
|
+
async deleteState() {
|
|
168
|
+
return this.deleteSerialized();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* We assume the state is small, and we save `newState` everytime, ignoring `lastPersistedState`.
|
|
172
|
+
*/
|
|
173
|
+
async persistDifference(_lastPersistedState, newState) {
|
|
174
|
+
const serializedString = ESSerializer.serialize(newState);
|
|
175
|
+
await this.saveSerialized(serializedString);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
-
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
-
export {};
|
|
1
|
+
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
+
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
+
export {};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
-
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
-
export {};
|
|
1
|
+
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
+
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
+
export {};
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
+
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
+
import { BUILTIN_PREFIX } from './constant.mjs';
|
|
4
|
+
import { setHiddenProperty } from './general.mjs';
|
|
5
|
+
import { Evt, EvtState } from '../Evt.mjs';
|
|
6
|
+
/** Copies the bytes of a buffer (or of part of it) into a plain array. */
|
|
7
|
+
function _bytesOf(buffer, offset, length) {
|
|
8
|
+
return Array.from(new Uint8Array(buffer, offset, length));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Copies an array subclass into a plain array, keeping the holes of sparse arrays.
|
|
12
|
+
* (We can't use `slice`, because it returns an instance of the subclass.)
|
|
13
|
+
*/
|
|
14
|
+
function _toPlainArray(arr) {
|
|
15
|
+
const result = new Array(arr.length);
|
|
16
|
+
arr.forEach((item, index) => result[index] = item); // `forEach` skips holes.
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
// ---------------------------------------------------------------------------------------------
|
|
20
|
+
// Errors.
|
|
21
|
+
/** Error properties saved in `*value`. */
|
|
22
|
+
const ERROR_KEYS = ['name', 'message', 'stack', 'cause', 'errors'];
|
|
23
|
+
function _encodeError(error, serialize) {
|
|
24
|
+
const saved = {};
|
|
25
|
+
// The name is only saved when it's not the default one of its class.
|
|
26
|
+
if (error.name !== Object.getPrototypeOf(error).name) {
|
|
27
|
+
saved.name = error.name;
|
|
28
|
+
}
|
|
29
|
+
// Always saved, even when empty, as the constructor may give the error a different message.
|
|
30
|
+
saved.message = error.message;
|
|
31
|
+
if (error.stack) {
|
|
32
|
+
saved.stack = error.stack;
|
|
33
|
+
}
|
|
34
|
+
if ('cause' in error) {
|
|
35
|
+
saved.cause = serialize(error.cause);
|
|
36
|
+
}
|
|
37
|
+
if (error instanceof AggregateError) {
|
|
38
|
+
saved.errors = serialize(error.errors);
|
|
39
|
+
}
|
|
40
|
+
return saved;
|
|
41
|
+
}
|
|
42
|
+
/** Puts the saved error data into `error`, as non-enumerable properties, like the originals. */
|
|
43
|
+
function _fillError(error, saved, deserialize) {
|
|
44
|
+
// Use the saved stack, not the one of this new error.
|
|
45
|
+
delete error.stack;
|
|
46
|
+
for (const key of ERROR_KEYS) {
|
|
47
|
+
if (key in saved) {
|
|
48
|
+
setHiddenProperty(error, key, deserialize(saved[key]));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function _errorType(ErrorClass) {
|
|
53
|
+
return {
|
|
54
|
+
constructor: ErrorClass,
|
|
55
|
+
name: ErrorClass.name,
|
|
56
|
+
encode: _encodeError,
|
|
57
|
+
decode(saved, deserialize) {
|
|
58
|
+
const error = ErrorClass === AggregateError ? new AggregateError([]) : new ErrorClass();
|
|
59
|
+
_fillError(error, saved, deserialize);
|
|
60
|
+
return error;
|
|
61
|
+
},
|
|
62
|
+
fill: _fillError,
|
|
63
|
+
keysSavedInValue: ERROR_KEYS,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
// ---------------------------------------------------------------------------------------------
|
|
67
|
+
// Typed arrays and Intl.
|
|
68
|
+
function _typedArrayType(TypedArrayClass) {
|
|
69
|
+
return {
|
|
70
|
+
constructor: TypedArrayClass,
|
|
71
|
+
name: TypedArrayClass.name,
|
|
72
|
+
// Items may be non-finite numbers, -0 or BigInts, so they are serialized too.
|
|
73
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
74
|
+
decode: (saved, deserialize) => new TypedArrayClass(deserialize(saved)),
|
|
75
|
+
hasItemProperties: true,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Intl objects are rebuilt from their resolved options, which include the locale. */
|
|
79
|
+
function _intlType(name, IntlClass) {
|
|
80
|
+
return {
|
|
81
|
+
constructor: IntlClass,
|
|
82
|
+
name,
|
|
83
|
+
encode: (target) => target.resolvedOptions(),
|
|
84
|
+
decode: (saved) => {
|
|
85
|
+
const { locale, ...options } = saved;
|
|
86
|
+
return new IntlClass(locale, options);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// ---------------------------------------------------------------------------------------------
|
|
91
|
+
// The table of built-in types.
|
|
92
|
+
const _BUILTIN_TYPES = [
|
|
93
|
+
{
|
|
94
|
+
constructor: Array, // Only used for subclasses. Plain arrays are saved as JSON arrays.
|
|
95
|
+
name: 'Array',
|
|
96
|
+
encode: (target, serialize) => serialize(_toPlainArray(target)),
|
|
97
|
+
decode: (saved, deserialize) => deserialize(saved),
|
|
98
|
+
fill(instance, saved, deserialize) {
|
|
99
|
+
const items = deserialize(saved);
|
|
100
|
+
instance.length = items.length;
|
|
101
|
+
items.forEach((item, index) => instance[index] = item); // `forEach` skips holes.
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
constructor: Map,
|
|
106
|
+
name: 'Map',
|
|
107
|
+
// Saved as an array of [key, value] pairs.
|
|
108
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
109
|
+
decode: (saved, deserialize) => new Map(deserialize(saved)),
|
|
110
|
+
fill(instance, saved, deserialize) {
|
|
111
|
+
instance.clear(); // Removes the entries the constructor may have added.
|
|
112
|
+
for (const [key, value] of deserialize(saved))
|
|
113
|
+
instance.set(key, value);
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
constructor: Set,
|
|
118
|
+
name: 'Set',
|
|
119
|
+
encode: (target, serialize) => serialize(Array.from(target)),
|
|
120
|
+
decode: (saved, deserialize) => new Set(deserialize(saved)),
|
|
121
|
+
fill(instance, saved, deserialize) {
|
|
122
|
+
instance.clear(); // Removes the items the constructor may have added.
|
|
123
|
+
for (const item of deserialize(saved))
|
|
124
|
+
instance.add(item);
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
constructor: Date,
|
|
129
|
+
name: 'Date',
|
|
130
|
+
// An invalid date has a NaN timestamp, which JSON saves as null.
|
|
131
|
+
encode: (target) => target.getTime(),
|
|
132
|
+
decode: (saved) => new Date(saved ?? NaN),
|
|
133
|
+
fill: (instance, saved) => instance.setTime(saved ?? NaN),
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
constructor: RegExp,
|
|
137
|
+
name: 'RegExp',
|
|
138
|
+
encode: (target) => ({ source: target.source, flags: target.flags }),
|
|
139
|
+
decode: (saved) => new RegExp(saved.source, saved.flags),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
constructor: Boolean,
|
|
143
|
+
name: 'Boolean',
|
|
144
|
+
encode: (target) => target.valueOf(),
|
|
145
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
146
|
+
decode: (saved) => new Boolean(saved),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
constructor: Number,
|
|
150
|
+
name: 'Number',
|
|
151
|
+
encode: (target, serialize) => serialize(target.valueOf()),
|
|
152
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
153
|
+
decode: (saved, deserialize) => new Number(deserialize(saved)),
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
constructor: String,
|
|
157
|
+
name: 'String',
|
|
158
|
+
encode: (target) => target.valueOf(),
|
|
159
|
+
// noinspection JSPrimitiveTypeWrapperUsage
|
|
160
|
+
decode: (saved) => new String(saved),
|
|
161
|
+
hasItemProperties: true,
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
constructor: ArrayBuffer,
|
|
165
|
+
name: 'ArrayBuffer',
|
|
166
|
+
encode: (target) => _bytesOf(target),
|
|
167
|
+
decode: (saved) => new Uint8Array(saved).buffer,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
constructor: DataView,
|
|
171
|
+
name: 'DataView',
|
|
172
|
+
// Only the bytes the DataView covers, not its whole buffer.
|
|
173
|
+
encode: (target) => _bytesOf(target.buffer, target.byteOffset, target.byteLength),
|
|
174
|
+
decode: (saved) => new DataView(new Uint8Array(saved).buffer),
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
constructor: Intl.Locale,
|
|
178
|
+
name: 'Locale',
|
|
179
|
+
encode: (target) => target.toString(),
|
|
180
|
+
decode: (saved) => new Intl.Locale(saved),
|
|
181
|
+
},
|
|
182
|
+
...[Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array,
|
|
183
|
+
Float32Array, Float64Array, BigInt64Array, BigUint64Array,
|
|
184
|
+
].map(_typedArrayType),
|
|
185
|
+
...[Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError,
|
|
186
|
+
].map(_errorType),
|
|
187
|
+
...Object.entries({
|
|
188
|
+
Collator: Intl.Collator,
|
|
189
|
+
DateTimeFormat: Intl.DateTimeFormat,
|
|
190
|
+
DisplayNames: Intl.DisplayNames,
|
|
191
|
+
ListFormat: Intl.ListFormat,
|
|
192
|
+
NumberFormat: Intl.NumberFormat,
|
|
193
|
+
PluralRules: Intl.PluralRules,
|
|
194
|
+
RelativeTimeFormat: Intl.RelativeTimeFormat,
|
|
195
|
+
Segmenter: Intl.Segmenter,
|
|
196
|
+
})
|
|
197
|
+
.filter(([, IntlClass]) => IntlClass !== undefined) // Not every environment has all of them.
|
|
198
|
+
.map(([name, IntlClass]) => _intlType(name, IntlClass)),
|
|
199
|
+
];
|
|
200
|
+
// `SharedArrayBuffer` is not available in every environment (for example, in browsers
|
|
201
|
+
// without cross-origin isolation).
|
|
202
|
+
if (typeof SharedArrayBuffer !== 'undefined') {
|
|
203
|
+
_BUILTIN_TYPES.push({
|
|
204
|
+
constructor: SharedArrayBuffer,
|
|
205
|
+
name: 'SharedArrayBuffer',
|
|
206
|
+
encode: (target) => _bytesOf(target),
|
|
207
|
+
decode(saved) {
|
|
208
|
+
const buffer = new SharedArrayBuffer(saved.length);
|
|
209
|
+
new Uint8Array(buffer).set(saved);
|
|
210
|
+
return buffer;
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
// Kiss events. They are always saved as spent, without their value, and restored as
|
|
215
|
+
// `Evt.spent()` and `EvtState.spent()`, so that an old event doesn't fire when the app starts.
|
|
216
|
+
// The value is not saved, as it may not be serializable (and a spent event has no value).
|
|
217
|
+
// Saved as built-in types, so that they don't need to be listed in the `ClassPersistor`.
|
|
218
|
+
_BUILTIN_TYPES.push({
|
|
219
|
+
constructor: Evt,
|
|
220
|
+
name: 'Evt',
|
|
221
|
+
encode: () => undefined,
|
|
222
|
+
decode: () => Evt.spent(),
|
|
223
|
+
hasItemProperties: true, // Nothing else is saved: not the value, nor `_spentAtCreation`.
|
|
224
|
+
}, {
|
|
225
|
+
constructor: EvtState,
|
|
226
|
+
name: 'EvtState',
|
|
227
|
+
encode: () => undefined,
|
|
228
|
+
decode: () => EvtState.spent(),
|
|
229
|
+
hasItemProperties: true,
|
|
230
|
+
});
|
|
231
|
+
const _BY_CONSTRUCTOR = new Map(_BUILTIN_TYPES.map((type) => [type.constructor, type]));
|
|
232
|
+
const _BY_SAVED_TYPE = new Map(_BUILTIN_TYPES.map((type) => [BUILTIN_PREFIX + type.name, type]));
|
|
233
|
+
/** Returns the built-in type of instances of exactly `constructor` (not of subclasses). */
|
|
234
|
+
function getBuiltinTypeByConstructor(constructor) {
|
|
235
|
+
return _BY_CONSTRUCTOR.get(constructor);
|
|
236
|
+
}
|
|
237
|
+
/** Returns the built-in type saved as `savedType` (like `@Map`). */
|
|
238
|
+
function getBuiltinTypeBySavedType(savedType) {
|
|
239
|
+
return _BY_SAVED_TYPE.get(savedType);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Returns the nearest built-in class that `target` inherits from, or undefined if it
|
|
243
|
+
* inherits only from `Object`.
|
|
244
|
+
*/
|
|
245
|
+
function findInheritedBuiltinType(target) {
|
|
246
|
+
for (let proto = Object.getPrototypeOf(target); proto !== null; proto = Object.getPrototypeOf(proto)) {
|
|
247
|
+
const type = _BY_CONSTRUCTOR.get(proto.constructor);
|
|
248
|
+
if (type) {
|
|
249
|
+
return type;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
export { getBuiltinTypeByConstructor, getBuiltinTypeBySavedType, findInheritedBuiltinType, };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The Esserializer code was copied and adapted from the original copyrighted work, MIT licensed, by cshao.
|
|
2
|
+
// All credit goes to him. See: https://www.npmjs.com/package/esserializer
|
|
3
|
+
/** The type of a saved object: the name of a user class, or a built-in type (starting with `@`). */
|
|
4
|
+
const TYPE_FIELD = '*type';
|
|
5
|
+
/** The data needed to rebuild a built-in value (its items, timestamp, text form, etc.). */
|
|
6
|
+
const VALUE_FIELD = '*value';
|
|
7
|
+
/**
|
|
8
|
+
* Prefix of the built-in types, like `@Map` or `@undefined`. User classes can't be saved
|
|
9
|
+
* under names starting with it, so they are never confused with built-in types.
|
|
10
|
+
*/
|
|
11
|
+
const BUILTIN_PREFIX = '@';
|
|
12
|
+
// Special types, for values that JSON can't represent.
|
|
13
|
+
/** `undefined`. */
|
|
14
|
+
const TYPE_UNDEFINED = '@undefined';
|
|
15
|
+
/** Numbers JSON can't represent: `NaN`, `Infinity`, `-Infinity` and `-0`. Saved as their text. */
|
|
16
|
+
const TYPE_NUMBER = '@number';
|
|
17
|
+
/** A `bigint`. Saved as its text. */
|
|
18
|
+
const TYPE_BIGINT = '@bigint';
|
|
19
|
+
/** A missing item of a sparse array. */
|
|
20
|
+
const TYPE_HOLE = '@hole';
|
|
21
|
+
/** An object with no prototype, like one made with `Object.create(null)`. Its properties are saved as usual. */
|
|
22
|
+
const TYPE_NULL_PROTOTYPE = '@nullPrototype';
|
|
23
|
+
/**
|
|
24
|
+
* User keys starting with this are saved with one more of it, so that they are not
|
|
25
|
+
* confused with the metadata fields. For example, a key `*type` is saved as `**type`.
|
|
26
|
+
*/
|
|
27
|
+
const KEY_ESCAPE = '*';
|
|
28
|
+
export { TYPE_FIELD, VALUE_FIELD, BUILTIN_PREFIX, TYPE_UNDEFINED, TYPE_NUMBER, TYPE_BIGINT, TYPE_HOLE, TYPE_NULL_PROTOTYPE, KEY_ESCAPE, };
|