@tanstack/db
Version:
A reactive client store for building super fast apps on sync
742 lines (741 loc) • 29 kB
JavaScript
"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const cursor = require("../../utils/cursor.cjs");
const error = require("../../utils/error.cjs");
const callbacks = require("../../utils/callbacks.cjs");
const expressions = require("../compiler/expressions.cjs");
class OrderedPostSettlementError {
constructor(cause) {
this.cause = cause;
}
}
class OrderedSourceLoader {
constructor(info, subscription, alias, onResult = () => {
}, canRetryRepair = () => false, getGraphInputRevision, hasPendingJoinedWork = () => false) {
this.info = info;
this.subscription = subscription;
this.alias = alias;
this.onResult = onResult;
this.canRetryRepair = canRetryRepair;
this.getGraphInputRevision = getGraphInputRevision;
this.hasPendingJoinedWork = hasPendingJoinedWork;
this.hasSettledSourceRequest = false;
this.needsOrderingRepair = false;
this.canRepairWithOrderedPrefix = false;
this.requesting = false;
this.authoritativeRequestState = `none`;
this.settledFiniteAcquisitions = /* @__PURE__ */ new Map();
this.orderedRepairAcquisitions = /* @__PURE__ */ new Set();
this.failedAcquisitions = /* @__PURE__ */ new Map();
this.active = true;
this.orderedLoadGeneration = 0;
this.orderingInvalidationGeneration = 0;
this.repairRetries = 0;
this.drainingNoInputContinuations = false;
this.info.isRequesting = () => this.requesting;
}
/** Derive invalidation from actual contributions, not a second cursor. */
onSourceChanges(changes, sentRows) {
let hasNewRows = false;
for (const change of changes) {
const previous = sentRows?.get(change.key);
if (change.type !== `insert` && previous !== void 0 && (change.type === `delete` || this.info.comparator(previous, change.value) !== 0)) {
this.invalidateSourceOrdering();
return;
}
if (change.type !== `delete` && previous === void 0) hasNewRows = true;
}
if (hasNewRows) this.invalidateCursor();
}
start() {
const { index, limit, offset, requiresFullSource } = this.info;
if (index) this.subscription.setOrderByIndex(index);
if (limit === 0) return;
if (requiresFullSource) {
this.loadFullSource();
return;
}
if (!index) {
this.loadPrefix(offset + limit);
return;
}
this.loadPage(offset + limit);
}
loadMore(windowOperationGeneration, continuesOrderedPrefixRepair = false) {
if (!this.active || this.info.limit === 0 || this.requesting) return;
windowOperationGeneration ??= this.blockedLoadMore?.windowOperationGeneration;
continuesOrderedPrefixRepair ||= this.blockedLoadMore?.continuesOrderedPrefixRepair ?? false;
if (this.info.joinedFilterSourceId && this.hasPendingJoinedWork()) {
if (windowOperationGeneration !== void 0 || continuesOrderedPrefixRepair) {
this.blockedLoadMore = {
windowOperationGeneration,
continuesOrderedPrefixRepair
};
}
return this.pending;
}
this.blockedLoadMore = void 0;
if (this.stagedContinuation) {
if (this.needsOrderingRepair && !this.stagedContinuation.isAuthoritativeRepair) {
this.stagedContinuation = void 0;
} else {
this.consumeStagedContinuation(windowOperationGeneration);
if (!this.active || this.pending) return this.pending;
}
}
const mayRetryFailure = this.failedRequest === void 0 || windowOperationGeneration !== void 0 && windowOperationGeneration !== this.failedRequest.windowOperationGeneration;
if (!mayRetryFailure) return this.pending;
if ((this.failedRequest || this.failedAcquisitions.size > 0) && windowOperationGeneration !== void 0) {
this.cancelRepairRetry();
this.repairRetries = 0;
if (this.failedRequest) {
this.failedRequest.windowOperationGeneration = windowOperationGeneration;
}
this.releaseFailedAcquisitions();
if (!this.active) return;
}
if (this.authoritativeRequestState === `failed`) {
this.authoritativeRequestState = `none`;
} else if (this.authoritativeRequestState !== `none` && !(continuesOrderedPrefixRepair && this.authoritativeRequestKind === `ordered-repair`))
return this.pending;
if (this.needsOrderingRepair && !(continuesOrderedPrefixRepair && this.authoritativeRequestKind === `ordered-repair`) || this.info.requiresFullSource) {
if (this.canRepairWithOrderedPrefix && !this.info.requiresFullSource) {
this.loadOrderedPrefixRepair(windowOperationGeneration);
} else {
this.loadFullSource(windowOperationGeneration);
}
return this.pending;
}
if (!this.info.index) {
const shouldLoad = windowOperationGeneration !== void 0 || (this.info.dataNeeded?.() ?? 0) > 0 || this.lastPrefixCount !== void 0 && this.lastPrefixCount < this.info.offset + this.info.limit;
if (shouldLoad) {
this.loadPrefix(
this.info.offset + this.info.limit,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
} else if (continuesOrderedPrefixRepair) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
}
return this.pending;
}
if (!this.info.dataNeeded || this.pending) {
if (continuesOrderedPrefixRepair && !this.pending) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
}
return this.pending;
}
let count = Math.max(
this.info.dataNeeded(),
this.hasSettledSourceRequest ? 0 : this.info.offset + this.info.limit
);
if (windowOperationGeneration !== void 0 && this.settledSourceBoundary !== void 0) {
const needed = this.info.offset + this.info.limit;
count = Math.max(count, needed - this.countAcquiredRows());
}
if (count > 0) {
this.loadPage(
count,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
} else if (continuesOrderedPrefixRepair) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
}
return this.pending;
}
loadFullSource(windowOperationGeneration) {
if (!this.active || this.authoritativeRequestState !== `none`) return;
this.authoritativeRequestState = `held`;
this.authoritativeRequestKind = `full-source`;
this.requestAndObserve(
(onLoadSubsetResult) => {
this.subscription.requestSnapshot({
...this.needsOrderingRepair ? { refetch: true } : {},
trackLoadSubsetPromise: false,
onLoadSubsetResult
});
},
`full-source`,
windowOperationGeneration
);
}
loadOrderedPrefixRepair(windowOperationGeneration) {
if (!this.active || this.authoritativeRequestState !== `none`) return;
this.orderedPrefixRepairGeneration = this.orderingInvalidationGeneration;
this.orderedRepairAcquisitions = /* @__PURE__ */ new Set();
this.authoritativeRequestState = `held`;
this.authoritativeRequestKind = `ordered-repair`;
this.requestAndObserve(
(onLoadSubsetResult) => {
this.subscription.requestSnapshot({
refetch: true,
orderBy: expressions.normalizeOrderByPaths(this.info.orderBy, this.alias),
limit: this.info.offset + this.info.limit,
trackLoadSubsetPromise: false,
onLoadSubsetResult
});
},
`ordered-repair`,
windowOperationGeneration,
true
);
}
loadPrefix(count, windowOperationGeneration, continuesOrderedPrefixRepair = false) {
if (!this.active || this.pending || this.stagedContinuation) return;
if (this.lastPrefixCount === count) {
if ((this.info.dataNeeded?.() ?? 0) > 0) {
if (continuesOrderedPrefixRepair) this.abandonOrderedPrefixRepair();
this.loadFullSource(windowOperationGeneration);
} else if (continuesOrderedPrefixRepair) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
}
return;
}
this.requestAndObserve(
(onLoadSubsetResult) => {
this.subscription.requestSnapshot({
...continuesOrderedPrefixRepair ? { refetch: true } : {},
orderBy: expressions.normalizeOrderByPaths(this.info.orderBy, this.alias),
limit: count,
trackLoadSubsetPromise: false,
onLoadSubsetResult
});
},
`ordered`,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
this.lastPrefixCount = count;
}
resetCursor() {
this.cancelRepairRetry();
this.repairRetries = 0;
this.orderedLoadGeneration++;
if (this.authoritativeRequestState === `complete`)
this.authoritativeRequestState = `held`;
this.pending = void 0;
this.blockedLoadMore = void 0;
this.lastBoundary = void 0;
this.settledSourceBoundary = void 0;
this.stagedContinuation = void 0;
this.invalidateCursor();
}
settleFullSourceReplay() {
if (this.authoritativeRequestKind === `ordered-repair`) {
this.finishOrderedPrefixRepair();
return;
}
if (this.authoritativeRequestState === `failed`) {
for (const [release, kind] of this.failedAcquisitions) {
if (kind === `full-source`) this.failedAcquisitions.delete(release);
}
this.authoritativeRequestState = `held`;
}
if (this.authoritativeRequestState !== `none`) {
this.authoritativeRequestState = `complete`;
this.authoritativeRequestKind = void 0;
this.retireSettledFiniteAcquisitions();
}
}
retireSettledFiniteAcquisitions(prefix, retained) {
if (this.authoritativeRequestState !== `complete` && !prefix && !retained || this.subscription.hasPendingTruncateReplacement)
return;
const orderedLoadGeneration = this.orderedLoadGeneration;
callbacks.runAllCallbacks(
Array.from(this.settledFiniteAcquisitions, ([release, count]) => () => {
if (retained?.has(release)) return;
if (!this.active || orderedLoadGeneration !== this.orderedLoadGeneration)
return;
if (this.authoritativeRequestState !== `complete` && !retained && (!prefix || release === prefix.release || count === void 0 || count > prefix.count))
return;
this.settledFiniteAcquisitions.delete(release);
release();
})
);
}
invalidateCursor() {
if (!this.stagedContinuation?.isAuthoritativeRepair) {
this.stagedContinuation = void 0;
}
this.lastPage = void 0;
this.lastPrefixCount = void 0;
}
invalidateSourceOrdering() {
this.orderingInvalidationGeneration++;
this.invalidateCursor();
this.lastBoundary = void 0;
if (!this.needsOrderingRepair && this.hasSettledSourceRequest && this.pending === void 0 && this.authoritativeRequestState === `none`) {
this.canRepairWithOrderedPrefix = true;
}
this.settledSourceBoundary = void 0;
this.needsOrderingRepair = true;
}
finishOrderedPrefixRepair(windowOperationGeneration) {
if (this.authoritativeRequestKind !== `ordered-repair`) return;
if (this.orderedPrefixRepairGeneration !== this.orderingInvalidationGeneration) {
this.orderedRepairAcquisitions = /* @__PURE__ */ new Set();
this.authoritativeRequestState = `none`;
this.authoritativeRequestKind = void 0;
this.orderedPrefixRepairGeneration = void 0;
if (!this.active) return;
if (this.failedRequest) {
this.canRepairWithOrderedPrefix = false;
this.loadFullSource(windowOperationGeneration);
} else {
this.canRepairWithOrderedPrefix = true;
this.loadOrderedPrefixRepair(windowOperationGeneration);
}
return;
}
const retained = this.orderedRepairAcquisitions;
try {
this.retireSettledFiniteAcquisitions(void 0, retained);
} finally {
const hasSupersededAcquisition = [
...this.settledFiniteAcquisitions.keys()
].some((release) => !retained.has(release));
if (!this.active || !hasSupersededAcquisition) {
this.orderedRepairAcquisitions = /* @__PURE__ */ new Set();
this.cancelRepairRetry();
this.repairRetries = 0;
this.canRepairWithOrderedPrefix = false;
this.needsOrderingRepair = false;
this.authoritativeRequestState = `none`;
this.authoritativeRequestKind = void 0;
this.orderedPrefixRepairGeneration = void 0;
}
}
}
abandonOrderedPrefixRepair() {
if (this.authoritativeRequestKind !== `ordered-repair`) return;
this.orderedRepairAcquisitions = /* @__PURE__ */ new Set();
this.canRepairWithOrderedPrefix = false;
this.authoritativeRequestState = `none`;
this.authoritativeRequestKind = void 0;
this.orderedPrefixRepairGeneration = void 0;
}
dispose() {
this.active = false;
this.stagedContinuation = void 0;
this.resetCursor();
this.failedAcquisitions.clear();
this.settledFiniteAcquisitions.clear();
}
countAcquiredRows() {
return this.subscription.readOrderedSnapshot({
orderBy: expressions.normalizeOrderByPaths(this.info.orderBy, this.alias),
limit: this.info.offset + this.info.limit
}).filter(
({ value }) => this.info.comparator(value, this.settledSourceBoundary) <= 0
).length;
}
loadPage(count, windowOperationGeneration, continuesOrderedPrefixRepair = false) {
if (!this.active || this.pending || this.stagedContinuation) return;
const startsFromSourcePrefix = this.settledSourceBoundary === void 0;
const biggest = this.settledSourceBoundary;
let minValues;
if (biggest !== void 0) {
const value = this.info.valueExtractorForRawRow(biggest);
if (!cursor.canExpressCursorOrder(this.info.orderBy, [value])) {
this.loadPrefix(
this.info.offset + this.info.limit,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
return;
}
minValues = [value];
}
const boundary = minValues?.[0];
if (this.lastPage?.count === count && Object.is(this.lastPage.boundary, boundary)) {
if (continuesOrderedPrefixRepair) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
}
return;
}
this.lastPage = { count, boundary };
this.requestAndObserve(
(onLoadSubsetResult) => {
this.subscription.requestLimitedSnapshot({
...continuesOrderedPrefixRepair ? { refetch: true } : {},
orderBy: expressions.normalizeOrderByPaths(this.info.orderBy, this.alias),
limit: count,
minValues,
// Local rows seen before the first provider request prove neither
// a cursor nor a remote offset. Start the first acquisition at zero.
offset: startsFromSourcePrefix ? 0 : this.countAcquiredRows(),
trackLoadSubsetPromise: false,
onLoadSubsetResult
});
},
`ordered`,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
}
observe(result, releaseAcquisition, kind, windowOperationGeneration, options, continuesOrderedPrefixRepair = false, requestGraphInputRevision) {
const isFullSource = kind === `full-source`;
const isOrderedRepair = kind === `ordered-repair`;
const isAuthoritativeRepair = isFullSource || isOrderedRepair || continuesOrderedPrefixRepair;
const retryRepair = isAuthoritativeRepair && this.hasSettledSourceRequest && this.needsOrderingRepair && windowOperationGeneration === void 0;
const orderedLoadGeneration = this.orderedLoadGeneration;
const orderingInvalidationGeneration = this.orderingInvalidationGeneration;
const settlesAsync = result instanceof Promise;
const canSettleSynchronously = !settlesAsync && windowOperationGeneration === void 0 && !isAuthoritativeRepair && requestGraphInputRevision !== void 0;
let synchronousCompletionFailure;
const continuation = (continuationKind) => ({
kind: continuationKind,
orderedLoadGeneration,
orderingInvalidationGeneration,
isAuthoritativeRepair,
windowOperationGeneration,
continuesOrderedPrefixRepair
});
const continueOrStage = (next, deferContinuation) => {
if (deferContinuation) {
this.stagedContinuation = next;
} else {
this.runContinuation(next);
}
};
const complete = (trackedRequest, deferContinuation = false) => {
if (trackedRequest && this.pending === trackedRequest) {
this.pending = void 0;
}
if (!this.active) return;
callbacks.runAllCallbacks([
() => {
if (!isFullSource) {
const prefixCount = options?.orderBy && !options.cursor ? options.limit : void 0;
this.settledFiniteAcquisitions.set(releaseAcquisition, prefixCount);
if (isOrderedRepair || continuesOrderedPrefixRepair) {
this.orderedRepairAcquisitions.add(releaseAcquisition);
}
if (!continuesOrderedPrefixRepair) {
this.retireSettledFiniteAcquisitions();
}
if (!continuesOrderedPrefixRepair && orderedLoadGeneration === this.orderedLoadGeneration && prefixCount !== void 0) {
this.retireSettledFiniteAcquisitions({
release: releaseAcquisition,
count: prefixCount
});
}
}
},
() => {
if (orderedLoadGeneration !== this.orderedLoadGeneration) return;
if (!isAuthoritativeRepair && orderingInvalidationGeneration !== this.orderingInvalidationGeneration)
return;
if (!isAuthoritativeRepair && (this.failedRequest || this.authoritativeRequestState !== `none`))
return;
if ((isOrderedRepair || continuesOrderedPrefixRepair) && this.failedRequest) {
this.abandonOrderedPrefixRepair();
return;
}
this.failedRequest = void 0;
if (kind !== `boundary`) {
this.hasSettledSourceRequest = true;
if (options?.orderBy && !options.cursor) {
this.lastPrefixCount = options.limit;
}
if (!isFullSource && options?.orderBy) {
try {
this.settledSourceBoundary = this.subscription.readOrderedSnapshot(options).at(-1)?.value ?? this.settledSourceBoundary;
} catch (error2) {
if (canSettleSynchronously) {
synchronousCompletionFailure = { error: error2 };
throw error2;
}
fail(error2);
}
}
}
if ((isOrderedRepair || continuesOrderedPrefixRepair) && this.orderedPrefixRepairGeneration !== this.orderingInvalidationGeneration) {
this.finishOrderedPrefixRepair(windowOperationGeneration);
return;
}
if (isFullSource) {
this.cancelRepairRetry();
this.repairRetries = 0;
this.canRepairWithOrderedPrefix = false;
this.needsOrderingRepair = false;
this.authoritativeRequestState = `complete`;
this.authoritativeRequestKind = void 0;
this.orderedPrefixRepairGeneration = void 0;
this.retireSettledFiniteAcquisitions();
}
if (isOrderedRepair || kind === `ordered`) {
continueOrStage(continuation(`boundary`), deferContinuation);
return;
}
continueOrStage(continuation(`more`), deferContinuation);
}
]);
};
const fail = (error2, trackedRequest) => {
this.settledFiniteAcquisitions.delete(releaseAcquisition);
this.stagedContinuation = void 0;
if (trackedRequest && this.pending === trackedRequest) {
this.pending = void 0;
}
if (!this.active) return;
this.requireFullSourceRepair();
if (orderedLoadGeneration !== this.orderedLoadGeneration) return;
if (isAuthoritativeRepair) {
if (isFullSource) {
this.authoritativeRequestState = `failed`;
} else {
this.authoritativeRequestState = `none`;
this.authoritativeRequestKind = void 0;
this.orderedPrefixRepairGeneration = void 0;
}
}
this.recordRequestFailure(windowOperationGeneration);
this.failedAcquisitions.set(releaseAcquisition, kind);
if (retryRepair) this.scheduleRepairRetry();
throw error2;
};
if (canSettleSynchronously) {
try {
callbacks.runAllCallbacks([
() => complete(void 0, true),
() => {
if (!synchronousCompletionFailure) {
this.onResult(true, false, false);
}
},
() => {
if (!synchronousCompletionFailure && this.getGraphInputRevision?.() === requestGraphInputRevision) {
this.drainNoInputContinuations(requestGraphInputRevision);
}
}
]);
} catch (error2) {
if (synchronousCompletionFailure) {
throw synchronousCompletionFailure.error;
}
throw new OrderedPostSettlementError(error2);
}
return this.pending;
}
const request = settlesAsync ? result : Promise.resolve();
const tracked = request.then(
() => complete(tracked),
(error2) => fail(error2, tracked)
);
this.pending = tracked;
void tracked.catch(() => {
});
this.onResult(
tracked,
isAuthoritativeRepair && this.needsOrderingRepair,
settlesAsync
);
return tracked;
}
runContinuation(continuation, windowOperationGeneration = continuation.windowOperationGeneration) {
if (!this.active || continuation.orderedLoadGeneration !== this.orderedLoadGeneration || !continuation.isAuthoritativeRepair && continuation.orderingInvalidationGeneration !== this.orderingInvalidationGeneration)
return;
if (continuation.kind === `boundary`) {
this.loadBoundary(
windowOperationGeneration,
continuation.continuesOrderedPrefixRepair
);
} else {
if (this.info.joinedFilterSourceId && this.hasPendingJoinedWork()) {
this.stagedContinuation = continuation;
return;
}
this.loadMore(
windowOperationGeneration,
continuation.continuesOrderedPrefixRepair
);
}
}
consumeStagedContinuation(windowOperationGeneration) {
const continuation = this.stagedContinuation;
if (!continuation) return;
this.stagedContinuation = void 0;
this.runContinuation(continuation, windowOperationGeneration);
}
drainNoInputContinuations(graphInputRevision) {
if (this.drainingNoInputContinuations) return;
this.drainingNoInputContinuations = true;
try {
while (this.stagedContinuation && !this.pending && this.getGraphInputRevision?.() === graphInputRevision) {
this.consumeStagedContinuation();
}
} finally {
this.drainingNoInputContinuations = false;
}
}
loadBoundary(windowOperationGeneration, continuesOrderedPrefixRepair = false) {
const biggest = this.settledSourceBoundary;
if (biggest === void 0) {
return continuesOrderedPrefixRepair ? this.loadMore(windowOperationGeneration, true) : void 0;
}
const value = this.info.valueExtractorForRawRow(biggest);
const orderBy = expressions.normalizeOrderByPaths(this.info.orderBy, this.alias);
if (!cursor.canExpressCursorOrder(orderBy.slice(0, 1), [value])) {
if (continuesOrderedPrefixRepair) this.abandonOrderedPrefixRepair();
this.loadFullSource(windowOperationGeneration);
return this.pending;
}
if (Object.is(this.lastBoundary, value)) {
return this.loadMore(
windowOperationGeneration,
continuesOrderedPrefixRepair
);
}
const where = cursor.buildCursorCurrent(orderBy, [value]);
if (!where) {
if (continuesOrderedPrefixRepair) this.abandonOrderedPrefixRepair();
this.loadFullSource(windowOperationGeneration);
return this.pending;
}
this.lastBoundary = value;
return this.requestAndObserve(
(onLoadSubsetResult) => {
this.subscription.requestSnapshot({
...continuesOrderedPrefixRepair ? { refetch: true } : {},
where,
trackLoadSubsetPromise: false,
onLoadSubsetResult
});
},
`boundary`,
windowOperationGeneration,
continuesOrderedPrefixRepair
);
}
requireFullSourceRepair() {
this.settledSourceBoundary = void 0;
this.canRepairWithOrderedPrefix = false;
this.orderedRepairAcquisitions.clear();
this.orderedPrefixRepairGeneration = void 0;
this.needsOrderingRepair = true;
}
cancelRepairRetry() {
clearTimeout(this.repairTimer);
this.repairTimer = void 0;
}
releaseFailedAcquisitions() {
const failed = this.failedAcquisitions;
this.failedAcquisitions = /* @__PURE__ */ new Map();
this.requesting = true;
try {
callbacks.runAllCallbacks(failed.keys());
} finally {
this.requesting = false;
}
}
scheduleRepairRetry() {
if (!this.active || !this.canRetryRepair() || this.repairTimer !== void 0 || this.repairRetries >= 2)
return;
const orderedLoadGeneration = this.orderedLoadGeneration;
const failedRequest = this.failedRequest;
this.repairTimer = setTimeout(
() => {
this.repairTimer = void 0;
const retry = Promise.resolve().then(() => {
if (!this.active || !this.canRetryRepair() || orderedLoadGeneration !== this.orderedLoadGeneration || this.failedRequest !== failedRequest || this.failedRequest?.windowOperationGeneration !== void 0)
return;
this.releaseFailedAcquisitions();
if (
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- release callbacks can dispose the loader
!this.active || orderedLoadGeneration !== this.orderedLoadGeneration || this.subscription.hasPendingTruncateReplacement
)
return;
this.failedRequest = void 0;
this.authoritativeRequestState = `none`;
this.loadFullSource();
});
void retry.catch(() => {
});
this.onResult(retry, true, true);
},
250 * 2 ** this.repairRetries++
);
}
failRequest(observed, error2, isAuthoritativeRepair, windowOperationGeneration, cancelObservedSettlement = false) {
if (cancelObservedSettlement) {
this.orderedLoadGeneration++;
this.pending = void 0;
}
this.requireFullSourceRepair();
this.recordRequestFailure(windowOperationGeneration);
if (isAuthoritativeRepair) {
this.authoritativeRequestState = `none`;
this.authoritativeRequestKind = void 0;
}
try {
observed?.release({ error: error2 });
} catch {
}
if (isAuthoritativeRepair && this.hasSettledSourceRequest && windowOperationGeneration === void 0) {
this.scheduleRepairRetry();
}
return error2;
}
/** A failed request blocks ordinary refinement until a new operation. */
recordRequestFailure(windowOperationGeneration) {
this.failedRequest = { windowOperationGeneration };
this.stagedContinuation = void 0;
this.blockedLoadMore = void 0;
this.invalidateCursor();
this.lastBoundary = void 0;
}
/** Observe settlement only after all synchronous request work succeeds. */
requestAndObserve(request, kind, windowOperationGeneration, continuesOrderedPrefixRepair = false) {
const isFullSource = kind === `full-source`;
const isAuthoritativeRepair = isFullSource || kind === `ordered-repair` || continuesOrderedPrefixRepair;
let observed;
this.requesting = true;
let observing = false;
const graphInputRevision = this.getGraphInputRevision?.();
try {
try {
request((result, options, release) => {
observed = { result, options, release };
});
} finally {
this.requesting = false;
}
if (!observed) return;
observing = true;
return this.observe(
observed.result,
observed.release,
kind,
windowOperationGeneration,
observed.options,
continuesOrderedPrefixRepair,
graphInputRevision
);
} catch (error$1) {
if (error$1 instanceof OrderedPostSettlementError) throw error$1.cause;
this.requesting = true;
try {
const failure = this.failRequest(
observed,
error.normalizeError(error$1),
isAuthoritativeRepair,
windowOperationGeneration,
observing
);
if (!observing && isAuthoritativeRepair && this.hasSettledSourceRequest) {
const rejected = Promise.reject(failure);
void rejected.catch(() => {
});
this.onResult(rejected, true, true);
}
throw failure;
} finally {
this.requesting = false;
}
}
}
}
exports.OrderedSourceLoader = OrderedSourceLoader;
//# sourceMappingURL=ordered-source-loader.cjs.map