@sanity/sdk 2.20.0 → 2.20.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/dist/_exports/_internal.js +1 -1
- package/dist/index.js +64 -17
- package/dist/index.js.map +1 -1
- package/dist/{telemetryManager-Bd3O8z2S.js → telemetryManager-BmGqzj0b.js} +2 -2
- package/dist/{telemetryManager-Bd3O8z2S.js.map → telemetryManager-BmGqzj0b.js.map} +1 -1
- package/dist/{version-DL2l07HG.js → version-CPumgx0u.js} +2 -2
- package/dist/{version-DL2l07HG.js.map → version-CPumgx0u.js.map} +1 -1
- package/package.json +4 -4
- package/src/document/documentConstants.ts +9 -0
- package/src/document/documentStore.test.ts +69 -0
- package/src/document/reducers.test.ts +224 -0
- package/src/document/reducers.ts +108 -25
|
@@ -95,7 +95,7 @@ function initTelemetry(instance, projectId) {
|
|
|
95
95
|
projectId,
|
|
96
96
|
environment
|
|
97
97
|
}), Promise.all([
|
|
98
|
-
import("../telemetryManager-
|
|
98
|
+
import("../telemetryManager-BmGqzj0b.js").then((n) => n.t),
|
|
99
99
|
import("../clientStore-CMzJ7XhZ.js").then((n) => n.t),
|
|
100
100
|
import("../authStore-CWTAuieY.js").then((n) => n.n)
|
|
101
101
|
]).then(async ([{ createTelemetryManager }, { getClient }, { getTokenState }]) => {
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { A as defineStore, C as REQUEST_TAG_PREFIX, D as bindActionByResource, E
|
|
|
2
2
|
import { A as addComment, B as normalizeComment, C as getCommentThreadsState, D as resolveCommentThreads, F as removeCommentById, G as COMMENTS_API_VERSION, H as assertDatasetResource, I as restoreComments, K as setCleanupTimeout, L as rollbackCommentUpdate, M as clearPendingTransaction, N as getCommentsKey, O as resolveComments, P as receiveComment, R as setCommentCreateError, S as commentsStore, T as getCommentsState, U as getAddonDatasetState, V as toCommentFieldPath, W as provisionAddonDataset, _ as setUsersData, a as getQueryState, b as PROJECT_API_VERSION, c as getPerspectiveState, d as addSubscription, f as cancelRequest, g as removeSubscription, h as parseUsersKey, i as getQueryKey, j as applyCommentUpdate, k as toCommentsKeyParts, l as getActiveReleasesState, m as initializeRequest, n as transformProjectionToPreview, o as parseQueryKey, p as getUsersKey, q as insecureRandomId, r as PREVIEW_PROJECTION, s as resolveQuery, t as createGroqSearchFilter, u as getAllReleasesState, v as setUsersError, x as USERS_STATE_CLEAR_DELAY, y as updateLastLoadMoreRequest, z as setPendingTransaction } from "./createGroqSearchFilter-B0NdA-EP.js";
|
|
3
3
|
import { n as configureLogging$1, r as createLogger } from "./getEnv-76AjZd_f.js";
|
|
4
4
|
import { i as isDeepEqual, n as getClient, o as omitProperty, r as getClientState, s as pickProperties } from "./clientStore-CMzJ7XhZ.js";
|
|
5
|
-
import { t as CORE_SDK_VERSION } from "./version-
|
|
5
|
+
import { t as CORE_SDK_VERSION } from "./version-CPumgx0u.js";
|
|
6
6
|
import { ClientError, CorsOriginError } from "@sanity/client";
|
|
7
7
|
import { EMPTY, NEVER, Observable, Subject, Subscription, asapScheduler, auditTime, catchError, combineLatest, concat, concatMap, debounceTime, defer, distinctUntilChanged, filter, first, firstValueFrom, from, fromEvent, groupBy, map, merge, mergeMap, of, pairwise, partition, race, retry, share, shareReplay, skip, startWith, switchMap, take, takeUntil, tap, throttle, throwError, timer, withLatestFrom } from "rxjs";
|
|
8
8
|
import { isKeySegment, isKeyedObject } from "@sanity/types";
|
|
@@ -2755,7 +2755,8 @@ function cleanupOutgoingTransaction(prev) {
|
|
|
2755
2755
|
for (let transactionId of outgoing.batchedTransactionIds) for (let documentId of ids) next = removeSubscriptionIdFromDocument(next, documentId, transactionId);
|
|
2756
2756
|
return {
|
|
2757
2757
|
...next,
|
|
2758
|
-
outgoing: void 0
|
|
2758
|
+
outgoing: void 0,
|
|
2759
|
+
documentStates: evictOrphanedDocumentStates(next.documentStates)
|
|
2759
2760
|
};
|
|
2760
2761
|
}
|
|
2761
2762
|
function revertOutgoingTransaction(prev) {
|
|
@@ -2785,11 +2786,16 @@ function revertOutgoingTransaction(prev) {
|
|
|
2785
2786
|
...base,
|
|
2786
2787
|
applied: nextApplied,
|
|
2787
2788
|
outgoing: void 0,
|
|
2788
|
-
documentStates: Object.fromEntries(Object.entries(base.documentStates).filter((e) => !!e[1]).map(([documentId,
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2789
|
+
documentStates: evictOrphanedDocumentStates(Object.fromEntries(Object.entries(base.documentStates).filter((e) => !!e[1]).map(([documentId, documentState]) => {
|
|
2790
|
+
let { unverifiedRevisions = {} } = documentState, next = {
|
|
2791
|
+
...documentState,
|
|
2792
|
+
unverifiedRevisions: prev.outgoing && prev.outgoing.transactionId in unverifiedRevisions ? omitProperty(unverifiedRevisions, prev.outgoing.transactionId) : unverifiedRevisions
|
|
2793
|
+
};
|
|
2794
|
+
return !next.subscriptions.length && isAwaitingOwnEcho(next) ? [documentId, next] : [documentId, {
|
|
2795
|
+
...next,
|
|
2796
|
+
local: documentId in working ? working[documentId] : next.local
|
|
2797
|
+
}];
|
|
2798
|
+
})))
|
|
2793
2799
|
};
|
|
2794
2800
|
}
|
|
2795
2801
|
/**
|
|
@@ -2830,7 +2836,7 @@ function applyRemoteDocument(prev, { document, documentId, previousRev, revision
|
|
|
2830
2836
|
let modifiesDocument = (transaction) => transaction.working[documentId]?._rev !== transaction.previousRevs[documentId], local = !prev.applied.some(modifiesDocument) && (!prev.outgoing || prev.outgoing.transactionId === revision || !modifiesDocument(prev.outgoing)) && !isDeepEqual(prevDocState.local, document) ? document : prevDocState.local;
|
|
2831
2837
|
return {
|
|
2832
2838
|
...prev,
|
|
2833
|
-
documentStates: {
|
|
2839
|
+
documentStates: evictOrphanedDocumentStates({
|
|
2834
2840
|
...prev.documentStates,
|
|
2835
2841
|
[documentId]: {
|
|
2836
2842
|
...prevDocState,
|
|
@@ -2839,7 +2845,7 @@ function applyRemoteDocument(prev, { document, documentId, previousRev, revision
|
|
|
2839
2845
|
local,
|
|
2840
2846
|
unverifiedRevisions
|
|
2841
2847
|
}
|
|
2842
|
-
}
|
|
2848
|
+
})
|
|
2843
2849
|
};
|
|
2844
2850
|
}
|
|
2845
2851
|
let working = {
|
|
@@ -2874,7 +2880,7 @@ function applyRemoteDocument(prev, { document, documentId, previousRev, revision
|
|
|
2874
2880
|
return {
|
|
2875
2881
|
...prev,
|
|
2876
2882
|
applied: nextApplied,
|
|
2877
|
-
documentStates: {
|
|
2883
|
+
documentStates: evictOrphanedDocumentStates({
|
|
2878
2884
|
...prev.documentStates,
|
|
2879
2885
|
[documentId]: {
|
|
2880
2886
|
...prevDocState,
|
|
@@ -2883,7 +2889,7 @@ function applyRemoteDocument(prev, { document, documentId, previousRev, revision
|
|
|
2883
2889
|
local,
|
|
2884
2890
|
unverifiedRevisions
|
|
2885
2891
|
}
|
|
2886
|
-
}
|
|
2892
|
+
})
|
|
2887
2893
|
};
|
|
2888
2894
|
}
|
|
2889
2895
|
function addSubscriptionIdToDocument(prev, documentId, subscriptionId) {
|
|
@@ -2901,8 +2907,13 @@ function addSubscriptionIdToDocument(prev, documentId, subscriptionId) {
|
|
|
2901
2907
|
};
|
|
2902
2908
|
}
|
|
2903
2909
|
function removeSubscriptionIdFromDocument(prev, documentId, subscriptionId) {
|
|
2904
|
-
let prevDocState = prev.documentStates?.[documentId]
|
|
2905
|
-
|
|
2910
|
+
let prevDocState = prev.documentStates?.[documentId];
|
|
2911
|
+
if (!prevDocState) return prev;
|
|
2912
|
+
let subscriptions = prevDocState.subscriptions.filter((id) => id !== subscriptionId);
|
|
2913
|
+
return !subscriptions.length && !isAwaitingOwnEcho(prevDocState) ? {
|
|
2914
|
+
...prev,
|
|
2915
|
+
documentStates: omitProperty(prev.documentStates, documentId)
|
|
2916
|
+
} : {
|
|
2906
2917
|
...prev,
|
|
2907
2918
|
documentStates: {
|
|
2908
2919
|
...prev.documentStates,
|
|
@@ -2911,10 +2922,46 @@ function removeSubscriptionIdFromDocument(prev, documentId, subscriptionId) {
|
|
|
2911
2922
|
subscriptions
|
|
2912
2923
|
}
|
|
2913
2924
|
}
|
|
2914
|
-
}
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2925
|
+
};
|
|
2926
|
+
}
|
|
2927
|
+
/**
|
|
2928
|
+
* Whether this document still expects the listener echo of one of its own
|
|
2929
|
+
* submitted transactions. That expectation is the only reason a document state
|
|
2930
|
+
* with no subscribers is kept around.
|
|
2931
|
+
*
|
|
2932
|
+
* Revisions stop counting after `UNVERIFIED_REVISION_RETENTION_TIME` because an
|
|
2933
|
+
* echo is not guaranteed to arrive at all: Content Lake records no transaction
|
|
2934
|
+
* when every operation resolves to a no-op (e.g. a keyed path that no longer
|
|
2935
|
+
* exists), while the client bumps `_rev` regardless and so tracks a revision
|
|
2936
|
+
* for it. Without the expiry, such a document (and the listener keyed off its
|
|
2937
|
+
* state) would be pinned for the lifetime of the store.
|
|
2938
|
+
*
|
|
2939
|
+
* `recentOwnTransactionIds` deliberately does not extend the wait: it is capped
|
|
2940
|
+
* rather than drained, so honoring it would keep every edited document alive.
|
|
2941
|
+
*/
|
|
2942
|
+
function isAwaitingOwnEcho(documentState) {
|
|
2943
|
+
let expiresAfter = Date.now() - 3e4;
|
|
2944
|
+
return Object.values(documentState.unverifiedRevisions ?? {}).some((revision) => !!revision && new Date(revision.timestamp).getTime() > expiresAfter);
|
|
2945
|
+
}
|
|
2946
|
+
/**
|
|
2947
|
+
* Removes every document state that has no subscribers left and no own
|
|
2948
|
+
* transaction still awaiting its echo.
|
|
2949
|
+
*
|
|
2950
|
+
* This is a sweep rather than a decision made where the last subscriber leaves
|
|
2951
|
+
* because `removeSubscriptionIdFromDocument` defers that eviction while an echo
|
|
2952
|
+
* is in flight, and both halves of the condition can change without a
|
|
2953
|
+
* subscriber going anywhere: the echo can verify, be dropped by a sync, be
|
|
2954
|
+
* discarded by a revert, or simply never arrive. Every reducer that can settle
|
|
2955
|
+
* an in-flight transaction sweeps, so the deferred decision is always re-made.
|
|
2956
|
+
*
|
|
2957
|
+
* Note this can evict the very document whose remote event is being processed,
|
|
2958
|
+
* which synchronously unsubscribes that document's listener from inside its own
|
|
2959
|
+
* emission (see `subscribeToSubscriptionsAndListenToDocuments`).
|
|
2960
|
+
*/
|
|
2961
|
+
function evictOrphanedDocumentStates(documentStates) {
|
|
2962
|
+
let next = documentStates;
|
|
2963
|
+
for (let [documentId, documentState] of Object.entries(documentStates)) documentState && (documentState.subscriptions.length || isAwaitingOwnEcho(documentState) || (next = omitProperty(next, documentId)));
|
|
2964
|
+
return next;
|
|
2918
2965
|
}
|
|
2919
2966
|
function manageSubscriberIds({ state }, handles) {
|
|
2920
2967
|
let documentIds = getDocumentIdsFromHandleLikes(handles), subscriptionId = insecureRandomId();
|