kiss-for-react 2.0.0 → 2.0.2
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 +6 -13
- package/lib/cjs/Esserializer/deserializer.js +82 -15
- package/lib/cjs/Hooks.js +9 -7
- package/lib/cjs/KissAction.js +173 -46
- package/lib/cjs/Store.js +94 -30
- package/lib/cjs/StoreProvider.js +99 -21
- package/lib/cjs/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/cjs/types/Hooks.d.ts.map +1 -1
- package/lib/cjs/types/KissAction.d.ts +69 -11
- package/lib/cjs/types/KissAction.d.ts.map +1 -1
- package/lib/cjs/types/Store.d.ts +15 -2
- package/lib/cjs/types/Store.d.ts.map +1 -1
- package/lib/cjs/types/StoreProvider.d.ts +21 -6
- package/lib/cjs/types/StoreProvider.d.ts.map +1 -1
- package/lib/esm/Esserializer/deserializer.mjs +82 -15
- package/lib/esm/Hooks.mjs +9 -7
- package/lib/esm/KissAction.mjs +173 -46
- package/lib/esm/Store.mjs +94 -30
- package/lib/esm/StoreProvider.mjs +103 -21
- package/lib/esm/types/Esserializer/deserializer.d.ts.map +1 -1
- package/lib/esm/types/Hooks.d.ts.map +1 -1
- package/lib/esm/types/KissAction.d.ts +69 -11
- package/lib/esm/types/KissAction.d.ts.map +1 -1
- package/lib/esm/types/Store.d.ts +15 -2
- package/lib/esm/types/Store.d.ts.map +1 -1
- package/lib/esm/types/StoreProvider.d.ts +21 -6
- package/lib/esm/types/StoreProvider.d.ts.map +1 -1
- package/package.json +1 -1
package/lib/esm/KissAction.mjs
CHANGED
|
@@ -13,7 +13,12 @@ export var Poll;
|
|
|
13
13
|
* Otherwise, runs the action immediately and starts periodic polling.
|
|
14
14
|
*/
|
|
15
15
|
Poll["start"] = "start";
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Stop polling (cancels the timer and does not run the action).
|
|
18
|
+
* Also aborts the actions of the same polling key that were dispatched with `Poll.start` or
|
|
19
|
+
* `Poll.runNowAndRestart`, but had not started polling yet (for example, because they were
|
|
20
|
+
* still in an async `before()`), so that they don't start polling later.
|
|
21
|
+
*/
|
|
17
22
|
Poll["stop"] = "stop";
|
|
18
23
|
/**
|
|
19
24
|
* Run the action immediately and restart polling from now.
|
|
@@ -1729,7 +1734,10 @@ export class KissAction {
|
|
|
1729
1734
|
/**
|
|
1730
1735
|
* Stops all polling at once, for all polling keys, as if `Poll.stop` was dispatched for each
|
|
1731
1736
|
* of them. This is probably useful during logout or similar scenarios. Runs that are already
|
|
1732
|
-
* in progress still finish, but no new ticks are dispatched.
|
|
1737
|
+
* in progress still finish, but no new ticks are dispatched. The actions with `Poll.start` or
|
|
1738
|
+
* `Poll.runNowAndRestart` that were dispatched before, but had not started polling yet (for
|
|
1739
|
+
* example, because they were still in an async `before()`) are aborted, so that they don't
|
|
1740
|
+
* start polling later.
|
|
1733
1741
|
*/
|
|
1734
1742
|
stopAllPolling() {
|
|
1735
1743
|
this.store._stopAllPolling();
|
|
@@ -2980,6 +2988,14 @@ export class ActionStatus {
|
|
|
2980
2988
|
* doesn't fail, and doesn't show errors. It also doesn't roll back the optimistic value, and
|
|
2981
2989
|
* doesn't reload, since that would change the state of the next user.
|
|
2982
2990
|
*
|
|
2991
|
+
* ## Aborting
|
|
2992
|
+
*
|
|
2993
|
+
* When the command is aborted (by `abort()`, or by `store.abort(ActionType)`) while its server
|
|
2994
|
+
* call runs, its result is ignored. When the call finishes, the server response is not applied,
|
|
2995
|
+
* the optimistic value is not rolled back, nothing is reloaded, and the call is not retried. The
|
|
2996
|
+
* optimistic value stays in the state. The command finishes silently, with
|
|
2997
|
+
* `isDispatchAborted: true`.
|
|
2998
|
+
*
|
|
2983
2999
|
* ## CheckInternet
|
|
2984
3000
|
*
|
|
2985
3001
|
* When combined with `checkInternet`, if there is no internet: no optimistic state is
|
|
@@ -3178,6 +3194,10 @@ export class OptimisticCommand extends KissAction {
|
|
|
3178
3194
|
// the state of the next user. It's aborted, so it doesn't fail, and doesn't show errors.
|
|
3179
3195
|
if (this._hasStoppedRetrying)
|
|
3180
3196
|
throw error;
|
|
3197
|
+
// When the command was aborted, its result is ignored, so it doesn't roll back and
|
|
3198
|
+
// doesn't reload. The store finishes it silently.
|
|
3199
|
+
if (this.isAborted)
|
|
3200
|
+
throw error;
|
|
3181
3201
|
commandFailed = true;
|
|
3182
3202
|
commandError = error;
|
|
3183
3203
|
// Decides if it's safe to roll back (default: only if we are still seeing our own
|
|
@@ -3196,6 +3216,9 @@ export class OptimisticCommand extends KissAction {
|
|
|
3196
3216
|
}
|
|
3197
3217
|
}
|
|
3198
3218
|
}
|
|
3219
|
+
// When the command was aborted while its server call ran, its result is ignored, so it
|
|
3220
|
+
// doesn't apply the server response, and doesn't reload. The store finishes it silently.
|
|
3221
|
+
this.abortSignal.throwIfAborted();
|
|
3199
3222
|
// Applies the server response, if any. This is outside the `try` above, because the command
|
|
3200
3223
|
// succeeded: if applying the response throws, the action fails with that error, but there
|
|
3201
3224
|
// is no rollback and no reload.
|
|
@@ -3218,6 +3241,8 @@ export class OptimisticCommand extends KissAction {
|
|
|
3218
3241
|
});
|
|
3219
3242
|
if (doReload) {
|
|
3220
3243
|
const reloadResult = await this.reloadFromServer();
|
|
3244
|
+
// The command was aborted while reloading, so the reload result is ignored.
|
|
3245
|
+
this.abortSignal.throwIfAborted();
|
|
3221
3246
|
// Reads the current value again, because the state may have changed while reloading.
|
|
3222
3247
|
const apply = this.shouldApplyReload({
|
|
3223
3248
|
currentValue: this.getValueFromState(this.state),
|
|
@@ -3265,6 +3290,9 @@ export class OptimisticCommand extends KissAction {
|
|
|
3265
3290
|
// An aborted command is not retried.
|
|
3266
3291
|
if (error instanceof AbortDispatchException)
|
|
3267
3292
|
throw error;
|
|
3293
|
+
// A command aborted with `abort()` or `store.abort()` is not retried either.
|
|
3294
|
+
if (this.isAborted)
|
|
3295
|
+
throw error;
|
|
3268
3296
|
// When `dispatchSync` threw because the command is ASYNC, its result is ignored, so
|
|
3269
3297
|
// there's no point retrying.
|
|
3270
3298
|
if (this._isDiscardedByDispatchSync)
|
|
@@ -3280,9 +3308,12 @@ export class OptimisticCommand extends KissAction {
|
|
|
3280
3308
|
throw error;
|
|
3281
3309
|
try {
|
|
3282
3310
|
// It also stops waiting right away when `clearInternalActionProps()` is called.
|
|
3311
|
+
// And when the command is aborted, in which case it keeps the abort error.
|
|
3283
3312
|
await this.store._waitRetryDelay(this, this._nextRetryDelay());
|
|
3284
3313
|
}
|
|
3285
|
-
catch {
|
|
3314
|
+
catch (waitError) {
|
|
3315
|
+
if (this.isAborted && !this.store._wereInternalActionPropsCleared(this))
|
|
3316
|
+
throw waitError;
|
|
3286
3317
|
throw this._stopRetrying();
|
|
3287
3318
|
}
|
|
3288
3319
|
}
|
|
@@ -3555,6 +3586,10 @@ export class OptimisticCommand extends KissAction {
|
|
|
3555
3586
|
* `true` meanwhile. The dispatches made while the key is taken apply their optimistic value,
|
|
3556
3587
|
* and then finish right away.
|
|
3557
3588
|
*
|
|
3589
|
+
* This includes a dispatch made while the server response is applied (for example, by
|
|
3590
|
+
* `store.dispatchWhen`, when the state with the response meets its condition). If that dispatch
|
|
3591
|
+
* changed the value, the dispatch that took the key sends one more request, with the new value.
|
|
3592
|
+
*
|
|
3558
3593
|
* ## Clearing
|
|
3559
3594
|
*
|
|
3560
3595
|
* `store.clearInternalActionProps()` (which is also called by `store.setShutDown(true)`)
|
|
@@ -3564,6 +3599,16 @@ export class OptimisticCommand extends KissAction {
|
|
|
3564
3599
|
* doesn't apply the server response, and doesn't call `onFinish`. It's aborted with an
|
|
3565
3600
|
* `AbortDispatchException`, so it doesn't fail, and doesn't show errors.
|
|
3566
3601
|
*
|
|
3602
|
+
* ## Aborting
|
|
3603
|
+
*
|
|
3604
|
+
* When the dispatch that sends the requests is aborted (by `abort()`, or by
|
|
3605
|
+
* `store.abort(ActionType)`) while a request is in flight, it stops when that request finishes,
|
|
3606
|
+
* or when the request rejects because of the `abortSignal`. It releases the key, doesn't send
|
|
3607
|
+
* follow-up requests, doesn't apply the server response, and doesn't call `onFinish`. It
|
|
3608
|
+
* finishes silently, with `isDispatchAborted: true`. The value in the state is not changed, so
|
|
3609
|
+
* a local change made while the request was in flight may not reach the server. The next
|
|
3610
|
+
* dispatch of the key takes the key, and sends its own value.
|
|
3611
|
+
*
|
|
3567
3612
|
* Notes:
|
|
3568
3613
|
* - It can be combined with `checkInternet`, both `{ dialog: true | false }` and
|
|
3569
3614
|
* `{ abort: true }`. If there is no internet, the optimistic value is not applied, and no
|
|
@@ -3664,7 +3709,8 @@ export class OptimisticSync extends KissAction {
|
|
|
3664
3709
|
*
|
|
3665
3710
|
* Note `onFinish` is called in both success and failure scenarios. On success, it runs only
|
|
3666
3711
|
* after the state is stable for this key. On failure, it runs immediately after the request
|
|
3667
|
-
* fails (there is no further stabilization or follow-up).
|
|
3712
|
+
* fails (there is no further stabilization or follow-up). It's not called when the action is
|
|
3713
|
+
* aborted, or when `store.clearInternalActionProps()` released the key.
|
|
3668
3714
|
*
|
|
3669
3715
|
* Important: The key is released *before* `onFinish` runs. This means new dispatches for the
|
|
3670
3716
|
* same key may start a new request while `onFinish` is still running.
|
|
@@ -3747,6 +3793,10 @@ export class OptimisticSync extends KissAction {
|
|
|
3747
3793
|
* If they are different, it means the state was changed after we sent the request, so we
|
|
3748
3794
|
* should send another request with the new value.
|
|
3749
3795
|
*
|
|
3796
|
+
* It's also called after the server response is applied, if applying it caused another
|
|
3797
|
+
* change of the state (for example, a save dispatched by `store.dispatchWhen`). Then, the
|
|
3798
|
+
* `sentValue` is the value that the server response put in the state.
|
|
3799
|
+
*
|
|
3750
3800
|
* Values are compared with `Object.is` (which is the same as `===`, except that `NaN` is equal
|
|
3751
3801
|
* to `NaN`). So, make sure `getValueFromState` returns the same object you applied, or
|
|
3752
3802
|
* override this method if you need custom equality logic.
|
|
@@ -3793,27 +3843,47 @@ export class OptimisticSync extends KissAction {
|
|
|
3793
3843
|
// in flight (for example, on logout). So, stop here.
|
|
3794
3844
|
if (!this.store._hasOptimisticSyncKey(this))
|
|
3795
3845
|
throw this._clearedError();
|
|
3796
|
-
|
|
3846
|
+
// The action was aborted while the request was in flight, so its result is ignored: it
|
|
3847
|
+
// doesn't apply the server response, and doesn't send follow-up requests.
|
|
3848
|
+
this.abortSignal.throwIfAborted();
|
|
3849
|
+
let stateValue = this.getValueFromState(this.state);
|
|
3797
3850
|
// If the state changed while the request was in flight, sends a follow-up request with
|
|
3798
3851
|
// the current value, without applying the server response, since the state isn't stable.
|
|
3799
|
-
|
|
3800
|
-
if ((this.maxFollowUpRequests !== -1) && (requestCount > this.maxFollowUpRequests))
|
|
3801
|
-
throw new StoreException(`Too many follow-up requests in action ${this.constructor.name} (> ${this.maxFollowUpRequests}).`);
|
|
3802
|
-
sentValue = stateValue;
|
|
3803
|
-
continue;
|
|
3804
|
-
}
|
|
3852
|
+
let sendAnother = this.ifShouldSendAnotherRequest({ stateValue, sentValue, requestCount });
|
|
3805
3853
|
// The state is stable for this key, so we apply the server response, if any.
|
|
3806
|
-
if (serverResponse !== null && serverResponse !== undefined) {
|
|
3854
|
+
if (!sendAnother && serverResponse !== null && serverResponse !== undefined) {
|
|
3807
3855
|
const newState = this.applyServerResponseToState(this.state, serverResponse);
|
|
3808
|
-
if (newState !== null)
|
|
3856
|
+
if (newState !== null) {
|
|
3809
3857
|
this._applyState(newState);
|
|
3858
|
+
// Applying the response may have changed the state again (for example, by a save of
|
|
3859
|
+
// this key dispatched by `store.dispatchWhen`). That save didn't send its value,
|
|
3860
|
+
// since this dispatch holds the key. So, if the value changed, sends a follow-up.
|
|
3861
|
+
if (this.state !== newState) {
|
|
3862
|
+
stateValue = this.getValueFromState(this.state);
|
|
3863
|
+
sendAnother = this.ifShouldSendAnotherRequest({
|
|
3864
|
+
stateValue,
|
|
3865
|
+
sentValue: this.getValueFromState(newState),
|
|
3866
|
+
requestCount,
|
|
3867
|
+
});
|
|
3868
|
+
}
|
|
3869
|
+
}
|
|
3810
3870
|
}
|
|
3811
|
-
|
|
3871
|
+
if (!sendAnother)
|
|
3872
|
+
break;
|
|
3873
|
+
if ((this.maxFollowUpRequests !== -1) && (requestCount > this.maxFollowUpRequests))
|
|
3874
|
+
throw new StoreException(`Too many follow-up requests in action ${this.constructor.name} (> ${this.maxFollowUpRequests}).`);
|
|
3875
|
+
sentValue = stateValue;
|
|
3812
3876
|
}
|
|
3813
3877
|
}
|
|
3814
3878
|
catch (error) {
|
|
3815
3879
|
if (!this.store._hasOptimisticSyncKey(this))
|
|
3816
3880
|
throw this._clearedError();
|
|
3881
|
+
// An aborted action releases the key, but doesn't call `onFinish`, since its result is
|
|
3882
|
+
// ignored. The store finishes it silently.
|
|
3883
|
+
if (this.isAborted) {
|
|
3884
|
+
this.store._releaseOptimisticSyncKey(this);
|
|
3885
|
+
throw error;
|
|
3886
|
+
}
|
|
3817
3887
|
finishError = error;
|
|
3818
3888
|
}
|
|
3819
3889
|
// Releases the key before `onFinish`, so that new dispatches can send requests.
|
|
@@ -4044,6 +4114,13 @@ let _deviceId = undefined;
|
|
|
4044
4114
|
* `onFinish`. It's aborted with an `AbortDispatchException`, so it doesn't fail, and doesn't
|
|
4045
4115
|
* show errors.
|
|
4046
4116
|
*
|
|
4117
|
+
* ## Aborting
|
|
4118
|
+
*
|
|
4119
|
+
* As in `OptimisticSync`, when the dispatch that sends the requests is aborted (by `abort()`, or
|
|
4120
|
+
* by `store.abort(ActionType)`) while a request is in flight, it stops when that request
|
|
4121
|
+
* finishes. It releases the key, doesn't send follow-up requests, doesn't apply the server
|
|
4122
|
+
* response, and doesn't call `onFinish`. It finishes silently, with `isDispatchAborted: true`.
|
|
4123
|
+
*
|
|
4047
4124
|
* Notes:
|
|
4048
4125
|
* - It can be combined with `checkInternet`, both `{ dialog: true | false }` and
|
|
4049
4126
|
* `{ abort: true }`. If there is no internet, the optimistic value is not applied, and no
|
|
@@ -4097,9 +4174,6 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4097
4174
|
// The server revision informed by `informServerRevision()` during the current request, or
|
|
4098
4175
|
// `null` if it was not informed. It's reset before each request.
|
|
4099
4176
|
_informedServerRevision = null;
|
|
4100
|
-
// True if `informServerRevision()`, during the current request, found that the push applied
|
|
4101
|
-
// while the request was in flight is older than the request. It's reset before each request.
|
|
4102
|
-
_wasPushStale = false;
|
|
4103
4177
|
/**
|
|
4104
4178
|
* Optionally, override `optimisticSyncKeyParams()` to differentiate the coalescing by the
|
|
4105
4179
|
* action parameters. For example, if you have a like button per item, return the item ID, so
|
|
@@ -4216,9 +4290,12 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4216
4290
|
// longer from a push. If a push was applied while the request was in flight, the server
|
|
4217
4291
|
// applied it before this request, so the pushed value is stale.
|
|
4218
4292
|
if (value > known.serverRevision) {
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4293
|
+
this.store._setOptimisticSyncRevision(this._key, {
|
|
4294
|
+
...known,
|
|
4295
|
+
serverRevision: value,
|
|
4296
|
+
isPush: false,
|
|
4297
|
+
hasStalePushValue: known.isPush || known.hasStalePushValue,
|
|
4298
|
+
});
|
|
4222
4299
|
}
|
|
4223
4300
|
}
|
|
4224
4301
|
/**
|
|
@@ -4228,7 +4305,8 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4228
4305
|
*
|
|
4229
4306
|
* Note `onFinish` is called in both success and failure scenarios. On success, it runs only
|
|
4230
4307
|
* after the state is stable for this key. On failure, it runs immediately after the request
|
|
4231
|
-
* fails (there is no further stabilization or follow-up).
|
|
4308
|
+
* fails (there is no further stabilization or follow-up). It's not called when the action is
|
|
4309
|
+
* aborted, or when `store.clearInternalActionProps()` released the key.
|
|
4232
4310
|
*
|
|
4233
4311
|
* Important: The key is released *before* `onFinish` runs. This means new dispatches for the
|
|
4234
4312
|
* same key may start a new request while `onFinish` is still running.
|
|
@@ -4280,6 +4358,7 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4280
4358
|
localRevision,
|
|
4281
4359
|
serverRevision: known.serverRevision,
|
|
4282
4360
|
isPush: false,
|
|
4361
|
+
hasStalePushValue: false,
|
|
4283
4362
|
localValue: this.getValueFromState(newState),
|
|
4284
4363
|
});
|
|
4285
4364
|
// Always applies the optimistic update immediately.
|
|
@@ -4307,7 +4386,6 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4307
4386
|
this.lastSentValue = sentValue;
|
|
4308
4387
|
// Reset before each request, to detect if `informServerRevision()` was called.
|
|
4309
4388
|
this._informedServerRevision = null;
|
|
4310
|
-
this._wasPushStale = false;
|
|
4311
4389
|
// Sends the value, and gets the server response (may be null or undefined).
|
|
4312
4390
|
const serverResponse = await this.sendValueToServer(sentValue, sentLocalRevision, OptimisticSyncWithPush.deviceId());
|
|
4313
4391
|
// The keys were released by `store.clearInternalActionProps()` while the request was
|
|
@@ -4319,39 +4397,54 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4319
4397
|
throw new StoreException(`Action ${this.constructor.name} is an OptimisticSyncWithPush, which requires calling ` +
|
|
4320
4398
|
'`informServerRevision()` inside `sendValueToServer()`. ' +
|
|
4321
4399
|
'If you don\'t need server-push handling, use `OptimisticSync` instead.');
|
|
4400
|
+
// The action was aborted while the request was in flight, so its result is ignored: it
|
|
4401
|
+
// doesn't apply the server response, and doesn't send follow-up requests.
|
|
4402
|
+
this.abortSignal.throwIfAborted();
|
|
4403
|
+
// A push applied while a request was in flight turned out to be older than that request
|
|
4404
|
+
// (because of `informServerRevision()`, or of the echo of that request). So, the pushed
|
|
4405
|
+
// value in the state is stale, and the latest local value is put back in the state. It's
|
|
4406
|
+
// then sent by the follow-up request, if any, and otherwise it's the value the server has.
|
|
4322
4407
|
const known = this._revisionOf(this._key);
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
!Object.is(this.getValueFromState(this.state), known.localValue))
|
|
4329
|
-
this._applyState(this.applyOptimisticValueToState(this.state, known.localValue));
|
|
4408
|
+
if (known.hasStalePushValue) {
|
|
4409
|
+
this.store._setOptimisticSyncRevision(this._key, { ...known, hasStalePushValue: false });
|
|
4410
|
+
if (!Object.is(this.getValueFromState(this.state), known.localValue))
|
|
4411
|
+
this._applyState(this.applyOptimisticValueToState(this.state, known.localValue));
|
|
4412
|
+
}
|
|
4330
4413
|
// If the latest change was made locally (not by a push), and the local-revision advanced
|
|
4331
4414
|
// since this request was sent, the user changed the value while the request was in
|
|
4332
4415
|
// flight. So, sends a follow-up request with the current value, without applying the
|
|
4333
4416
|
// server response, since the state isn't stable.
|
|
4334
|
-
|
|
4335
|
-
if ((this.maxFollowUpRequests !== -1) && (requestCount > this.maxFollowUpRequests))
|
|
4336
|
-
throw new StoreException(`Too many follow-up requests in action ${this.constructor.name} (> ${this.maxFollowUpRequests}).`);
|
|
4337
|
-
sentValue = this.getValueFromState(this.state);
|
|
4338
|
-
sentLocalRevision = known.localRevision;
|
|
4339
|
-
continue;
|
|
4340
|
-
}
|
|
4417
|
+
let sendAnother = this._hasLocalChangeAfter(sentLocalRevision);
|
|
4341
4418
|
// The state is stable for this key, so we apply the server response, if any, but only
|
|
4342
4419
|
// if it's not stale (no newer server revision is known, for example from a push).
|
|
4343
|
-
if (serverResponse !== null && serverResponse !== undefined &&
|
|
4344
|
-
informedServerRevision >=
|
|
4420
|
+
if (!sendAnother && serverResponse !== null && serverResponse !== undefined &&
|
|
4421
|
+
informedServerRevision >= this._revisionOf(this._key).serverRevision) {
|
|
4345
4422
|
const newState = this.applyServerResponseToState(this.state, serverResponse);
|
|
4346
|
-
if (newState !== null)
|
|
4423
|
+
if (newState !== null) {
|
|
4347
4424
|
this._applyState(newState);
|
|
4425
|
+
// Applying the response may have dispatched a save of this key (for example, by
|
|
4426
|
+
// `store.dispatchWhen`). That save didn't send its value, since this dispatch holds
|
|
4427
|
+
// the key. So, sends it in a follow-up request.
|
|
4428
|
+
sendAnother = this._hasLocalChangeAfter(sentLocalRevision);
|
|
4429
|
+
}
|
|
4348
4430
|
}
|
|
4349
|
-
|
|
4431
|
+
if (!sendAnother)
|
|
4432
|
+
break;
|
|
4433
|
+
if ((this.maxFollowUpRequests !== -1) && (requestCount > this.maxFollowUpRequests))
|
|
4434
|
+
throw new StoreException(`Too many follow-up requests in action ${this.constructor.name} (> ${this.maxFollowUpRequests}).`);
|
|
4435
|
+
sentValue = this.getValueFromState(this.state);
|
|
4436
|
+
sentLocalRevision = this._revisionOf(this._key).localRevision;
|
|
4350
4437
|
}
|
|
4351
4438
|
}
|
|
4352
4439
|
catch (error) {
|
|
4353
4440
|
if (!this.store._hasOptimisticSyncKey(this))
|
|
4354
4441
|
throw this._clearedError();
|
|
4442
|
+
// An aborted action releases the key, but doesn't call `onFinish`, since its result is
|
|
4443
|
+
// ignored. The store finishes it silently.
|
|
4444
|
+
if (this.isAborted) {
|
|
4445
|
+
this.store._releaseOptimisticSyncKey(this);
|
|
4446
|
+
throw error;
|
|
4447
|
+
}
|
|
4355
4448
|
finishError = error;
|
|
4356
4449
|
}
|
|
4357
4450
|
// Releases the key before `onFinish`, so that new dispatches can send requests.
|
|
@@ -4370,6 +4463,14 @@ export class OptimisticSyncWithPush extends KissAction {
|
|
|
4370
4463
|
_revisionOf(key) {
|
|
4371
4464
|
return _revisionOf(this.store, key, this.getServerRevisionFromState(this.state, key));
|
|
4372
4465
|
}
|
|
4466
|
+
/**
|
|
4467
|
+
* True if the latest change of the key of this dispatch was made locally (not by a push), and
|
|
4468
|
+
* is newer than the given local-revision.
|
|
4469
|
+
*/
|
|
4470
|
+
_hasLocalChangeAfter(localRevision) {
|
|
4471
|
+
const known = this._revisionOf(this._key);
|
|
4472
|
+
return !known.isPush && (known.localRevision > localRevision);
|
|
4473
|
+
}
|
|
4373
4474
|
_clearedError() {
|
|
4374
4475
|
return kissAbort('The internal action props were cleared, so the action stopped syncing.');
|
|
4375
4476
|
}
|
|
@@ -4395,6 +4496,7 @@ function _revisionOf(store, key, serverRevisionFromState) {
|
|
|
4395
4496
|
localRevision: kept?.localRevision ?? 0,
|
|
4396
4497
|
serverRevision: Math.max(kept?.serverRevision ?? -1, serverRevisionFromState),
|
|
4397
4498
|
isPush: kept?.isPush ?? false,
|
|
4499
|
+
hasStalePushValue: kept?.hasStalePushValue ?? false,
|
|
4398
4500
|
localValue: kept?.localValue,
|
|
4399
4501
|
};
|
|
4400
4502
|
}
|
|
@@ -4413,12 +4515,18 @@ function _revisionOf(store, key, serverRevisionFromState) {
|
|
|
4413
4515
|
*
|
|
4414
4516
|
* 1. If the push is not newer than the newest known server revision for the key (considering
|
|
4415
4517
|
* both the revision kept by Kiss, and `getServerRevisionFromState()`), it's stale or out of
|
|
4416
|
-
* order, so it's ignored.
|
|
4518
|
+
* order, so it's ignored. There is one exception: the echo of the latest request of this
|
|
4519
|
+
* device, when the server response already informed its server revision (with
|
|
4520
|
+
* `informServerRevision`), but the state doesn't have that revision yet. It's applied as in
|
|
4521
|
+
* step 3, so that `applyServerPushToState` saves the server revision in the state.
|
|
4417
4522
|
*
|
|
4418
4523
|
* 2. If the push is the echo of an older request of this same device (the user changed the
|
|
4419
4524
|
* value again after that request was sent), it's not applied, since the state already has a
|
|
4420
4525
|
* newer local value. Its server revision is still recorded, but it doesn't count as a push,
|
|
4421
|
-
* so the newer local value is still sent in a follow-up request.
|
|
4526
|
+
* so the newer local value is still sent in a follow-up request. If a push from another
|
|
4527
|
+
* device was applied before this echo, that push is older than the request, so its value is
|
|
4528
|
+
* stale: the request in flight puts the latest local value back in the state when it
|
|
4529
|
+
* finishes, and sends it in the follow-up request.
|
|
4422
4530
|
*
|
|
4423
4531
|
* 3. Otherwise (a push from another device, or the echo of the latest request of this device),
|
|
4424
4532
|
* it's applied with `applyServerPushToState`, and recorded as the latest change of the key.
|
|
@@ -4512,16 +4620,33 @@ export class ServerPush extends KissAction {
|
|
|
4512
4620
|
if (typeof serverRevision !== 'number' || !Number.isFinite(serverRevision))
|
|
4513
4621
|
throw new StoreException(`Action ${this.constructor.name} pushed an invalid server revision: ` +
|
|
4514
4622
|
`it must be a finite number, but got ${String(serverRevision)}.`);
|
|
4515
|
-
const
|
|
4623
|
+
const serverRevisionFromState = this.getServerRevisionFromState(this.state, key);
|
|
4624
|
+
const known = _revisionOf(this.store, key, serverRevisionFromState);
|
|
4625
|
+
const isSelf = (deviceId === OptimisticSyncWithPush.deviceId());
|
|
4626
|
+
// The echo of the latest request of this device, whose server revision is already known,
|
|
4627
|
+
// because the server response came first, and informed it with `informServerRevision`. But
|
|
4628
|
+
// that revision is not in the state yet. So, the echo is applied, which lets
|
|
4629
|
+
// `applyServerPushToState` save the revision in the state. Otherwise, a stale push could
|
|
4630
|
+
// overwrite the value after the app restarts with the persisted state.
|
|
4631
|
+
const isEchoOfInformedRevision = isSelf &&
|
|
4632
|
+
(serverRevision === known.serverRevision) &&
|
|
4633
|
+
(serverRevisionFromState < serverRevision) &&
|
|
4634
|
+
(localRevision >= known.localRevision);
|
|
4516
4635
|
// Ignores stale and out-of-order pushes.
|
|
4517
|
-
if (serverRevision <= known.serverRevision)
|
|
4636
|
+
if (serverRevision <= known.serverRevision && !isEchoOfInformedRevision)
|
|
4518
4637
|
return null;
|
|
4519
|
-
const isSelf = (deviceId === OptimisticSyncWithPush.deviceId());
|
|
4520
4638
|
// The echo of an older request of this device: the state already has a newer local value.
|
|
4521
4639
|
// So, it only records the server revision. It does NOT apply the push, and does NOT count
|
|
4522
4640
|
// as a push, since then the newer local value would not be sent in a follow-up request.
|
|
4641
|
+
// If the value in the state came from a push, that push is older than this echo, so the
|
|
4642
|
+
// pushed value is stale. Then, the request in flight puts the latest local value back.
|
|
4523
4643
|
if (isSelf && (localRevision < known.localRevision)) {
|
|
4524
|
-
this.store._setOptimisticSyncRevision(key, {
|
|
4644
|
+
this.store._setOptimisticSyncRevision(key, {
|
|
4645
|
+
...known,
|
|
4646
|
+
serverRevision,
|
|
4647
|
+
isPush: false,
|
|
4648
|
+
hasStalePushValue: known.isPush || known.hasStalePushValue,
|
|
4649
|
+
});
|
|
4525
4650
|
return null;
|
|
4526
4651
|
}
|
|
4527
4652
|
// Safe to apply (a push from another device, or the echo of the latest local value).
|
|
@@ -4535,6 +4660,8 @@ export class ServerPush extends KissAction {
|
|
|
4535
4660
|
localRevision: isSelf ? Math.max(known.localRevision, localRevision) : known.localRevision,
|
|
4536
4661
|
serverRevision,
|
|
4537
4662
|
isPush: (newState === null) ? known.isPush : true,
|
|
4663
|
+
// An applied push replaces the stale pushed value in the state, if any.
|
|
4664
|
+
hasStalePushValue: (newState === null) ? known.hasStalePushValue : false,
|
|
4538
4665
|
});
|
|
4539
4666
|
return newState;
|
|
4540
4667
|
}
|