kiss-for-react 2.0.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/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/Store.mjs
CHANGED
|
@@ -85,11 +85,11 @@ class NotificationBatch {
|
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
87
|
* Like `run`, but instead of publishing the notifications when `fn` finishes, returns a
|
|
88
|
-
* function that publishes them, for the caller to call when it's safe. Used by `StoreSlice
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* another batch, the notifications are left to that batch, and the returned
|
|
92
|
-
* nothing.
|
|
88
|
+
* function that publishes them, for the caller to call when it's safe. Used by `StoreSlice`
|
|
89
|
+
* on the server, which merges the slice into the request's store while React renders it
|
|
90
|
+
* (where a notification would make React warn about updating other components during a
|
|
91
|
+
* render). Inside another batch, the notifications are left to that batch, and the returned
|
|
92
|
+
* function does nothing.
|
|
93
93
|
*/
|
|
94
94
|
suspend(fn) {
|
|
95
95
|
this.depth++;
|
|
@@ -605,7 +605,7 @@ export class Store {
|
|
|
605
605
|
this._removeAllFreshKeys();
|
|
606
606
|
this._removeAllThrottleLocks();
|
|
607
607
|
this._removeAllDebounceLocks();
|
|
608
|
-
this._stopAllPolling();
|
|
608
|
+
this._stopAllPolling('The internal action props were cleared, so the action did not start polling.');
|
|
609
609
|
this._discardAllSequentialQueues();
|
|
610
610
|
this._removeAllOptimisticSyncKeys();
|
|
611
611
|
this._clearCount++;
|
|
@@ -755,6 +755,13 @@ export class Store {
|
|
|
755
755
|
// tell they must not continue. While a tick is running (when `pollWaitsForRun` is true) the
|
|
756
756
|
// cycle has no timer, but polling is still active.
|
|
757
757
|
_pollingCycles = [];
|
|
758
|
+
// The actions with `Poll.start` or `Poll.runNowAndRestart` that were dispatched, but have not
|
|
759
|
+
// started polling yet (because they are still in an async `before()`, for example). If their
|
|
760
|
+
// polling key is stopped (`Poll.stop`), or all polling is stopped (`stopAllPolling()`,
|
|
761
|
+
// `clearInternalActionProps()`) before they start, they are aborted, so that they don't start
|
|
762
|
+
// polling later. An action leaves the set when it reaches the polling reducer, or when it's
|
|
763
|
+
// aborted. Actions that finish before that are collected, since this is a weak set.
|
|
764
|
+
_pendingPollingStarts = new WeakSet();
|
|
758
765
|
// The errors thrown by `dispatchSync` when the action is ASYNC. See `_notSyncError`.
|
|
759
766
|
_notSyncErrors = new WeakSet();
|
|
760
767
|
/**
|
|
@@ -769,6 +776,11 @@ export class Store {
|
|
|
769
776
|
// the ones that have one.
|
|
770
777
|
_props = new Map();
|
|
771
778
|
_propDisposers = new Map();
|
|
779
|
+
// The async disposals of props that are still running: the ones started by `disposeProp`,
|
|
780
|
+
// `disposeProps`, `logOut`, a `setProp` that replaced a value, and `dispose()` itself.
|
|
781
|
+
// `dispose()` waits for all of them, so that the dependencies and the configuration are
|
|
782
|
+
// closed only after the resources that may use them. Each one removes itself when it settles.
|
|
783
|
+
_pendingDisposals = new Set();
|
|
772
784
|
// True after `dispose()` was called. A disposed store doesn't accept actions or props.
|
|
773
785
|
_disposed = false;
|
|
774
786
|
// The environment, the configuration and the dependencies (see `ConstructorParams`). The
|
|
@@ -1557,6 +1569,8 @@ export class Store {
|
|
|
1557
1569
|
Store._logLazy(() => `${this._dispatchCount}) ${action}`);
|
|
1558
1570
|
action._changeStatus({ isDispatched: true });
|
|
1559
1571
|
action._clearCountAtDispatch = this._clearCount;
|
|
1572
|
+
if (this._startsPolling(action))
|
|
1573
|
+
this._pendingPollingStarts.add(action);
|
|
1560
1574
|
// Note the store was already injected into the action by `_mustAbortDispatch`.
|
|
1561
1575
|
// The action is dispatched twice. This is the 1st: when the action starts (ini true).
|
|
1562
1576
|
this._observeAction(action, true);
|
|
@@ -2052,7 +2066,10 @@ export class Store {
|
|
|
2052
2066
|
async function _wrapReduceDebounceAsync() {
|
|
2053
2067
|
// In a server store, the debounce period is not waited: there is no user input to
|
|
2054
2068
|
// merge on the server. The action is still ASYNC, as any debounced action.
|
|
2055
|
-
const isLast = store._isServer || await store._waitDebounce(action['debounceLockBuilder'](), debounceMillis);
|
|
2069
|
+
const isLast = store._isServer || await store._waitDebounce(action['debounceLockBuilder'](), debounceMillis, action.abortSignal);
|
|
2070
|
+
// An action aborted during the debounce period doesn't run its reducer.
|
|
2071
|
+
if (action.isAborted)
|
|
2072
|
+
throw store._abortError(action);
|
|
2056
2073
|
if (!isLast)
|
|
2057
2074
|
return null;
|
|
2058
2075
|
if (store._mustAbortAfterDebounce(action))
|
|
@@ -2071,25 +2088,29 @@ export class Store {
|
|
|
2071
2088
|
/**
|
|
2072
2089
|
* Resolves with `true` after the debounce period, if no other action with the same lock was
|
|
2073
2090
|
* dispatched meanwhile. Otherwise, resolves with `false` as soon as the other action is
|
|
2074
|
-
* dispatched (or the locks are removed)
|
|
2091
|
+
* dispatched (or the locks are removed), or as soon as the given `signal` of the waiting
|
|
2092
|
+
* action is aborted. An aborted action also removes its lock, so that the next action with
|
|
2093
|
+
* the same lock waits for its own debounce period.
|
|
2075
2094
|
*/
|
|
2076
|
-
_waitDebounce(lock, debounceMillis) {
|
|
2095
|
+
_waitDebounce(lock, debounceMillis, signal) {
|
|
2077
2096
|
const index = this._debounceLocks.findIndex(entry => _isSameNonReentrantKey(entry.lock, lock));
|
|
2078
2097
|
if (index !== -1)
|
|
2079
2098
|
this._debounceLocks.splice(index, 1)[0].cancel();
|
|
2080
2099
|
return new Promise(resolve => {
|
|
2081
|
-
const
|
|
2100
|
+
const end = (isLast) => {
|
|
2101
|
+
clearTimeout(timer);
|
|
2102
|
+
signal.removeEventListener('abort', onAbort);
|
|
2082
2103
|
this._debounceLocks = this._debounceLocks.filter(other => other !== entry);
|
|
2083
|
-
resolve(
|
|
2084
|
-
}, capTimeoutMillis(debounceMillis));
|
|
2085
|
-
const entry = {
|
|
2086
|
-
lock,
|
|
2087
|
-
cancel: () => {
|
|
2088
|
-
clearTimeout(timer);
|
|
2089
|
-
resolve(false);
|
|
2090
|
-
},
|
|
2104
|
+
resolve(isLast);
|
|
2091
2105
|
};
|
|
2106
|
+
const onAbort = () => end(false);
|
|
2107
|
+
const timer = setTimeout(() => end(true), capTimeoutMillis(debounceMillis));
|
|
2108
|
+
const entry = { lock, cancel: () => end(false) };
|
|
2092
2109
|
this._debounceLocks.push(entry);
|
|
2110
|
+
if (signal.aborted)
|
|
2111
|
+
end(false);
|
|
2112
|
+
else
|
|
2113
|
+
signal.addEventListener('abort', onAbort);
|
|
2093
2114
|
});
|
|
2094
2115
|
}
|
|
2095
2116
|
/**
|
|
@@ -2114,12 +2135,11 @@ export class Store {
|
|
|
2114
2135
|
const store = this;
|
|
2115
2136
|
function _wrapReducePolling(reduce) {
|
|
2116
2137
|
function _reducePolling() {
|
|
2117
|
-
//
|
|
2118
|
-
//
|
|
2119
|
-
//
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
throw kissAbort('The internal action props were cleared, so the action did not start polling.');
|
|
2138
|
+
// The action is about to start polling (or to find polling already active), so stopping
|
|
2139
|
+
// the polling from now on must not abort it: its immediate run still finishes. Note that
|
|
2140
|
+
// if the polling was stopped while the action was in an async `before()`, the action was
|
|
2141
|
+
// aborted, and the store doesn't call this reducer (see `_getWrapReduce`).
|
|
2142
|
+
store._pendingPollingStarts.delete(action);
|
|
2123
2143
|
// In a server store, `Poll.start` and `Poll.runNowAndRestart` run the action once, like
|
|
2124
2144
|
// `Poll.once`, and start no polling cycle: the server renders once, and nothing would
|
|
2125
2145
|
// use the next tick. The polling validations (`createPollingAction()` is overridden,
|
|
@@ -2137,6 +2157,9 @@ export class Store {
|
|
|
2137
2157
|
case Poll.stop:
|
|
2138
2158
|
if (cycle !== undefined)
|
|
2139
2159
|
store._stopPollingCycle(cycle);
|
|
2160
|
+
// The actions of this key that were dispatched before, but had not started polling
|
|
2161
|
+
// yet (they are in an async `before()`, for example) must not start polling later.
|
|
2162
|
+
store._abortPendingPollingStarts('Polling was stopped with Poll.stop, so the action did not start polling.', key);
|
|
2140
2163
|
return null;
|
|
2141
2164
|
case Poll.runNowAndRestart:
|
|
2142
2165
|
if (cycle !== undefined)
|
|
@@ -2263,9 +2286,12 @@ export class Store {
|
|
|
2263
2286
|
/**
|
|
2264
2287
|
* For Kiss internal use only. Use `stopAllPolling()` in the action instead.
|
|
2265
2288
|
* Stops all polling cycles. Runs that are in progress still finish, but no new ticks start.
|
|
2289
|
+
* The actions with `Poll.start` or `Poll.runNowAndRestart` that were dispatched before, but
|
|
2290
|
+
* had not started polling yet (they are in an async `before()`, for example) are aborted, so
|
|
2291
|
+
* that they don't start polling later. The `reason` is the message of their abort.
|
|
2266
2292
|
* @internal
|
|
2267
2293
|
*/
|
|
2268
|
-
_stopAllPolling() {
|
|
2294
|
+
_stopAllPolling(reason = 'All polling was stopped, so the action did not start polling.') {
|
|
2269
2295
|
const cycles = this._pollingCycles;
|
|
2270
2296
|
this._pollingCycles = [];
|
|
2271
2297
|
for (const cycle of cycles) {
|
|
@@ -2274,6 +2300,28 @@ export class Store {
|
|
|
2274
2300
|
cycle.timer = null;
|
|
2275
2301
|
cycle.cancelWait?.();
|
|
2276
2302
|
}
|
|
2303
|
+
this._abortPendingPollingStarts(reason);
|
|
2304
|
+
}
|
|
2305
|
+
// True if the action starts polling when its reducer runs: it has `Poll.start` or
|
|
2306
|
+
// `Poll.runNowAndRestart`, in a store that is not a server store (where they run once).
|
|
2307
|
+
_startsPolling(action) {
|
|
2308
|
+
return !this._isServer && (action.poll === Poll.start || action.poll === Poll.runNowAndRestart);
|
|
2309
|
+
}
|
|
2310
|
+
/**
|
|
2311
|
+
* Aborts the actions in progress that would start polling when their reducer runs, but have
|
|
2312
|
+
* not run it yet (see `_pendingPollingStarts`). With a `key`, only the actions of that polling
|
|
2313
|
+
* key. Without it, all of them. The aborted action finishes silently, with
|
|
2314
|
+
* `isDispatchAborted: true`, without running its reducer (see `_getWrapReduce`).
|
|
2315
|
+
*/
|
|
2316
|
+
_abortPendingPollingStarts(reason, key) {
|
|
2317
|
+
for (const action of this._actionsInProgress) {
|
|
2318
|
+
if (!this._pendingPollingStarts.has(action))
|
|
2319
|
+
continue;
|
|
2320
|
+
if (key !== undefined && !_isSameNonReentrantKey(action['computePollingKey'](), key))
|
|
2321
|
+
continue;
|
|
2322
|
+
this._pendingPollingStarts.delete(action);
|
|
2323
|
+
action._abort(reason);
|
|
2324
|
+
}
|
|
2277
2325
|
}
|
|
2278
2326
|
_retryWrapReduce(action, userWrapReduce) {
|
|
2279
2327
|
const store = this;
|
|
@@ -2333,6 +2381,10 @@ export class Store {
|
|
|
2333
2381
|
}
|
|
2334
2382
|
Store._logLazy(() => `${attempt} ${action}${attemptNumber}.`);
|
|
2335
2383
|
}
|
|
2384
|
+
// An action aborted while the internet was checked doesn't run its reducer. (The wait
|
|
2385
|
+
// between attempts already stops when the action is aborted.)
|
|
2386
|
+
if (action.isAborted)
|
|
2387
|
+
throw store._abortError(action);
|
|
2336
2388
|
// With unlimitedRetryCheckInternet, `dispatchSync` throws before the reducer runs for
|
|
2337
2389
|
// the first time (when the internet is checked). Since its result would be ignored,
|
|
2338
2390
|
// the reducer doesn't run.
|
|
@@ -3193,7 +3245,8 @@ export class Store {
|
|
|
3193
3245
|
/**
|
|
3194
3246
|
* Internal: Disposes the value of a prop, as described in `setProp`. Returns a promise if the
|
|
3195
3247
|
* disposal is async, or `null`. Neither throws nor rejects: the errors are reported with
|
|
3196
|
-
* `_reportDisposeError`.
|
|
3248
|
+
* `_reportDisposeError`. An async disposal is kept in `_pendingDisposals` until it settles,
|
|
3249
|
+
* so that `dispose()` waits for it even if the caller discards the returned promise.
|
|
3197
3250
|
*/
|
|
3198
3251
|
_disposeValue(key, value, dispose) {
|
|
3199
3252
|
const what = `Disposing the prop ${Store._describeKey(key)} threw an error`;
|
|
@@ -3210,7 +3263,10 @@ export class Store {
|
|
|
3210
3263
|
}
|
|
3211
3264
|
if (!Store._isPromiseLike(result))
|
|
3212
3265
|
return null;
|
|
3213
|
-
|
|
3266
|
+
const promise = Promise.resolve(result).then(() => undefined, (error) => this._reportDisposeError(error, what));
|
|
3267
|
+
this._pendingDisposals.add(promise);
|
|
3268
|
+
void promise.then(() => this._pendingDisposals.delete(promise));
|
|
3269
|
+
return promise;
|
|
3214
3270
|
}
|
|
3215
3271
|
/**
|
|
3216
3272
|
* Internal: Disposes the value with the TC39 explicit resource management protocol, if it
|
|
@@ -3310,9 +3366,10 @@ export class Store {
|
|
|
3310
3366
|
}
|
|
3311
3367
|
/**
|
|
3312
3368
|
* Internal: like `mergeServerState`, but doesn't publish the state change. Returns a function
|
|
3313
|
-
* that publishes it. For `StoreSlice
|
|
3314
|
-
*
|
|
3315
|
-
* publishing
|
|
3369
|
+
* that publishes it. For `StoreSlice` on the server, which merges the slice into the
|
|
3370
|
+
* request's store while React renders it (there is no commit on the server), where
|
|
3371
|
+
* publishing would make React warn about updating other components during a render. On the
|
|
3372
|
+
* client, `StoreSlice` merges with `mergeServerState`, when the route commits.
|
|
3316
3373
|
* @internal
|
|
3317
3374
|
*/
|
|
3318
3375
|
_mergeServerStateSilently(slice) {
|
|
@@ -3434,6 +3491,13 @@ export class Store {
|
|
|
3434
3491
|
this._focus.dispose();
|
|
3435
3492
|
this._online.dispose();
|
|
3436
3493
|
await this._disposeProps();
|
|
3494
|
+
// Also waits for the async disposals that started before (a prop removed with
|
|
3495
|
+
// `disposeProp`, `disposeProps` or `logOut`, or replaced with `setProp`) and are still
|
|
3496
|
+
// running, and for the ones started meanwhile (a disposal may dispose other props, and a
|
|
3497
|
+
// prop set during the disposal is disposed right away). The loop ends when no disposal is
|
|
3498
|
+
// running, so that the dependencies are closed only after all the resources.
|
|
3499
|
+
while (this._pendingDisposals.size > 0)
|
|
3500
|
+
await Promise.all([...this._pendingDisposals]);
|
|
3437
3501
|
await this._disposeInjected(this._dependencies, 'the dependencies');
|
|
3438
3502
|
await this._disposeInjected(this._configuration, 'the configuration');
|
|
3439
3503
|
}
|
|
@@ -14,12 +14,12 @@ const { createContext, useContext, useEffect, useLayoutEffect, useMemo, useState
|
|
|
14
14
|
// re-exports the components from here, and a plain Node import of it with that condition (as in
|
|
15
15
|
// the package's own tests) must not throw.
|
|
16
16
|
export const StoreContext = (typeof createContext === 'function')
|
|
17
|
-
? createContext({ store: null, getServerState: () => undefined })
|
|
17
|
+
? createContext({ store: null, getServerState: () => undefined, getState: () => undefined })
|
|
18
18
|
: null;
|
|
19
19
|
export function StoreProvider(props) {
|
|
20
20
|
const { store, children } = props;
|
|
21
21
|
const serverState = ('serverState' in props) ? props.serverState : store._initialState;
|
|
22
|
-
const value = useMemo(() => ({ store, getServerState: () => serverState }), [store, serverState]);
|
|
22
|
+
const value = useMemo(() => ({ store, getServerState: () => serverState, getState: store._getState }), [store, serverState]);
|
|
23
23
|
return (React.createElement(StoreContext.Provider, { value: value }, children));
|
|
24
24
|
}
|
|
25
25
|
// `useLayoutEffect` doesn't run on the server, and React 18 warns when it's rendered there.
|
|
@@ -44,16 +44,22 @@ const useIsomorphicLayoutEffect = (typeof window === 'undefined') ? useEffect :
|
|
|
44
44
|
* render the merged state, so the HTML shows the route's data.
|
|
45
45
|
* - On the client, while hydrating, the components inside it hydrate against the same merged
|
|
46
46
|
* server state (no mismatch, even if the live store already differs), and the slice is merged
|
|
47
|
-
* into the live store before
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
47
|
+
* into the live store in the same commit, before their effects run, so nothing changes under
|
|
48
|
+
* them after hydration.
|
|
49
|
+
* - On client-side navigation, the components inside it read the live state with the slice
|
|
50
|
+
* merged into it from their first render, so the first render of the route already shows its
|
|
51
|
+
* data: no empty or stale frame. The live store gets the slice when the route commits, before
|
|
52
|
+
* the effects of the route and before the browser paints, and the other components (the
|
|
53
|
+
* layout) catch up right then. A render that React throws away (a transition to the route
|
|
54
|
+
* that is cancelled, or that suspends and never commits) leaves nothing in the live store.
|
|
51
55
|
*
|
|
52
56
|
* The merge is `store.mergeServerState(slice)`: the `mergeServerState` option of `createStore`,
|
|
53
57
|
* or a shallow merge for plain-object states. The slice is merged once per mount: use `key`
|
|
54
58
|
* (the entity id, for example) to merge again when the route's data changes. If the store is
|
|
55
59
|
* not ready yet (a persistor still reading), the slice is merged again into the persisted
|
|
56
|
-
* state when it arrives.
|
|
60
|
+
* state when it arrives. If the `StoreProvider` above switches to another store while the slice
|
|
61
|
+
* stays mounted, the slice is merged into that store too (once per store), and the components
|
|
62
|
+
* inside it read and change that store.
|
|
57
63
|
*/
|
|
58
64
|
export function StoreSlice(props) {
|
|
59
65
|
const { state: slice, children } = props;
|
|
@@ -61,20 +67,96 @@ export function StoreSlice(props) {
|
|
|
61
67
|
const store = parent.store;
|
|
62
68
|
if (store === null)
|
|
63
69
|
throw new StoreException('StoreSlice must be used within a StoreProvider.');
|
|
64
|
-
// Once per mount
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
// Once per mount and store. Usually there's only one store. If the provider switches to another
|
|
71
|
+
// store while this slice stays mounted, the components inside it must read the new store, and
|
|
72
|
+
// still find the route's data there, so the slice is merged into the new store too (its
|
|
73
|
+
// current `state` prop: the latest data of the route, and a new store has no local changes to
|
|
74
|
+
// keep). If the provider switches back to a store this slice already used, the slice is not
|
|
75
|
+
// merged into it again, so the changes made in that store since then are kept.
|
|
76
|
+
//
|
|
77
|
+
// The server state of the subtree is the parent's server state with the slice: what the
|
|
78
|
+
// server HTML shows for this route. (React calls the `useState` initializer twice in
|
|
79
|
+
// StrictMode, and keeps one of the results. An `AppliedSlice` created in a render that React
|
|
80
|
+
// throws away is harmless: on the client, only `CommitSlice` changes the store.)
|
|
81
|
+
const [appliedByStore] = useState(() => new WeakMap());
|
|
82
|
+
let applied = appliedByStore.get(store);
|
|
83
|
+
if (applied === undefined) {
|
|
84
|
+
applied = new AppliedSlice(store, parent.getServerState(), parent.getState, slice);
|
|
85
|
+
appliedByStore.set(store, applied);
|
|
86
|
+
}
|
|
87
|
+
const value = useMemo(() => ({ store, getServerState: applied.getServerState, getState: applied.getState }), [store, applied]);
|
|
88
|
+
// `CommitSlice` comes before the children, so that its layout effect runs before theirs.
|
|
89
|
+
return (React.createElement(StoreContext.Provider, { value: value },
|
|
90
|
+
React.createElement(CommitSlice, { applied: applied }),
|
|
91
|
+
children));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The slice of a mounted `StoreSlice`, and how the components inside it read the state.
|
|
95
|
+
*
|
|
96
|
+
* On the server there is no commit, so the slice is merged into the request's store right away,
|
|
97
|
+
* while React renders (the server render is synchronous and never thrown away, and nothing
|
|
98
|
+
* listens to the request's store, so nothing is published).
|
|
99
|
+
*
|
|
100
|
+
* On the client, the live store is not changed while React renders: React may throw the render
|
|
101
|
+
* away (a cancelled transition, a route that suspends and never commits), and a change made in
|
|
102
|
+
* it would stay in the store and show up in the other components later. Instead, until the
|
|
103
|
+
* route commits, the components inside the slice read the parent's state with the slice merged
|
|
104
|
+
* into it (`getState`, cached by the parent's state, since `useSyncExternalStore` needs the
|
|
105
|
+
* same object while nothing changed), and the other components still read the parent's state.
|
|
106
|
+
* When the route commits, `commit` merges the slice into the live store, in a layout effect
|
|
107
|
+
* that runs before the effects of the route's components, and publishes the change, so that the
|
|
108
|
+
* other components catch up before the browser paints. From then on, the components inside the
|
|
109
|
+
* slice read the parent's state, which has the slice.
|
|
110
|
+
*
|
|
111
|
+
* The parent's state is what the context above the slice gives: below a `StoreProvider`, the
|
|
112
|
+
* live state; below another `StoreSlice`, the `getState` of that slice. So, when nested slices
|
|
113
|
+
* mount in the same render (in the Next.js app router, a layout and its page, on client-side
|
|
114
|
+
* navigation), the components inside the inner slice read the data of both slices from their
|
|
115
|
+
* first render. The outer slice commits first (its `CommitSlice` comes before the inner slice),
|
|
116
|
+
* then the inner one, so the live store gets both, and the inner one wins for a key both have.
|
|
117
|
+
*/
|
|
118
|
+
class AppliedSlice {
|
|
119
|
+
store;
|
|
120
|
+
parentGetState;
|
|
121
|
+
slice;
|
|
122
|
+
serverState;
|
|
123
|
+
isCommitted = false;
|
|
124
|
+
base = undefined;
|
|
125
|
+
merged = undefined;
|
|
126
|
+
constructor(store, parentServerState, parentGetState, slice) {
|
|
127
|
+
this.store = store;
|
|
128
|
+
this.parentGetState = parentGetState;
|
|
129
|
+
this.slice = slice;
|
|
130
|
+
this.serverState = store._mergeSlice(parentServerState, slice);
|
|
131
|
+
if (typeof window === 'undefined' || store.isServer) {
|
|
132
|
+
store._mergeServerStateSilently(slice);
|
|
133
|
+
this.isCommitted = true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
getServerState = () => this.serverState;
|
|
137
|
+
getState = () => {
|
|
138
|
+
const state = this.parentGetState();
|
|
139
|
+
if (this.isCommitted)
|
|
140
|
+
return state;
|
|
141
|
+
if (this.base !== state) {
|
|
142
|
+
this.base = state;
|
|
143
|
+
this.merged = this.store._mergeSlice(state, this.slice);
|
|
144
|
+
}
|
|
145
|
+
return this.merged;
|
|
146
|
+
};
|
|
147
|
+
/** Merges the slice into the live store, once. The second call (StrictMode) does nothing. */
|
|
148
|
+
commit() {
|
|
149
|
+
if (this.isCommitted)
|
|
150
|
+
return;
|
|
151
|
+
this.isCommitted = true;
|
|
152
|
+
this.store.mergeServerState(this.slice);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** Rendered before the children of a `StoreSlice`: its layout effect runs before theirs. */
|
|
156
|
+
function CommitSlice(props) {
|
|
157
|
+
const { applied } = props;
|
|
75
158
|
useIsomorphicLayoutEffect(() => {
|
|
76
|
-
applied.
|
|
159
|
+
applied.commit();
|
|
77
160
|
}, [applied]);
|
|
78
|
-
|
|
79
|
-
return (React.createElement(StoreContext.Provider, { value: value }, children));
|
|
161
|
+
return null;
|
|
80
162
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserializer.d.ts","sourceRoot":"","sources":["../../../../src/Esserializer/deserializer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AAqBhC,OAAO,kBAAkB,MAAM,0BAA0B,CAAC;AAE1D;;;GAGG;AACH,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAWxC;;;GAGG;AACH,iBAAS,wBAAwB,CAC/B,SAAS,EAAE,GAAG,EACd,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,EAC3B,OAAO,EAAE,kBAAkB,GAAG,GAAG,CAMlC;AAED;;;GAGG;AACH,iBAAS,wCAAwC,CAC/C,SAAS,EAAE,GAAG,EACd,YAAY,EAAE,YAAY,EAC1B,OAAO,GAAE,kBAAuB,GAC/B,GAAG,CA2DL;
|
|
1
|
+
{"version":3,"file":"deserializer.d.ts","sourceRoot":"","sources":["../../../../src/Esserializer/deserializer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AAqBhC,OAAO,kBAAkB,MAAM,0BAA0B,CAAC;AAE1D;;;GAGG;AACH,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAWxC;;;GAGG;AACH,iBAAS,wBAAwB,CAC/B,SAAS,EAAE,GAAG,EACd,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,EAC3B,OAAO,EAAE,kBAAkB,GAAG,GAAG,CAMlC;AAED;;;GAGG;AACH,iBAAS,wCAAwC,CAC/C,SAAS,EAAE,GAAG,EACd,YAAY,EAAE,YAAY,EAC1B,OAAO,GAAE,kBAAuB,GAC/B,GAAG,CA2DL;AAoND;;;;;GAKG;AACH,iBAAS,6BAA6B,CAAC,OAAO,GAAE,KAAK,CAAC,GAAG,CAAM,GAAG,YAAY,CAc7E;AAED,wFAAwF;AACxF,iBAAS,kBAAkB,CAAC,QAAQ,EAAE,GAAG,GAAG,MAAM,CAEjD;AAED,OAAO,EACL,wBAAwB,EACxB,wCAAwC,EACxC,6BAA6B,EAC7B,kBAAkB,EACnB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Hooks.d.ts","sourceRoot":"","sources":["../../../src/Hooks.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACvE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"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AG,EAAE,GAAG,UAAU,CAAC,GAAG,CAAC,CAAA;KAAE,EAAE,EACvD,EACE,mBAA2B,EAC3B,aAAoB,EACpB,SAAS,GACV,GAAE;QACD,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAC9B,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;KAClB,GAAG,OAAO,CAAC,IAAI,CAAC;IASxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACH,yBAAyB,CACvB,WAAW,EAAE;QAAE,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,UAAU,CAAC,EAAE,CAAC,CAAA;KAAE,EAAE,EACtD,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,SAAS,CAAC,EAAE,SAAS,CAAA;KAAE,GACjE,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAC1B,yBAAyB,CACvB,WAAW,EAAE;QAAE,KAAI,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,UAAU,CAAC,EAAE,CAAC,CAAA;KAAE,EAAE,EACtD,OAAO,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;KAAE,GACjE,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;CAiBlC"}
|
|
@@ -45,7 +45,12 @@ export declare enum Poll {
|
|
|
45
45
|
* Otherwise, runs the action immediately and starts periodic polling.
|
|
46
46
|
*/
|
|
47
47
|
start = "start",
|
|
48
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Stop polling (cancels the timer and does not run the action).
|
|
50
|
+
* Also aborts the actions of the same polling key that were dispatched with `Poll.start` or
|
|
51
|
+
* `Poll.runNowAndRestart`, but had not started polling yet (for example, because they were
|
|
52
|
+
* still in an async `before()`), so that they don't start polling later.
|
|
53
|
+
*/
|
|
49
54
|
stop = "stop",
|
|
50
55
|
/**
|
|
51
56
|
* Run the action immediately and restart polling from now.
|
|
@@ -2064,9 +2069,13 @@ export declare abstract class KissAction<St> {
|
|
|
2064
2069
|
* each key, call `stopAllPolling()` from any action. Polling also stops when you call
|
|
2065
2070
|
* `store.clearInternalActionProps()`, or `store.logOut()`, or when the store is shut down
|
|
2066
2071
|
* with `store.setShutDown(true)`, and is not restarted if you later turn the shutdown off.
|
|
2067
|
-
*
|
|
2068
|
-
*
|
|
2069
|
-
*
|
|
2072
|
+
*
|
|
2073
|
+
* Stopping polling, in any of these ways or with `Poll.stop`, also stops the actions with
|
|
2074
|
+
* `Poll.start` or `Poll.runNowAndRestart` that were dispatched before, but had not started
|
|
2075
|
+
* polling yet (for example, because they were still in an async `before()`). They don't start
|
|
2076
|
+
* polling, and are aborted (`Poll.stop` only aborts the ones of its own polling key). This
|
|
2077
|
+
* way, a component that stops polling when it unmounts doesn't leave polling running when its
|
|
2078
|
+
* start was still pending.
|
|
2070
2079
|
*
|
|
2071
2080
|
* You can display loading states and errors in your components by tracking the action
|
|
2072
2081
|
* class that does the work:
|
|
@@ -2218,7 +2227,8 @@ export declare abstract class KissAction<St> {
|
|
|
2218
2227
|
* - `Poll.start`: Starts polling and runs `reduce` immediately.
|
|
2219
2228
|
* If polling is already active for this key, does nothing.
|
|
2220
2229
|
*
|
|
2221
|
-
* - `Poll.stop`: Cancels the polling for this key and skips `reduce`.
|
|
2230
|
+
* - `Poll.stop`: Cancels the polling for this key and skips `reduce`. Also aborts the actions
|
|
2231
|
+
* of this key that would start polling, but had not started it yet.
|
|
2222
2232
|
*
|
|
2223
2233
|
* - `Poll.runNowAndRestart`: Runs `reduce` immediately and restarts the polling timer from
|
|
2224
2234
|
* that moment. If polling is not active, behaves like `Poll.start`.
|
|
@@ -2455,7 +2465,10 @@ export declare abstract class KissAction<St> {
|
|
|
2455
2465
|
/**
|
|
2456
2466
|
* Stops all polling at once, for all polling keys, as if `Poll.stop` was dispatched for each
|
|
2457
2467
|
* of them. This is probably useful during logout or similar scenarios. Runs that are already
|
|
2458
|
-
* in progress still finish, but no new ticks are dispatched.
|
|
2468
|
+
* in progress still finish, but no new ticks are dispatched. The actions with `Poll.start` or
|
|
2469
|
+
* `Poll.runNowAndRestart` that were dispatched before, but had not started polling yet (for
|
|
2470
|
+
* example, because they were still in an async `before()`) are aborted, so that they don't
|
|
2471
|
+
* start polling later.
|
|
2459
2472
|
*/
|
|
2460
2473
|
stopAllPolling(): void;
|
|
2461
2474
|
/**
|
|
@@ -3640,6 +3653,14 @@ export declare class ActionStatus {
|
|
|
3640
3653
|
* doesn't fail, and doesn't show errors. It also doesn't roll back the optimistic value, and
|
|
3641
3654
|
* doesn't reload, since that would change the state of the next user.
|
|
3642
3655
|
*
|
|
3656
|
+
* ## Aborting
|
|
3657
|
+
*
|
|
3658
|
+
* When the command is aborted (by `abort()`, or by `store.abort(ActionType)`) while its server
|
|
3659
|
+
* call runs, its result is ignored. When the call finishes, the server response is not applied,
|
|
3660
|
+
* the optimistic value is not rolled back, nothing is reloaded, and the call is not retried. The
|
|
3661
|
+
* optimistic value stays in the state. The command finishes silently, with
|
|
3662
|
+
* `isDispatchAborted: true`.
|
|
3663
|
+
*
|
|
3643
3664
|
* ## CheckInternet
|
|
3644
3665
|
*
|
|
3645
3666
|
* When combined with `checkInternet`, if there is no internet: no optimistic state is
|
|
@@ -4123,6 +4144,10 @@ export declare abstract class OptimisticCommand<St, T = any> extends KissAction<
|
|
|
4123
4144
|
* `true` meanwhile. The dispatches made while the key is taken apply their optimistic value,
|
|
4124
4145
|
* and then finish right away.
|
|
4125
4146
|
*
|
|
4147
|
+
* This includes a dispatch made while the server response is applied (for example, by
|
|
4148
|
+
* `store.dispatchWhen`, when the state with the response meets its condition). If that dispatch
|
|
4149
|
+
* changed the value, the dispatch that took the key sends one more request, with the new value.
|
|
4150
|
+
*
|
|
4126
4151
|
* ## Clearing
|
|
4127
4152
|
*
|
|
4128
4153
|
* `store.clearInternalActionProps()` (which is also called by `store.setShutDown(true)`)
|
|
@@ -4132,6 +4157,16 @@ export declare abstract class OptimisticCommand<St, T = any> extends KissAction<
|
|
|
4132
4157
|
* doesn't apply the server response, and doesn't call `onFinish`. It's aborted with an
|
|
4133
4158
|
* `AbortDispatchException`, so it doesn't fail, and doesn't show errors.
|
|
4134
4159
|
*
|
|
4160
|
+
* ## Aborting
|
|
4161
|
+
*
|
|
4162
|
+
* When the dispatch that sends the requests is aborted (by `abort()`, or by
|
|
4163
|
+
* `store.abort(ActionType)`) while a request is in flight, it stops when that request finishes,
|
|
4164
|
+
* or when the request rejects because of the `abortSignal`. It releases the key, doesn't send
|
|
4165
|
+
* follow-up requests, doesn't apply the server response, and doesn't call `onFinish`. It
|
|
4166
|
+
* finishes silently, with `isDispatchAborted: true`. The value in the state is not changed, so
|
|
4167
|
+
* a local change made while the request was in flight may not reach the server. The next
|
|
4168
|
+
* dispatch of the key takes the key, and sends its own value.
|
|
4169
|
+
*
|
|
4135
4170
|
* Notes:
|
|
4136
4171
|
* - It can be combined with `checkInternet`, both `{ dialog: true | false }` and
|
|
4137
4172
|
* `{ abort: true }`. If there is no internet, the optimistic value is not applied, and no
|
|
@@ -4297,7 +4332,8 @@ export declare abstract class OptimisticSync<St, T = any> extends KissAction<St>
|
|
|
4297
4332
|
*
|
|
4298
4333
|
* Note `onFinish` is called in both success and failure scenarios. On success, it runs only
|
|
4299
4334
|
* after the state is stable for this key. On failure, it runs immediately after the request
|
|
4300
|
-
* fails (there is no further stabilization or follow-up).
|
|
4335
|
+
* fails (there is no further stabilization or follow-up). It's not called when the action is
|
|
4336
|
+
* aborted, or when `store.clearInternalActionProps()` released the key.
|
|
4301
4337
|
*
|
|
4302
4338
|
* Important: The key is released *before* `onFinish` runs. This means new dispatches for the
|
|
4303
4339
|
* same key may start a new request while `onFinish` is still running.
|
|
@@ -4377,6 +4413,10 @@ export declare abstract class OptimisticSync<St, T = any> extends KissAction<St>
|
|
|
4377
4413
|
* If they are different, it means the state was changed after we sent the request, so we
|
|
4378
4414
|
* should send another request with the new value.
|
|
4379
4415
|
*
|
|
4416
|
+
* It's also called after the server response is applied, if applying it caused another
|
|
4417
|
+
* change of the state (for example, a save dispatched by `store.dispatchWhen`). Then, the
|
|
4418
|
+
* `sentValue` is the value that the server response put in the state.
|
|
4419
|
+
*
|
|
4380
4420
|
* Values are compared with `Object.is` (which is the same as `===`, except that `NaN` is equal
|
|
4381
4421
|
* to `NaN`). So, make sure `getValueFromState` returns the same object you applied, or
|
|
4382
4422
|
* override this method if you need custom equality logic.
|
|
@@ -4604,6 +4644,13 @@ export declare abstract class OptimisticSync<St, T = any> extends KissAction<St>
|
|
|
4604
4644
|
* `onFinish`. It's aborted with an `AbortDispatchException`, so it doesn't fail, and doesn't
|
|
4605
4645
|
* show errors.
|
|
4606
4646
|
*
|
|
4647
|
+
* ## Aborting
|
|
4648
|
+
*
|
|
4649
|
+
* As in `OptimisticSync`, when the dispatch that sends the requests is aborted (by `abort()`, or
|
|
4650
|
+
* by `store.abort(ActionType)`) while a request is in flight, it stops when that request
|
|
4651
|
+
* finishes. It releases the key, doesn't send follow-up requests, doesn't apply the server
|
|
4652
|
+
* response, and doesn't call `onFinish`. It finishes silently, with `isDispatchAborted: true`.
|
|
4653
|
+
*
|
|
4607
4654
|
* Notes:
|
|
4608
4655
|
* - It can be combined with `checkInternet`, both `{ dialog: true | false }` and
|
|
4609
4656
|
* `{ abort: true }`. If there is no internet, the optimistic value is not applied, and no
|
|
@@ -4651,7 +4698,6 @@ export declare abstract class OptimisticSyncWithPush<St, T = any> extends KissAc
|
|
|
4651
4698
|
maxFollowUpRequests: number;
|
|
4652
4699
|
private _key;
|
|
4653
4700
|
private _informedServerRevision;
|
|
4654
|
-
private _wasPushStale;
|
|
4655
4701
|
/**
|
|
4656
4702
|
* Optionally, override `optimisticSyncKeyParams()` to differentiate the coalescing by the
|
|
4657
4703
|
* action parameters. For example, if you have a like button per item, return the item ID, so
|
|
@@ -4845,7 +4891,8 @@ export declare abstract class OptimisticSyncWithPush<St, T = any> extends KissAc
|
|
|
4845
4891
|
*
|
|
4846
4892
|
* Note `onFinish` is called in both success and failure scenarios. On success, it runs only
|
|
4847
4893
|
* after the state is stable for this key. On failure, it runs immediately after the request
|
|
4848
|
-
* fails (there is no further stabilization or follow-up).
|
|
4894
|
+
* fails (there is no further stabilization or follow-up). It's not called when the action is
|
|
4895
|
+
* aborted, or when `store.clearInternalActionProps()` released the key.
|
|
4849
4896
|
*
|
|
4850
4897
|
* Important: The key is released *before* `onFinish` runs. This means new dispatches for the
|
|
4851
4898
|
* same key may start a new request while `onFinish` is still running.
|
|
@@ -4892,6 +4939,11 @@ export declare abstract class OptimisticSyncWithPush<St, T = any> extends KissAc
|
|
|
4892
4939
|
* kept by Kiss, and the one in the state.
|
|
4893
4940
|
*/
|
|
4894
4941
|
private _revisionOf;
|
|
4942
|
+
/**
|
|
4943
|
+
* True if the latest change of the key of this dispatch was made locally (not by a push), and
|
|
4944
|
+
* is newer than the given local-revision.
|
|
4945
|
+
*/
|
|
4946
|
+
private _hasLocalChangeAfter;
|
|
4895
4947
|
private _clearedError;
|
|
4896
4948
|
private _applyState;
|
|
4897
4949
|
}
|
|
@@ -4924,12 +4976,18 @@ export type PushMetadata = {
|
|
|
4924
4976
|
*
|
|
4925
4977
|
* 1. If the push is not newer than the newest known server revision for the key (considering
|
|
4926
4978
|
* both the revision kept by Kiss, and `getServerRevisionFromState()`), it's stale or out of
|
|
4927
|
-
* order, so it's ignored.
|
|
4979
|
+
* order, so it's ignored. There is one exception: the echo of the latest request of this
|
|
4980
|
+
* device, when the server response already informed its server revision (with
|
|
4981
|
+
* `informServerRevision`), but the state doesn't have that revision yet. It's applied as in
|
|
4982
|
+
* step 3, so that `applyServerPushToState` saves the server revision in the state.
|
|
4928
4983
|
*
|
|
4929
4984
|
* 2. If the push is the echo of an older request of this same device (the user changed the
|
|
4930
4985
|
* value again after that request was sent), it's not applied, since the state already has a
|
|
4931
4986
|
* newer local value. Its server revision is still recorded, but it doesn't count as a push,
|
|
4932
|
-
* so the newer local value is still sent in a follow-up request.
|
|
4987
|
+
* so the newer local value is still sent in a follow-up request. If a push from another
|
|
4988
|
+
* device was applied before this echo, that push is older than the request, so its value is
|
|
4989
|
+
* stale: the request in flight puts the latest local value back in the state when it
|
|
4990
|
+
* finishes, and sends it in the follow-up request.
|
|
4933
4991
|
*
|
|
4934
4992
|
* 3. Otherwise (a push from another device, or the echo of the latest request of this device),
|
|
4935
4993
|
* it's applied with `applyServerPushToState`, and recorded as the latest change of the key.
|