kiss-for-react 1.1.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +699 -156
- package/lib/cjs/ClassPersistor.js +177 -112
- package/lib/cjs/Esserializer/DeserializationOptions.js +4 -4
- package/lib/cjs/Esserializer/SerializeOptions.js +4 -4
- package/lib/cjs/Esserializer/builtins.js +258 -0
- package/lib/cjs/Esserializer/constant.js +39 -131
- package/lib/cjs/Esserializer/deserializer.js +293 -313
- package/lib/cjs/Esserializer/general.js +91 -39
- package/lib/cjs/Esserializer/index.js +47 -43
- package/lib/cjs/Esserializer/serializer.js +168 -133
- package/lib/cjs/Evt.js +132 -0
- package/lib/cjs/Hooks.js +1474 -969
- package/lib/cjs/KissAction.js +4762 -1333
- package/lib/cjs/LoadPages.js +336 -0
- package/lib/cjs/Persistor.js +189 -120
- package/lib/cjs/ProcessPersistence.js +579 -262
- package/lib/cjs/Store.js +4342 -1534
- package/lib/cjs/StoreException.js +162 -75
- package/lib/cjs/StoreProvider.js +166 -0
- package/lib/cjs/UserException.js +212 -218
- package/lib/cjs/Wait.js +309 -309
- package/lib/cjs/index.js +69 -42
- package/lib/cjs/server.js +54 -0
- package/lib/cjs/types/ClassPersistor.d.ts +143 -112
- package/lib/cjs/types/ClassPersistor.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/cjs/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/cjs/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/builtins.d.ts +36 -0
- package/lib/cjs/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/cjs/types/Esserializer/constant.d.ts +25 -49
- package/lib/cjs/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/general.d.ts +28 -4
- package/lib/cjs/types/Esserializer/general.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/index.d.ts +33 -29
- package/lib/cjs/types/Esserializer/index.d.ts.map +1 -1
- package/lib/cjs/types/Esserializer/serializer.d.ts +14 -3
- package/lib/cjs/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/cjs/types/Evt.d.ts +124 -0
- package/lib/cjs/types/Evt.d.ts.map +1 -0
- package/lib/cjs/types/Hooks.d.ts +1174 -837
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +5257 -1398
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/LoadPages.d.ts +287 -0
- package/lib/cjs/types/LoadPages.d.ts.map +1 -0
- package/lib/cjs/types/Persistor.d.ts +187 -118
- package/lib/cjs/types/Persistor.d.ts.map +1 -1
- package/lib/cjs/types/ProcessPersistence.d.ts +197 -99
- package/lib/cjs/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +2266 -1042
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreException.d.ts +97 -28
- package/lib/cjs/types/StoreException.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +95 -0
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -0
- package/lib/cjs/types/UserException.d.ts +178 -169
- package/lib/cjs/types/UserException.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +12 -9
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/server.d.ts +12 -0
- package/lib/cjs/types/server.d.ts.map +1 -0
- package/lib/cjs/types/utils.d.ts +15 -1
- package/lib/cjs/types/utils.d.ts.map +1 -1
- package/lib/cjs/utils.js +45 -7
- package/lib/esm/ClassPersistor.mjs +177 -0
- package/lib/esm/Esserializer/{DeserializationOptions.js → DeserializationOptions.mjs} +3 -3
- package/lib/esm/Esserializer/{SerializeOptions.js → SerializeOptions.mjs} +3 -3
- package/lib/esm/Esserializer/builtins.mjs +254 -0
- package/lib/esm/Esserializer/constant.mjs +28 -0
- package/lib/esm/Esserializer/deserializer.mjs +288 -0
- package/lib/esm/Esserializer/general.mjs +81 -0
- package/lib/esm/Esserializer/index.mjs +43 -0
- package/lib/esm/Esserializer/serializer.mjs +164 -0
- package/lib/esm/Evt.mjs +151 -0
- package/lib/esm/Hooks.mjs +1448 -0
- package/lib/esm/KissAction.mjs +4816 -0
- package/lib/esm/LoadPages.mjs +340 -0
- package/lib/esm/Persistor.mjs +180 -0
- package/lib/esm/ProcessPersistence.mjs +581 -0
- package/lib/esm/Store.mjs +4464 -0
- package/lib/esm/StoreException.mjs +153 -0
- package/lib/esm/StoreProvider.mjs +162 -0
- package/lib/esm/{UserException.js → UserException.mjs} +240 -246
- package/lib/esm/{Wait.js → Wait.mjs} +309 -309
- package/lib/esm/index.mjs +12 -9
- package/lib/esm/server.mjs +23 -0
- package/lib/esm/types/ClassPersistor.d.ts +143 -112
- package/lib/esm/types/ClassPersistor.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts +13 -6
- package/lib/esm/types/Esserializer/DeserializationOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts +11 -5
- package/lib/esm/types/Esserializer/SerializeOptions.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/builtins.d.ts +36 -0
- package/lib/esm/types/Esserializer/builtins.d.ts.map +1 -0
- package/lib/esm/types/Esserializer/constant.d.ts +25 -49
- package/lib/esm/types/Esserializer/constant.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/deserializer.d.ts +27 -17
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/general.d.ts +28 -4
- package/lib/esm/types/Esserializer/general.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/index.d.ts +33 -29
- package/lib/esm/types/Esserializer/index.d.ts.map +1 -1
- package/lib/esm/types/Esserializer/serializer.d.ts +14 -3
- package/lib/esm/types/Esserializer/serializer.d.ts.map +1 -1
- package/lib/esm/types/Evt.d.ts +124 -0
- package/lib/esm/types/Evt.d.ts.map +1 -0
- package/lib/esm/types/Hooks.d.ts +1174 -837
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +5257 -1398
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/LoadPages.d.ts +287 -0
- package/lib/esm/types/LoadPages.d.ts.map +1 -0
- package/lib/esm/types/Persistor.d.ts +187 -118
- package/lib/esm/types/Persistor.d.ts.map +1 -1
- package/lib/esm/types/ProcessPersistence.d.ts +197 -99
- package/lib/esm/types/ProcessPersistence.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +2266 -1042
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreException.d.ts +97 -28
- package/lib/esm/types/StoreException.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +95 -0
- package/lib/esm/types/StoreProvider.d.ts.map +1 -0
- package/lib/esm/types/UserException.d.ts +178 -169
- package/lib/esm/types/UserException.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +12 -9
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/server.d.ts +12 -0
- package/lib/esm/types/server.d.ts.map +1 -0
- package/lib/esm/types/utils.d.ts +15 -1
- package/lib/esm/types/utils.d.ts.map +1 -1
- package/lib/esm/utils.mjs +38 -0
- package/package.json +54 -30
- package/lib/cjs/UnmodifiableSetView.js +0 -53
- package/lib/cjs/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/cjs/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/ClassPersistor.js +0 -112
- package/lib/esm/Esserializer/constant.js +0 -81
- package/lib/esm/Esserializer/deserializer.js +0 -307
- package/lib/esm/Esserializer/general.js +0 -34
- package/lib/esm/Esserializer/index.js +0 -39
- package/lib/esm/Esserializer/serializer.js +0 -130
- package/lib/esm/Hooks.js +0 -955
- package/lib/esm/KissAction.js +0 -1459
- package/lib/esm/Persistor.js +0 -113
- package/lib/esm/ProcessPersistence.js +0 -262
- package/lib/esm/Store.js +0 -1606
- package/lib/esm/StoreException.js +0 -69
- package/lib/esm/UnmodifiableSetView.js +0 -50
- package/lib/esm/types/UnmodifiableSetView.d.ts +0 -24
- package/lib/esm/types/UnmodifiableSetView.d.ts.map +0 -1
- package/lib/esm/utils.js +0 -3
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
import { PersistAction } from "./Persistor.mjs";
|
|
2
|
+
import { UpdateStateAction } from "./KissAction.mjs";
|
|
3
|
+
import { Store } from "./Store.mjs";
|
|
4
|
+
import { StoreException } from "./StoreException.mjs";
|
|
5
|
+
import { capTimeoutMillis } from "./utils.mjs";
|
|
6
|
+
export class ProcessPersistence {
|
|
7
|
+
persistor;
|
|
8
|
+
lastPersistedState;
|
|
9
|
+
newestState;
|
|
10
|
+
isPersisting = false;
|
|
11
|
+
isANewStateAvailable = false;
|
|
12
|
+
lastPersistTime = new Date();
|
|
13
|
+
timer; // Timer's type
|
|
14
|
+
isPaused = false;
|
|
15
|
+
isInit = false;
|
|
16
|
+
// False while `readInitialState` is running. State changes are only persisted after it finishes.
|
|
17
|
+
isReady = false;
|
|
18
|
+
// True only while the store applies the state read from the persistor. That's the only
|
|
19
|
+
// dispatch allowed before the store is ready.
|
|
20
|
+
isApplyingReadState = false;
|
|
21
|
+
// Called by `readInitialState` right after `isReady` becomes true. The store uses it to
|
|
22
|
+
// publish the readiness to the `useIsStoreReady` hook.
|
|
23
|
+
onReady = null;
|
|
24
|
+
_ready;
|
|
25
|
+
_resolveReady;
|
|
26
|
+
// Called when the persistence process currently running finishes. See `_waitForPersisting`.
|
|
27
|
+
_persistingWaiters = [];
|
|
28
|
+
// True if a `PersistAction` was dispatched while a persistence operation was running.
|
|
29
|
+
// When it finishes, the state is persisted ignoring the throttle.
|
|
30
|
+
_isPersistActionWaiting = false;
|
|
31
|
+
// How many `logOut` calls are running. See `_startLogOut`.
|
|
32
|
+
_logOutsRunning = 0;
|
|
33
|
+
// Called when no `logOut` is running anymore. See `_waitForLogOut`.
|
|
34
|
+
_logOutWaiters = [];
|
|
35
|
+
/**
|
|
36
|
+
* Called with the persistence errors: the errors thrown by `Persistor.persistDifference`
|
|
37
|
+
* (after `Persistor.wrapError`, and only if it didn't return `null`), the errors thrown
|
|
38
|
+
* while reading the persisted state, and the errors added with `Persistor.addError`.
|
|
39
|
+
* The `originalError` is the error before `Persistor.wrapError` (or the same error, if it
|
|
40
|
+
* didn't go through `wrapError`). If not set, the errors are logged with `Store.log()`.
|
|
41
|
+
* The `observeOnly` is true when the error is also thrown to the caller (by `logOut`), so it
|
|
42
|
+
* should only be observed (given to the `errorObserver`), and not thrown again.
|
|
43
|
+
*/
|
|
44
|
+
onError = null;
|
|
45
|
+
constructor(persistor, lastPersistedState) {
|
|
46
|
+
this.persistor = persistor;
|
|
47
|
+
this.lastPersistedState = lastPersistedState;
|
|
48
|
+
this.newestState = null;
|
|
49
|
+
this._ready = new Promise(resolve => this._resolveReady = resolve);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolves when the persisted state has been read (or failed to read) at startup.
|
|
53
|
+
* Never rejects.
|
|
54
|
+
*/
|
|
55
|
+
ready() {
|
|
56
|
+
return this._ready;
|
|
57
|
+
}
|
|
58
|
+
async readInitialState(store, initialState) {
|
|
59
|
+
try {
|
|
60
|
+
await this._readInitialState(store, initialState);
|
|
61
|
+
}
|
|
62
|
+
//
|
|
63
|
+
finally {
|
|
64
|
+
// Now persist the state changes that happened while reading/saving the initial state.
|
|
65
|
+
this.processAddedErrors();
|
|
66
|
+
this.isReady = true;
|
|
67
|
+
// Tells the store right away, so that it publishes the readiness to `useIsStoreReady`.
|
|
68
|
+
// This runs after the read state was applied to the store (if there was one), so a
|
|
69
|
+
// component that sees the store as ready also sees the restored state.
|
|
70
|
+
this.onReady?.();
|
|
71
|
+
this._resolveReady();
|
|
72
|
+
if (this.isInit)
|
|
73
|
+
this.process(null, this.newestState);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async _readInitialState(store, initialState) {
|
|
77
|
+
let stateReadFromPersistor = null;
|
|
78
|
+
try {
|
|
79
|
+
stateReadFromPersistor = await this.persistor.readState();
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.log('Error reading state:' + error + '. State will reset.');
|
|
83
|
+
this.reportError(error);
|
|
84
|
+
try {
|
|
85
|
+
await this.persistor.deleteState();
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
this.log('Error deleting the state:' + error + '.');
|
|
89
|
+
this.reportError(error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (stateReadFromPersistor === null) {
|
|
93
|
+
try {
|
|
94
|
+
// If it was not possible to read the persisted state,
|
|
95
|
+
// we persist the initial-state passed to the Store constructor.
|
|
96
|
+
await this.persistor.saveInitialState(initialState);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
// Nothing was saved. Otherwise, the next save would diff against the initial-state,
|
|
100
|
+
// which was never saved. With null, the next save saves the whole state.
|
|
101
|
+
this.lastPersistedState = null;
|
|
102
|
+
this.log('Error saving initial state:' + error + '.');
|
|
103
|
+
this.reportError(error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//
|
|
107
|
+
else {
|
|
108
|
+
// If the saved state was read successfully, we replace the store state with it.
|
|
109
|
+
// In this case, the initial-state passed in the Store constructor was used
|
|
110
|
+
// only while the persisted state is loading.
|
|
111
|
+
// The slices of server state merged while the persisted state was being read (see
|
|
112
|
+
// `Store.mergeServerState`) are merged into it again, so that they are not lost.
|
|
113
|
+
const stateToApply = store._applyPendingServerSlices(stateReadFromPersistor);
|
|
114
|
+
let promise;
|
|
115
|
+
this.isApplyingReadState = true;
|
|
116
|
+
try {
|
|
117
|
+
promise = store.dispatchAndWait(new UpdateStateAction((_) => stateToApply, false));
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
this.isApplyingReadState = false;
|
|
121
|
+
}
|
|
122
|
+
await promise;
|
|
123
|
+
// The state just read is what's saved, even if the persistor is paused. Otherwise, the
|
|
124
|
+
// next save would diff against the initial-state, which was never saved.
|
|
125
|
+
this.lastPersistedState = stateReadFromPersistor;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
log(message) {
|
|
129
|
+
try {
|
|
130
|
+
Store.log(message);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Discard error.
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gives the error to `onError`, or logs it if `onError` is not set.
|
|
138
|
+
*/
|
|
139
|
+
reportError(error, originalError = error, observeOnly = false) {
|
|
140
|
+
if (this.onError) {
|
|
141
|
+
try {
|
|
142
|
+
this.onError(error, originalError, observeOnly);
|
|
143
|
+
}
|
|
144
|
+
catch (_error) {
|
|
145
|
+
this.log('Error processing a persistence error:' + _error + '.');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
//
|
|
149
|
+
else
|
|
150
|
+
this.log('Persistence error:' + error + '.');
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Reports the errors added with `Persistor.addError`, removing them from the persistor.
|
|
154
|
+
*/
|
|
155
|
+
processAddedErrors() {
|
|
156
|
+
while (true) {
|
|
157
|
+
let error;
|
|
158
|
+
try {
|
|
159
|
+
error = this.persistor.getAndRemoveFirstError?.();
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (error === null || error === undefined)
|
|
165
|
+
break;
|
|
166
|
+
this.reportError(error);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
get throttle() {
|
|
170
|
+
return this.persistor.throttle || 0;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Same as `Persistor.saveInitialState` but will remember the `initialState` as the
|
|
174
|
+
* `lastPersistedState`. If a save is running, waits for it to finish first, so that it can't
|
|
175
|
+
* overwrite the `initialState` later.
|
|
176
|
+
*/
|
|
177
|
+
async saveInitialState(initialState) {
|
|
178
|
+
return this._runAlone(async () => {
|
|
179
|
+
this.lastPersistedState = initialState;
|
|
180
|
+
try {
|
|
181
|
+
await this.persistor.saveInitialState(initialState);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
// Not saved, so it's not considered persisted (unless it was replaced in the meantime).
|
|
185
|
+
if (this.lastPersistedState === initialState)
|
|
186
|
+
this.lastPersistedState = null;
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Same as `Persistor.readState` but will remember the read state as the `lastPersistedState`.
|
|
193
|
+
* If a save is running, waits for it to finish first, so that it reads the saved state, and
|
|
194
|
+
* so that the save can't change the `lastPersistedState` later.
|
|
195
|
+
*/
|
|
196
|
+
async readState() {
|
|
197
|
+
return this._runAlone(async () => {
|
|
198
|
+
const state = await this.persistor.readState();
|
|
199
|
+
this.lastPersistedState = state;
|
|
200
|
+
return state;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Same as `Persistor.deleteState` but will clear the `lastPersistedState`.
|
|
205
|
+
* If a save is running, waits for it to finish first, so that it can't put the deleted
|
|
206
|
+
* state back later.
|
|
207
|
+
*/
|
|
208
|
+
async deleteState() {
|
|
209
|
+
return this._runAlone(async () => {
|
|
210
|
+
this.lastPersistedState = null;
|
|
211
|
+
await this.persistor.deleteState();
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Runs the `operation` when no other persistence operation is running, so that they don't
|
|
216
|
+
* overlap. While it runs, state changes are not saved. They are saved when it finishes, if
|
|
217
|
+
* necessary.
|
|
218
|
+
*/
|
|
219
|
+
async _runAlone(operation) {
|
|
220
|
+
while (this.isPersisting)
|
|
221
|
+
await this._waitForPersisting();
|
|
222
|
+
this._startOperation();
|
|
223
|
+
try {
|
|
224
|
+
return await operation();
|
|
225
|
+
}
|
|
226
|
+
finally {
|
|
227
|
+
this._finishOperation();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
_startOperation() {
|
|
231
|
+
this.isPersisting = true;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Called when a persistence operation finishes (even if it failed). If the state may have
|
|
235
|
+
* changed while it was running, persists it if necessary.
|
|
236
|
+
*/
|
|
237
|
+
_finishOperation() {
|
|
238
|
+
this.processAddedErrors();
|
|
239
|
+
this.isPersisting = false;
|
|
240
|
+
const waiters = this._persistingWaiters;
|
|
241
|
+
this._persistingWaiters = [];
|
|
242
|
+
for (const resolve of waiters)
|
|
243
|
+
resolve();
|
|
244
|
+
const newStateAvailable = this.isANewStateAvailable;
|
|
245
|
+
const persistActionWaiting = this._isPersistActionWaiting;
|
|
246
|
+
this.isANewStateAvailable = false;
|
|
247
|
+
this._isPersistActionWaiting = false;
|
|
248
|
+
// If a new state became available while the operation was running, save it if necessary.
|
|
249
|
+
// If a PersistAction was dispatched meanwhile, the throttle is ignored.
|
|
250
|
+
if (newStateAvailable)
|
|
251
|
+
this._process(null, this.newestState, persistActionWaiting);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Call `logOut()` when you want to delete the persisted state, and return the store
|
|
255
|
+
* state to the given initial-state. That's usually necessary when the user logs out
|
|
256
|
+
* of your app, or the user deletes its account, so that another user may log in,
|
|
257
|
+
* or start a new sign-up process.
|
|
258
|
+
*
|
|
259
|
+
* Note: If you know about any timers or async processes that you may have started,
|
|
260
|
+
* you should stop/cancel them all before calling this method.
|
|
261
|
+
*
|
|
262
|
+
* You may opt to:
|
|
263
|
+
*
|
|
264
|
+
* - Wait for `throttle` milliseconds to make sure all async processes that the app may
|
|
265
|
+
* have started have time to finish. The default `throttle` is 3000 milliseconds (3 seconds).
|
|
266
|
+
*
|
|
267
|
+
* - Wait for all actions currently running to finish, but wait at most `actionsThrottle`
|
|
268
|
+
* milliseconds. If the actions are not finished by then, the state will be deleted anyway.
|
|
269
|
+
* The default `actionsThrottle` is 6000 milliseconds (6 seconds).
|
|
270
|
+
*
|
|
271
|
+
* The result of the actions dispatched before logOut is discarded, and so are the actions
|
|
272
|
+
* they dispatch after it.
|
|
273
|
+
*
|
|
274
|
+
* Same as `store.logOut`. See it for more details.
|
|
275
|
+
*/
|
|
276
|
+
async logOut({ store, initialState, throttle = 3000, actionsThrottle = 6000, }) {
|
|
277
|
+
return store._logOut(this, initialState, throttle, actionsThrottle);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* For Kiss internal use only. Called by the store when `logOut` starts.
|
|
281
|
+
* While a logOut is running, the persistor doesn't start a new persistence process
|
|
282
|
+
* (except to persist the initial-state). This is kept apart from `pause`/`resume`, so that
|
|
283
|
+
* a persistor paused by the app, before or during logOut, stays paused after it.
|
|
284
|
+
* @internal
|
|
285
|
+
*/
|
|
286
|
+
_startLogOut() {
|
|
287
|
+
// Logging out while the persisted state is still being read is a developer error:
|
|
288
|
+
// the read would finish afterwards and bring back the old user's state.
|
|
289
|
+
if (!this.isReady)
|
|
290
|
+
throw new StoreException('Called logOut() while the persisted state is still being read. ' +
|
|
291
|
+
'Wait for store.ready() before calling logOut().');
|
|
292
|
+
this._logOutsRunning++;
|
|
293
|
+
this._cancelTimer();
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* For Kiss internal use only. Called by the store during `logOut`.
|
|
297
|
+
* If the state is currently being persisted, we can't delete it right now. Wait until the
|
|
298
|
+
* current persistence finishes. Since a logOut is running, no new persistence process will
|
|
299
|
+
* start after that.
|
|
300
|
+
* @internal
|
|
301
|
+
*/
|
|
302
|
+
async _waitWhilePersisting() {
|
|
303
|
+
while (this.isPersisting)
|
|
304
|
+
await this._waitForPersisting();
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* For Kiss internal use only. Called by the store during `logOut`, to delete the persisted
|
|
308
|
+
* state. If it fails, the error is reported (only observed, since logOut throws it), and
|
|
309
|
+
* returned. Returns `null` if it succeeded.
|
|
310
|
+
* @internal
|
|
311
|
+
*/
|
|
312
|
+
async _deleteLogOutState() {
|
|
313
|
+
// Another logOut that runs at the same time may be persisting its initial-state.
|
|
314
|
+
await this._waitWhilePersisting();
|
|
315
|
+
try {
|
|
316
|
+
await this.deleteState();
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
this.reportError(error, error, true);
|
|
321
|
+
return { error };
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* For Kiss internal use only. Called by the store during `logOut`, to persist the initial-state.
|
|
326
|
+
* Note this is done even if the persistor is paused. If it fails, the error (after
|
|
327
|
+
* `Persistor.wrapError`) is reported (only observed, since logOut throws it), and returned.
|
|
328
|
+
* Returns `null` if it succeeded, or if `wrapError` swallowed the error.
|
|
329
|
+
* @internal
|
|
330
|
+
*/
|
|
331
|
+
async _persistLogOutState(initialState) {
|
|
332
|
+
// Another logOut that runs at the same time may be persisting its initial-state. If both
|
|
333
|
+
// saves ran at the same time, the one that finishes last would win, and it could be the
|
|
334
|
+
// initial-state of the other logOut.
|
|
335
|
+
await this._waitWhilePersisting();
|
|
336
|
+
return this._persist(new Date(), initialState, true);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* For Kiss internal use only. Called by the store when `logOut` finishes (even if it failed).
|
|
340
|
+
* If no other logOut is running, and the persistor is not paused, persistence resumes: if the
|
|
341
|
+
* state changed after the initial-state was persisted, this persists the new state.
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
_endLogOut() {
|
|
345
|
+
this._logOutsRunning--;
|
|
346
|
+
if (this._logOutsRunning > 0)
|
|
347
|
+
return;
|
|
348
|
+
const waiters = this._logOutWaiters;
|
|
349
|
+
this._logOutWaiters = [];
|
|
350
|
+
for (const resolve of waiters)
|
|
351
|
+
resolve();
|
|
352
|
+
if (!this.isPaused && this.isInit)
|
|
353
|
+
this.process(null, this.newestState);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Resolves when no `logOut` is running anymore.
|
|
357
|
+
* Only call it while `_logOutsRunning` is more than zero.
|
|
358
|
+
*/
|
|
359
|
+
_waitForLogOut() {
|
|
360
|
+
return new Promise(resolve => this._logOutWaiters.push(resolve));
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* 1) If we're still persisting the last time, don't persist no matter what.
|
|
364
|
+
* Instead, check again when it finishes.
|
|
365
|
+
* 2) If action is of type `UpdateStateAction` with `ifPersists` false,
|
|
366
|
+
* don't persist, but consider the state as persisted (if no earlier change is waiting to
|
|
367
|
+
* be saved). This is done even while the persistor is paused.
|
|
368
|
+
* 3) If throttle period is done (or if action is PersistAction), persist.
|
|
369
|
+
* 4) If throttle period is NOT done, create a timer to persist as soon as it finishes.
|
|
370
|
+
*
|
|
371
|
+
* Return true if the persist process started.
|
|
372
|
+
* Return false if persistence was postponed.
|
|
373
|
+
*
|
|
374
|
+
*/
|
|
375
|
+
process(action, newState) {
|
|
376
|
+
return this._process(action, newState, false);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Same as `process`, but if `ignoreThrottle` is true, the throttle is ignored,
|
|
380
|
+
* as if the action were a `PersistAction`.
|
|
381
|
+
*/
|
|
382
|
+
_process(action, newState, ignoreThrottle) {
|
|
383
|
+
// True if an earlier state change was not saved yet. For example, because it's waiting for
|
|
384
|
+
// the throttle, for a running save to finish, or for the paused persistor to resume.
|
|
385
|
+
const hasUnsavedChange = this.isInit && (this.newestState !== this.lastPersistedState);
|
|
386
|
+
this.isInit = true;
|
|
387
|
+
this.newestState = newState;
|
|
388
|
+
const isNotPersisted = (action instanceof UpdateStateAction) && (!action.ifPersists);
|
|
389
|
+
// 1) While a persistence operation is running, remember to check again when it finishes.
|
|
390
|
+
// This is done even if the new state is the last persisted state, since the running
|
|
391
|
+
// operation will change the last persisted state when it finishes. But not for a state
|
|
392
|
+
// that should not be persisted.
|
|
393
|
+
if (this.isPersisting && !isNotPersisted) {
|
|
394
|
+
this.isANewStateAvailable = true;
|
|
395
|
+
if ((action instanceof PersistAction || ignoreThrottle) && !this.isPaused)
|
|
396
|
+
this._isPersistActionWaiting = true;
|
|
397
|
+
}
|
|
398
|
+
if (this.lastPersistedState === newState)
|
|
399
|
+
return false;
|
|
400
|
+
// This is checked even while the persistor is paused. Otherwise, when it resumes,
|
|
401
|
+
// it would see a difference, and save the state that should not be persisted.
|
|
402
|
+
if (isNotPersisted) {
|
|
403
|
+
// Only consider this state as persisted if no earlier change is waiting to be saved.
|
|
404
|
+
// Otherwise, those earlier changes would be lost. In that case, the pending save will
|
|
405
|
+
// save the newest state, which includes this one.
|
|
406
|
+
if (!hasUnsavedChange && !this.timer && !this.isPersisting && !this.isANewStateAvailable)
|
|
407
|
+
this.lastPersistedState = this.newestState;
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
if (this.isPaused)
|
|
411
|
+
return false;
|
|
412
|
+
// While a logOut is running, don't persist. When it finishes, it persists what's needed.
|
|
413
|
+
if (this._logOutsRunning > 0)
|
|
414
|
+
return false;
|
|
415
|
+
// While the initial state is being read/saved, don't persist. It will be persisted when it finishes.
|
|
416
|
+
if (!this.isReady)
|
|
417
|
+
return false;
|
|
418
|
+
// 1) If we're still persisting the last time, don't persist no matter what.
|
|
419
|
+
// It was already remembered above, to check again when it finishes.
|
|
420
|
+
if (this.isPersisting) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
//
|
|
424
|
+
else {
|
|
425
|
+
const now = new Date();
|
|
426
|
+
// 2) If throttle period is done (or if action is PersistAction), persist.
|
|
427
|
+
if (now.valueOf() - this.lastPersistTime.valueOf() >= this.throttle
|
|
428
|
+
|| (action instanceof PersistAction)
|
|
429
|
+
|| ignoreThrottle) {
|
|
430
|
+
this._cancelTimer();
|
|
431
|
+
this._persist(now, newState).then();
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
// 3) If throttle period is NOT done, create a timer to persist as soon as it finishes.
|
|
435
|
+
else {
|
|
436
|
+
if (!this.timer) {
|
|
437
|
+
const asSoonAsThrottleFinishes = this.throttle - (now.valueOf() - this.lastPersistTime.valueOf());
|
|
438
|
+
this.timer = setTimeout(() => {
|
|
439
|
+
this.timer = undefined;
|
|
440
|
+
this.process(null, this.newestState); // TypeScript forced cast
|
|
441
|
+
}, capTimeoutMillis(asSoonAsThrottleFinishes));
|
|
442
|
+
}
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
_cancelTimer() {
|
|
448
|
+
if (this.timer) {
|
|
449
|
+
clearTimeout(this.timer);
|
|
450
|
+
this.timer = undefined;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Resolves when the persistence process currently running finishes.
|
|
455
|
+
* Only call it while `isPersisting` is true.
|
|
456
|
+
*
|
|
457
|
+
* Note we use the `isPersisting` flag, and don't keep the promise of the running process.
|
|
458
|
+
* When `persistDifference` throws synchronously, instead of returning a rejected promise,
|
|
459
|
+
* `_persist` finishes before it even returns its promise. A kept promise would then never be
|
|
460
|
+
* cleared, and waiting for it in a loop would freeze the app.
|
|
461
|
+
*/
|
|
462
|
+
_waitForPersisting() {
|
|
463
|
+
return new Promise(resolve => this._persistingWaiters.push(resolve));
|
|
464
|
+
}
|
|
465
|
+
// Never rejects. A `persistDifference` that throws synchronously, or returns a rejected
|
|
466
|
+
// promise, is handled the same way: its error is reported, and the state is not saved.
|
|
467
|
+
/**
|
|
468
|
+
* Returns the error (after `Persistor.wrapError`) if the save failed, or `null` if it
|
|
469
|
+
* succeeded, or if `wrapError` swallowed the error. If `observeOnly` is true, the error is
|
|
470
|
+
* only observed, since the caller throws it.
|
|
471
|
+
*/
|
|
472
|
+
async _persist(now, newState, observeOnly = false) {
|
|
473
|
+
let failure = null;
|
|
474
|
+
this._startOperation();
|
|
475
|
+
this.lastPersistTime = now;
|
|
476
|
+
this.isANewStateAvailable = false;
|
|
477
|
+
try {
|
|
478
|
+
await this.persistor.persistDifference(this.lastPersistedState, newState);
|
|
479
|
+
// Only consider the state persisted if it succeeded. Otherwise, the next time we persist,
|
|
480
|
+
// the difference will be calculated from the last state that was actually persisted.
|
|
481
|
+
this.lastPersistedState = newState;
|
|
482
|
+
}
|
|
483
|
+
//
|
|
484
|
+
catch (error) {
|
|
485
|
+
// The error is reported, never thrown, so it can't become an unhandled rejection.
|
|
486
|
+
// Note the save is not retried here. It will be tried again when the state changes.
|
|
487
|
+
let processedError;
|
|
488
|
+
try {
|
|
489
|
+
processedError = this.persistor.wrapError ? this.persistor.wrapError(error) : error;
|
|
490
|
+
}
|
|
491
|
+
catch (_error) {
|
|
492
|
+
// If `wrapError` throws, the thrown error is used instead.
|
|
493
|
+
processedError = _error;
|
|
494
|
+
}
|
|
495
|
+
if (processedError !== null && processedError !== undefined) {
|
|
496
|
+
this.reportError(processedError, error, observeOnly);
|
|
497
|
+
failure = { error: processedError };
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
//
|
|
501
|
+
finally {
|
|
502
|
+
// If a new state became available while the present state was saving, save again.
|
|
503
|
+
this._finishOperation();
|
|
504
|
+
}
|
|
505
|
+
return failure;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Pause the `Persistor` temporarily.
|
|
509
|
+
*
|
|
510
|
+
* When `pause` is called, the Persistor will not start a new persistence process, until method
|
|
511
|
+
* `resume` is called. This will not affect the current persistence process, if one is currently
|
|
512
|
+
* running.
|
|
513
|
+
*
|
|
514
|
+
* Note: A persistence process starts when the `persistDifference` method is called, and
|
|
515
|
+
* finishes when the promise returned by that method completes.
|
|
516
|
+
*
|
|
517
|
+
*/
|
|
518
|
+
pause() {
|
|
519
|
+
this.isPaused = true;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* For Kiss internal use only. Called by `store.dispose()`. Pauses the persistor, and cancels
|
|
523
|
+
* the pending save (a state change waiting for the throttle). A save that is already running
|
|
524
|
+
* still finishes.
|
|
525
|
+
*/
|
|
526
|
+
dispose() {
|
|
527
|
+
this.isPaused = true;
|
|
528
|
+
this._cancelTimer();
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Persists the current state (if it's not yet persisted), then pauses the `Persistor`
|
|
532
|
+
* temporarily.
|
|
533
|
+
*
|
|
534
|
+
*
|
|
535
|
+
* When `persistAndPause` is called while the persisted state is still being read, while a
|
|
536
|
+
* `logOut` is running, or while a persistence process is running, it waits for them to
|
|
537
|
+
* finish. Note logOut persists its initial-state, and the state before it is never persisted. Then, if the current state is
|
|
538
|
+
* not yet persisted, it immediately starts a new persistence process (ignoring `throttle`).
|
|
539
|
+
* The returned promise completes when the current state is persisted.
|
|
540
|
+
*
|
|
541
|
+
* If the save fails, the returned promise still resolves. It never rejects. This is on
|
|
542
|
+
* purpose, not a bug: this method is usually called when the app is shutting down or going to
|
|
543
|
+
* the background, and then the app can't recover from a failed save anyway. The error is not
|
|
544
|
+
* lost: it's reported (see `onError`) before the promise resolves, so it can be logged.
|
|
545
|
+
*
|
|
546
|
+
* Then, the Persistor will not start another persistence process, until method `resume` is
|
|
547
|
+
* called.
|
|
548
|
+
*
|
|
549
|
+
* Note: A persistence process starts when the `persistDifference` method is called, and
|
|
550
|
+
* finishes when the promise returned by that method completes.
|
|
551
|
+
*
|
|
552
|
+
*/
|
|
553
|
+
async persistAndPause() {
|
|
554
|
+
this.isPaused = true;
|
|
555
|
+
this._cancelTimer();
|
|
556
|
+
// If the persisted state is still being read, wait until it finishes.
|
|
557
|
+
await this._ready;
|
|
558
|
+
// If a logOut is running, the current state belongs to the previous user, and must not be
|
|
559
|
+
// persisted. Wait until the logOut finishes, since it persists its initial-state (even
|
|
560
|
+
// though the persistor is paused). And if the state is being persisted, wait until it
|
|
561
|
+
// finishes. Since the persistor is paused, no new persistence process will start after that.
|
|
562
|
+
while (this._logOutsRunning > 0 || this.isPersisting) {
|
|
563
|
+
if (this._logOutsRunning > 0)
|
|
564
|
+
await this._waitForLogOut();
|
|
565
|
+
else
|
|
566
|
+
await this._waitForPersisting();
|
|
567
|
+
}
|
|
568
|
+
if (this.isInit && (this.lastPersistedState !== this.newestState)) {
|
|
569
|
+
const now = new Date();
|
|
570
|
+
await this._persist(now, this.newestState); // TypeScript forced cast
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Resumes persistence by the `Persistor`, after calling `pause` or `persistAndPause`.
|
|
575
|
+
*/
|
|
576
|
+
resume() {
|
|
577
|
+
this.isPaused = false;
|
|
578
|
+
if (this.isInit)
|
|
579
|
+
this.process(null, this.newestState);
|
|
580
|
+
}
|
|
581
|
+
}
|