kiss-for-react 1.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/LICENSE +21 -0
- package/README.md +555 -0
- package/lib/cjs/ClassPersistor.js +112 -0
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -0
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -0
- package/lib/cjs/Esserializer/constant.js +131 -0
- package/lib/cjs/Esserializer/deserializer.js +313 -0
- package/lib/cjs/Esserializer/general.js +39 -0
- package/lib/cjs/Esserializer/index.js +43 -0
- package/lib/cjs/Esserializer/serializer.js +133 -0
- package/lib/cjs/Hooks.js +969 -0
- package/lib/cjs/KissAction.js +1268 -0
- package/lib/cjs/Persistor.js +120 -0
- package/lib/cjs/ProcessPersistence.js +262 -0
- package/lib/cjs/Store.js +1534 -0
- package/lib/cjs/StoreException.js +75 -0
- package/lib/cjs/UnmodifiableSetView.js +53 -0
- package/lib/cjs/UserException.js +218 -0
- package/lib/cjs/Wait.js +309 -0
- package/lib/cjs/index.js +42 -0
- package/lib/cjs/types/ClassPersistor.d.ts +113 -0
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +7 -0
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +6 -0
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +50 -0
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/deserializer.d.ts +18 -0
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/general.d.ts +5 -0
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/index.d.ts +30 -0
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/serializer.d.ts +4 -0
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +838 -0
- package/lib/cjs/types/Hooks.d.ts.map +1 -0
- package/lib/cjs/types/KissAction.d.ts +1324 -0
- package/lib/cjs/types/KissAction.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +119 -0
- package/lib/cjs/types/Persistor.d.ts.map +1 -0
- package/lib/cjs/types/ProcessPersistence.d.ts +100 -0
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -0
- package/lib/cjs/types/Store.d.ts +1043 -0
- package/lib/cjs/types/Store.d.ts.map +1 -0
- package/lib/cjs/types/StoreException.d.ts +29 -0
- package/lib/cjs/types/StoreException.d.ts.map +1 -0
- package/lib/cjs/types/UnmodifiableSetView.d.ts +24 -0
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +170 -0
- package/lib/cjs/types/UserException.d.ts.map +1 -0
- package/lib/cjs/types/Wait.d.ts +1 -0
- package/lib/cjs/types/Wait.d.ts.map +1 -0
- package/lib/cjs/types/index.d.ts +10 -0
- package/lib/cjs/types/index.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +2 -0
- package/lib/cjs/types/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +7 -0
- package/lib/esm/ClassPersistor.js +112 -0
- package/lib/esm/Esserializer/DeserializationOptions.js +3 -0
- package/lib/esm/Esserializer/SerializeOptions.js +3 -0
- package/lib/esm/Esserializer/constant.js +81 -0
- package/lib/esm/Esserializer/deserializer.js +307 -0
- package/lib/esm/Esserializer/general.js +34 -0
- package/lib/esm/Esserializer/index.js +39 -0
- package/lib/esm/Esserializer/serializer.js +130 -0
- package/lib/esm/Hooks.js +955 -0
- package/lib/esm/KissAction.js +1368 -0
- package/lib/esm/Persistor.js +113 -0
- package/lib/esm/ProcessPersistence.js +262 -0
- package/lib/esm/Store.js +1606 -0
- package/lib/esm/StoreException.js +69 -0
- package/lib/esm/UnmodifiableSetView.js +50 -0
- package/lib/esm/UserException.js +246 -0
- package/lib/esm/Wait.js +309 -0
- package/lib/esm/index.mjs +9 -0
- package/lib/esm/types/ClassPersistor.d.ts +113 -0
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +7 -0
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +6 -0
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +50 -0
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/deserializer.d.ts +18 -0
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/general.d.ts +5 -0
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/index.d.ts +30 -0
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/serializer.d.ts +4 -0
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +838 -0
- package/lib/esm/types/Hooks.d.ts.map +1 -0
- package/lib/esm/types/KissAction.d.ts +1324 -0
- package/lib/esm/types/KissAction.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +119 -0
- package/lib/esm/types/Persistor.d.ts.map +1 -0
- package/lib/esm/types/ProcessPersistence.d.ts +100 -0
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -0
- package/lib/esm/types/Store.d.ts +1043 -0
- package/lib/esm/types/Store.d.ts.map +1 -0
- package/lib/esm/types/StoreException.d.ts +29 -0
- package/lib/esm/types/StoreException.d.ts.map +1 -0
- package/lib/esm/types/UnmodifiableSetView.d.ts +24 -0
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +170 -0
- package/lib/esm/types/UserException.d.ts.map +1 -0
- package/lib/esm/types/Wait.d.ts +1 -0
- package/lib/esm/types/Wait.d.ts.map +1 -0
- package/lib/esm/types/index.d.ts +10 -0
- package/lib/esm/types/index.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +2 -0
- package/lib/esm/types/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +3 -0
- package/package.json +87 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { KissAction } from './KissAction';
|
|
2
|
+
import { Store } from './Store';
|
|
3
|
+
/**
|
|
4
|
+
* Use it like this:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const persistor = new MyPersistor();
|
|
8
|
+
*
|
|
9
|
+
* let initialState = await persistor.readState();
|
|
10
|
+
*
|
|
11
|
+
* if (initialState === null) {
|
|
12
|
+
* initialState = AppState.initialState();
|
|
13
|
+
* await persistor.saveInitialState(initialState);
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* const store = createStore<AppState>({
|
|
17
|
+
* initialState: initialState,
|
|
18
|
+
* persistor: persistor,
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* IMPORTANT: When the store is created with a Persistor, it assumes
|
|
23
|
+
* the provided initial state was already persisted. Ensure this is the case.
|
|
24
|
+
*/
|
|
25
|
+
export class Persistor {
|
|
26
|
+
/**
|
|
27
|
+
* The default throttle is 2 seconds (2000 milliseconds).
|
|
28
|
+
* Return `null` to turn off the throttle.
|
|
29
|
+
*/
|
|
30
|
+
get throttle() {
|
|
31
|
+
return 2000; // Default throttle is 2 seconds.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A decorator to print persistor information to the console.
|
|
36
|
+
* Use it like this:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* const store = createStore<AppState>({
|
|
40
|
+
* ...otherOptions,
|
|
41
|
+
* persistor: new PersistorPrinterDecorator<AppState>(persistor),
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export class PersistorPrinterDecorator {
|
|
46
|
+
_persistor;
|
|
47
|
+
constructor(persistor) {
|
|
48
|
+
this._persistor = persistor;
|
|
49
|
+
}
|
|
50
|
+
async readState() {
|
|
51
|
+
Store.log('Persistor: read state.');
|
|
52
|
+
return this._persistor.readState();
|
|
53
|
+
}
|
|
54
|
+
async deleteState() {
|
|
55
|
+
Store.log('Persistor: delete state.');
|
|
56
|
+
return this._persistor.deleteState();
|
|
57
|
+
}
|
|
58
|
+
async persistDifference(lastPersistedState, newState) {
|
|
59
|
+
Store.log(`Persistor: persist difference:
|
|
60
|
+
lastPersistedState = ${lastPersistedState}
|
|
61
|
+
newState = ${newState}`);
|
|
62
|
+
return this._persistor.persistDifference(lastPersistedState, newState);
|
|
63
|
+
}
|
|
64
|
+
async saveInitialState(state) {
|
|
65
|
+
Store.log('Persistor: save initial state.');
|
|
66
|
+
return this._persistor.saveInitialState(state);
|
|
67
|
+
}
|
|
68
|
+
get throttle() {
|
|
69
|
+
return this._persistor.throttle;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A dummy persistor.
|
|
74
|
+
*/
|
|
75
|
+
export class PersistorDummy {
|
|
76
|
+
async readState() {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
async deleteState() {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
async persistDifference(_lastPersistedState, _newState) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
async saveInitialState(_state) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
get throttle() {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export class PersistException extends Error {
|
|
93
|
+
constructor(message) {
|
|
94
|
+
super(message);
|
|
95
|
+
this.name = 'PersistException';
|
|
96
|
+
// Maintains proper stack trace for where our error was thrown (only available on V8).
|
|
97
|
+
if (Error.captureStackTrace) {
|
|
98
|
+
Error.captureStackTrace(this, PersistException);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* An action that can be dispatched to force the Persistor to persist the state immediately.
|
|
104
|
+
* It does not change the state.
|
|
105
|
+
*
|
|
106
|
+
* Note: This only works if the Persistor is waiting for the throttle period.
|
|
107
|
+
* If the Persistor is currently persisting the state, this action will be ignored.
|
|
108
|
+
*/
|
|
109
|
+
export class PersistAction extends KissAction {
|
|
110
|
+
reduce() {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { PersistAction } from "./Persistor";
|
|
2
|
+
import { UpdateStateAction } from "./KissAction";
|
|
3
|
+
import { Store } from "./Store";
|
|
4
|
+
export class ProcessPersistence {
|
|
5
|
+
persistor;
|
|
6
|
+
lastPersistedState;
|
|
7
|
+
newestState;
|
|
8
|
+
isPersisting = false;
|
|
9
|
+
isANewStateAvailable = false;
|
|
10
|
+
lastPersistTime = new Date();
|
|
11
|
+
timer; // Timer's type
|
|
12
|
+
isPaused = false;
|
|
13
|
+
isInit = false;
|
|
14
|
+
finishedPersistingCallback = null;
|
|
15
|
+
constructor(persistor, lastPersistedState) {
|
|
16
|
+
this.persistor = persistor;
|
|
17
|
+
this.lastPersistedState = lastPersistedState;
|
|
18
|
+
this.newestState = null;
|
|
19
|
+
}
|
|
20
|
+
async readInitialState(store, initialState) {
|
|
21
|
+
let stateReadFromPersistor = null;
|
|
22
|
+
try {
|
|
23
|
+
stateReadFromPersistor = await this.persistor.readState();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
this.log('Error reading state:' + error + '. State will reset.');
|
|
27
|
+
try {
|
|
28
|
+
await this.persistor.deleteState();
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
this.log('Error deleting the state:' + error + '.');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (stateReadFromPersistor === null) {
|
|
35
|
+
try {
|
|
36
|
+
// If it was not possible to read the persisted state,
|
|
37
|
+
// we persist the initial-state passed to the Store constructor.
|
|
38
|
+
await this.persistor.saveInitialState(initialState);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
this.log('Error saving initial state:' + error + '.');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//
|
|
45
|
+
else {
|
|
46
|
+
// If the saved state was read successfully, we replace the store state with it.
|
|
47
|
+
// In this case, the initial-state passed in the Store constructor was used
|
|
48
|
+
// only while the persisted state is loading.
|
|
49
|
+
await store.dispatchAndWait(new UpdateStateAction((_) => stateReadFromPersistor, false));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
log(message) {
|
|
53
|
+
try {
|
|
54
|
+
Store.log(message);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Discard error.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
get throttle() {
|
|
61
|
+
return this.persistor.throttle || 0;
|
|
62
|
+
}
|
|
63
|
+
async saveInitialState(initialState) {
|
|
64
|
+
this.lastPersistedState = initialState;
|
|
65
|
+
await this.persistor.saveInitialState(initialState);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Same as `Persistor.readState` but will remember the read state as the `lastPersistedState`.
|
|
69
|
+
*/
|
|
70
|
+
async readState() {
|
|
71
|
+
const state = await this.persistor.readState();
|
|
72
|
+
this.lastPersistedState = state;
|
|
73
|
+
return state;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Same as `Persistor.deleteState` but will clear the `lastPersistedState`.
|
|
77
|
+
*/
|
|
78
|
+
async deleteState() {
|
|
79
|
+
this.lastPersistedState = null;
|
|
80
|
+
await this.persistor.deleteState();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Call `logOut()` when you want to delete the persisted state, and return the store
|
|
84
|
+
* state to the given initial-state. That's usually necessary when the user logs out
|
|
85
|
+
* of your app, or the user deletes its account, so that another user may log in,
|
|
86
|
+
* or start a new sign-up process.
|
|
87
|
+
*
|
|
88
|
+
* Note: If you know about any timers or async processes that you may have started,
|
|
89
|
+
* you should stop/cancel them all before calling this method.
|
|
90
|
+
*
|
|
91
|
+
* You may opt to:
|
|
92
|
+
*
|
|
93
|
+
* - Wait for `throttle` milliseconds to make sure all async processes that the app may
|
|
94
|
+
* have started have time to finish. The default `throttle` is 3000 milliseconds (3 seconds).
|
|
95
|
+
*
|
|
96
|
+
* - Wait for all actions currently running to finish, but wait at most `actionsThrottle`
|
|
97
|
+
* milliseconds. If the actions are not finished by then, the state will be deleted anyway.
|
|
98
|
+
* The default `actionsThrottle` is 6000 milliseconds (6 seconds).
|
|
99
|
+
*/
|
|
100
|
+
async logOut({ store, initialState, throttle = 3000, actionsThrottle = 6000, }) {
|
|
101
|
+
// Pauses the persistor, so it doesn't start a new persistence process.
|
|
102
|
+
this.pause();
|
|
103
|
+
// Cancel the persistence timer.
|
|
104
|
+
this._cancelTimer();
|
|
105
|
+
// Set the store to shut down, so it doesn't start any new actions.
|
|
106
|
+
store.setShutDown(true);
|
|
107
|
+
// If the state is NOT currently being persisted,
|
|
108
|
+
if (!this.isPersisting) {
|
|
109
|
+
// Clear the callback that is called when it finishes persisting.
|
|
110
|
+
this.finishedPersistingCallback = null;
|
|
111
|
+
// Wait for the throttle period to finish.
|
|
112
|
+
await new Promise(resolve => setTimeout(resolve, throttle));
|
|
113
|
+
// Wait for all actions currently running to finish,
|
|
114
|
+
// but wait at most `actionsThrottle` milliseconds.
|
|
115
|
+
await store.waitAllActions(null, {
|
|
116
|
+
timeoutMillis: actionsThrottle,
|
|
117
|
+
completeImmediately: true
|
|
118
|
+
})
|
|
119
|
+
.catch((error) => {
|
|
120
|
+
});
|
|
121
|
+
// Delete the old persisted state.
|
|
122
|
+
await this.deleteState();
|
|
123
|
+
// Synchronously change the store state to the initial-state.
|
|
124
|
+
store.dispatchSync(new UpdateStateAction((state) => initialState));
|
|
125
|
+
// Persist the new initial-state.
|
|
126
|
+
await this._persist(new Date(), initialState).then();
|
|
127
|
+
// Restart the store accepting new actions.
|
|
128
|
+
store.setShutDown(false);
|
|
129
|
+
}
|
|
130
|
+
//
|
|
131
|
+
// If it's currently being persisted, we can't delete the state right now.
|
|
132
|
+
else {
|
|
133
|
+
// But as soon as it finishes persisting, try again.
|
|
134
|
+
this.finishedPersistingCallback = async () => {
|
|
135
|
+
await this.logOut({
|
|
136
|
+
store, initialState, throttle, actionsThrottle
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 1) If we're still persisting the last time, don't persist no matter what.
|
|
143
|
+
* 2) If action is of type `UpdateStateAction` with `ifPersists` false,
|
|
144
|
+
* don't persist, but consider the state as persisted.
|
|
145
|
+
* 3) If throttle period is done (or if action is PersistAction), persist.
|
|
146
|
+
* 4) If throttle period is NOT done, create a timer to persist as soon as it finishes.
|
|
147
|
+
*
|
|
148
|
+
* Return true if the persist process started.
|
|
149
|
+
* Return false if persistence was postponed.
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
152
|
+
process(action, newState) {
|
|
153
|
+
this.isInit = true;
|
|
154
|
+
this.newestState = newState;
|
|
155
|
+
if (this.isPaused || this.lastPersistedState === newState)
|
|
156
|
+
return false;
|
|
157
|
+
if ((action instanceof UpdateStateAction) && (!action.ifPersists)) {
|
|
158
|
+
this.lastPersistedState = this.newestState;
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
// 1) If we're still persisting the last time, don't persist no matter what.
|
|
162
|
+
if (this.isPersisting) {
|
|
163
|
+
this.isANewStateAvailable = true;
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
//
|
|
167
|
+
else {
|
|
168
|
+
const now = new Date();
|
|
169
|
+
// 2) If throttle period is done (or if action is PersistAction), persist.
|
|
170
|
+
if (now.valueOf() - this.lastPersistTime.valueOf() >= this.throttle
|
|
171
|
+
|| (action instanceof PersistAction)) {
|
|
172
|
+
this._cancelTimer();
|
|
173
|
+
this._persist(now, newState).then();
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
// 3) If throttle period is NOT done, create a timer to persist as soon as it finishes.
|
|
177
|
+
else {
|
|
178
|
+
if (!this.timer) {
|
|
179
|
+
const asSoonAsThrottleFinishes = this.throttle - (now.valueOf() - this.lastPersistTime.valueOf());
|
|
180
|
+
this.timer = setTimeout(() => {
|
|
181
|
+
this.timer = undefined;
|
|
182
|
+
this.process(null, this.newestState); // TypeScript forced cast
|
|
183
|
+
}, asSoonAsThrottleFinishes);
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
_cancelTimer() {
|
|
190
|
+
if (this.timer) {
|
|
191
|
+
clearTimeout(this.timer);
|
|
192
|
+
this.timer = undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async _persist(now, newState) {
|
|
196
|
+
this.isPersisting = true;
|
|
197
|
+
this.lastPersistTime = now;
|
|
198
|
+
this.isANewStateAvailable = false;
|
|
199
|
+
try {
|
|
200
|
+
await this.persistor.persistDifference(this.lastPersistedState, newState);
|
|
201
|
+
}
|
|
202
|
+
//
|
|
203
|
+
finally {
|
|
204
|
+
this.lastPersistedState = newState;
|
|
205
|
+
this.isPersisting = false;
|
|
206
|
+
this.finishedPersistingCallback?.();
|
|
207
|
+
// If a new state became available while the present state was saving, save again.
|
|
208
|
+
if (this.isANewStateAvailable) {
|
|
209
|
+
this.isANewStateAvailable = false;
|
|
210
|
+
this.process(null, this.newestState);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Pause the `Persistor` temporarily.
|
|
216
|
+
*
|
|
217
|
+
* When `pause` is called, the Persistor will not start a new persistence process, until method
|
|
218
|
+
* `resume` is called. This will not affect the current persistence process, if one is currently
|
|
219
|
+
* running.
|
|
220
|
+
*
|
|
221
|
+
* Note: A persistence process starts when the `persistDifference` method is called, and
|
|
222
|
+
* finishes when the promise returned by that method completes.
|
|
223
|
+
*
|
|
224
|
+
*/
|
|
225
|
+
pause() {
|
|
226
|
+
this.isPaused = true;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Persists the current state (if it's not yet persisted), then pauses the `Persistor`
|
|
230
|
+
* temporarily.
|
|
231
|
+
*
|
|
232
|
+
*
|
|
233
|
+
* When `persistAndPause` is called, this will not affect the current persistence process, if
|
|
234
|
+
* one is currently running. If no persistence process was running, it will immediately start a
|
|
235
|
+
* new persistence process (ignoring `throttle`).
|
|
236
|
+
*
|
|
237
|
+
* Then, the Persistor will not start another persistence process, until method `resume` is
|
|
238
|
+
* called.
|
|
239
|
+
*
|
|
240
|
+
* Note: A persistence process starts when the `persistDifference` method is called, and
|
|
241
|
+
* finishes when the promise returned by that method completes.
|
|
242
|
+
*
|
|
243
|
+
*/
|
|
244
|
+
async persistAndPause() {
|
|
245
|
+
this.isPaused = true;
|
|
246
|
+
this._cancelTimer();
|
|
247
|
+
if (this.isInit //
|
|
248
|
+
&& !this.isPersisting //
|
|
249
|
+
&& (this.lastPersistedState !== this.newestState)) {
|
|
250
|
+
const now = new Date();
|
|
251
|
+
return this._persist(now, this.newestState); // TypeScript forced cast
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Resumes persistence by the `Persistor`, after calling `pause` or `persistAndPause`.
|
|
256
|
+
*/
|
|
257
|
+
resume() {
|
|
258
|
+
this.isPaused = false;
|
|
259
|
+
if (this.isInit)
|
|
260
|
+
this.process(null, this.newestState);
|
|
261
|
+
}
|
|
262
|
+
}
|