UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines 68.6 kB
{"version":3,"file":"collection-config-builder.cjs","sources":["../../../../src/query/live/collection-config-builder.ts"],"sourcesContent":["import { D2, output } from '@tanstack/db-ivm'\nimport { compileQuery } from '../compiler/index.js'\nimport {\n  MissingAliasInputsError,\n  SetWindowReentrancyError,\n  SetWindowRequiresOrderByError,\n} from '../../errors.js'\nimport {\n  getActivePublicationContext,\n  transactionScopedScheduler,\n  withPublicationContext,\n} from '../../scheduler.js'\nimport { getActiveTransaction } from '../../transactions.js'\nimport { deepEquals } from '../../utils.js'\nimport { runAllCallbacks } from '../../utils/callbacks.js'\nimport { normalizeError } from '../../utils/error.js'\nimport { CollectionSubscriber } from './collection-subscriber.js'\nimport { getCollectionBuilder } from './collection-registry.js'\nimport { LIVE_QUERY_INTERNAL } from './internal.js'\nimport { materializeCompilation } from './materialized-pipeline.js'\nimport { BucketFacadeAdapter } from './bucket-facade-adapter.js'\nimport {\n  buildQueryFromConfig,\n  extractCollectionFromSource,\n  extractCollectionSources,\n  extractCollectionsFromQuery,\n} from './utils.js'\nimport type { LiveQueryInternalUtils } from './internal.js'\nimport type { WindowOptions } from '../compiler/index.js'\nimport type { SchedulerContextId } from '../../scheduler.js'\nimport type { CollectionSubscription } from '../../collection/subscription.js'\nimport type { RootStreamBuilder } from '@tanstack/db-ivm'\nimport type { OrderByOptimizationInfo } from '../compiler/order-by.js'\nimport type { Collection } from '../../collection/index.js'\nimport type {\n  CollectionConfigSingleRowOption,\n  KeyedStream,\n  ResultStream,\n  StringCollationConfig,\n  SyncConfig,\n  UtilsRecord,\n} from '../../types.js'\nimport type { Context, GetResult } from '../builder/types.js'\nimport type { BasicExpression, QueryIR } from '../ir.js'\nimport type { LazyCollectionCallbacks } from '../compiler/joins.js'\nimport type {\n  Changes,\n  FullSyncState,\n  LiveQueryCollectionConfig,\n  SyncState,\n} from './types.js'\nimport type { AllCollectionEvents } from '../../collection/events.js'\n\nexport type LiveQueryCollectionUtils = UtilsRecord & {\n  /** Most recent subset-load failure observed by this live query. */\n  readonly lastSubsetError: unknown | undefined\n  /**\n   * Sets the offset and limit of an ordered query.\n   * Is a no-op if the query is not ordered.\n   *\n   * @returns `true` if no subset loading was triggered, or `Promise<void>` that resolves when the subset has been loaded\n   */\n  setWindow: (options: WindowOptions) => true | Promise<void>\n  /**\n   * Gets the current window (offset and limit) for an ordered query.\n   *\n   * @returns The current window settings, or `undefined` if the query is not windowed\n   */\n  getWindow: () => { offset: number; limit: number } | undefined\n  [LIVE_QUERY_INTERNAL]: LiveQueryInternalUtils\n}\n\ntype PendingGraphRun = {\n  syncSession: number\n  loadCallbacks: Set<() => void>\n}\n\n// Global counter for auto-generated collection IDs\nlet liveQueryCollectionCounter = 0\n\ntype SyncMethods<TResult extends object> = Parameters<\n  SyncConfig<TResult>[`sync`]\n>[0]\n\nexport class CollectionConfigBuilder<\n  TContext extends Context,\n  TResult extends object = GetResult<TContext>,\n> {\n  private readonly id: string\n  readonly query: QueryIR\n  private readonly collections: Record<string, Collection<any, any, any>>\n  private readonly collectionSources: ReturnType<\n    typeof extractCollectionSources\n  >\n\n  // WeakMap to store the keys of the results\n  // so that we can retrieve them in the getKey function\n  private readonly resultKeys = new WeakMap<object, unknown>()\n\n  // WeakMap to store the orderBy index for each result\n  private readonly orderByIndices = new WeakMap<object, string>()\n\n  private readonly compare?: (val1: TResult, val2: TResult) => number\n  private readonly compareOptions?: StringCollationConfig\n\n  private isGraphRunning = false\n\n  // Current sync session state (set when sync starts, cleared when it stops)\n  // Public for testing purposes (CollectionConfigBuilder is internal, not public API)\n  public currentSyncConfig:\n    | Parameters<SyncConfig<TResult>[`sync`]>[0]\n    | undefined\n  public currentSyncState: FullSyncState | undefined\n\n  // Error state tracking\n  private isInErrorState = false\n  private fatalQueryError = false\n  private readonly erroredSourceIds = new Set<string>()\n  private lastSubsetError: unknown | undefined\n\n  // Reference to the live query collection for error state transitions\n  public liveQueryCollection?: Collection<TResult, any, any>\n\n  private windowFn: ((options: WindowOptions) => void) | undefined\n  private readonly initialWindow: WindowOptions | undefined\n  private currentWindow: WindowOptions | undefined\n  private settledWindow: WindowOptions | undefined\n  private activeWindowOperation:\n    | { generation: number; failed: boolean; error?: unknown }\n    | undefined\n\n  private maybeRunGraphFn: (() => void) | undefined\n  private readonly builderDependencies = new Set<\n    CollectionConfigBuilder<any, any>\n  >()\n\n  // Pending graph runs per scheduler context (e.g., per transaction)\n  // The builder manages its own state; the scheduler just orchestrates execution order\n  // Only stores callbacks - if sync ends, pending jobs gracefully no-op\n  private readonly pendingGraphRuns = new Map<\n    SchedulerContextId,\n    PendingGraphRun\n  >()\n\n  // Unsubscribe function for scheduler's onClear listener\n  // Registered when sync starts, unregistered when sync stops\n  // Prevents memory leaks by releasing the scheduler's reference to this builder\n  private unsubscribeFromSchedulerClears?: () => void\n\n  private graphCache: D2 | undefined\n  private inputsCache: Record<string, RootStreamBuilder<unknown>> | undefined\n  private pipelineCache: ResultStream | undefined\n  public sourceWhereClausesCache:\n    | Map<string, BasicExpression<boolean>>\n    | undefined\n  private bucketFacadesCache:\n    | ReturnType<typeof materializeCompilation>[`facades`]\n    | undefined\n\n  // Map of opaque source ID to subscription\n  readonly subscriptions: Record<string, CollectionSubscription> = {}\n  // Map of opaque source ID to demand callbacks for that lazy source\n  lazySourcesCallbacks: Record<string, LazyCollectionCallbacks> = {}\n  // Set of opaque source IDs that are lazy (don't load initial state)\n  readonly lazySources = new Set<string>()\n  private readonly activeDemands = new Map<\n    string,\n    {\n      generation: number\n      settled: boolean\n    }\n  >()\n  private readonly demandGenerations = new Map<string, number>()\n  private readonly pendingOrderedLoads = new Set<Promise<unknown>>()\n  private orderedLoadFailed = false\n  // Source replay cannot settle a failed imperative window operation.\n  private windowFailed = false\n  private syncSession = 0\n  private windowOperationGeneration = 0\n  // Map of lexical source IDs to optimizable ORDER BY state\n  optimizableOrderByCollections: Record<string, OrderByOptimizationInfo> = {}\n\n  constructor(\n    private readonly config: LiveQueryCollectionConfig<TContext, TResult>,\n  ) {\n    // Generate a unique ID if not provided\n    this.id = config.id || `live-query-${++liveQueryCollectionCounter}`\n\n    this.query = buildQueryFromConfig({\n      query: config.query,\n      requireObjectResult: true,\n    })\n    this.initialWindow = this.query.orderBy?.length\n      ? {\n          offset: this.query.offset ?? 0,\n          limit: this.query.limit ?? Infinity,\n        }\n      : undefined\n    this.settledWindow = this.initialWindow\n    this.collections = extractCollectionsFromQuery(this.query)\n    this.collectionSources = extractCollectionSources(this.query)\n\n    // Create compare function for ordering if the query has orderBy\n    if (this.query.orderBy && this.query.orderBy.length > 0) {\n      this.compare = createOrderByComparator<TResult>(this.orderByIndices)\n    }\n\n    // Use explicitly provided compareOptions if available, otherwise inherit from FROM collection\n    this.compareOptions =\n      this.config.defaultStringCollation ??\n      extractCollectionFromSource(this.query).compareOptions\n\n    // Compile the base pipeline once initially\n    // This is done to ensure that any errors are thrown immediately and synchronously\n    this.compileBasePipeline()\n  }\n\n  /**\n   * Recursively checks if a query or any of its subqueries contains joins\n   */\n  private hasJoins(query: QueryIR): boolean {\n    // Check if this query has joins\n    if (query.join && query.join.length > 0) {\n      return true\n    }\n\n    // Recursively check subqueries in the from clause\n    if (query.from.type === `queryRef`) {\n      if (this.hasJoins(query.from.query)) {\n        return true\n      }\n    } else if (query.from.type === `unionFrom`) {\n      for (const source of query.from.sources) {\n        if (source.type === `queryRef` && this.hasJoins(source.query)) {\n          return true\n        }\n      }\n    } else if (query.from.type === `unionAll`) {\n      for (const branch of query.from.queries) {\n        if (this.hasJoins(branch)) {\n          return true\n        }\n      }\n    }\n\n    return false\n  }\n\n  getConfig(): CollectionConfigSingleRowOption<TResult> & {\n    utils: LiveQueryCollectionUtils\n  } {\n    const builder = this\n    return {\n      id: this.id,\n      getKey:\n        this.config.getKey ||\n        ((item: any) =>\n          (this.resultKeys.get(item) ?? item.$key) as string | number),\n      sync: this.getSyncConfig(),\n      compare: this.compare,\n      defaultStringCollation: this.compareOptions,\n      gcTime: this.config.gcTime ?? 5000, // 5 seconds by default for live queries\n      schema: this.config.schema,\n      onInsert: this.config.onInsert,\n      onUpdate: this.config.onUpdate,\n      onDelete: this.config.onDelete,\n      startSync: this.config.startSync,\n      singleResult: this.query.singleResult,\n      utils: {\n        get lastSubsetError() {\n          return builder.lastSubsetError\n        },\n        setWindow: this.setWindow.bind(this),\n        getWindow: this.getWindow.bind(this),\n        [LIVE_QUERY_INTERNAL]: {\n          getBuilder: () => this,\n          hasCustomGetKey: !!this.config.getKey,\n          hasJoins: this.hasJoins(this.query),\n          hasDistinct: !!this.query.distinct,\n        },\n      },\n    }\n  }\n\n  setWindow(options: WindowOptions): true | Promise<void> {\n    const windowFn = this.windowFn\n    if (!windowFn) {\n      throw new SetWindowRequiresOrderByError()\n    }\n    if (\n      this.activeWindowOperation ||\n      this.isGraphRunning ||\n      Object.values(this.optimizableOrderByCollections).some((info) =>\n        info.isRequesting?.(),\n      )\n    ) {\n      throw new SetWindowReentrancyError()\n    }\n\n    // Keep caller-owned objects out of the long-lived query state. A caller may\n    // reuse and mutate its options object after this operation settles.\n    const baseWindow =\n      this.currentWindow ?? this.settledWindow ?? this.initialWindow\n    const requestedWindow: WindowOptions = {\n      offset: options.offset ?? baseWindow?.offset,\n      limit: options.limit ?? baseWindow?.limit,\n    }\n    const sourceRecovery = this.pendingSourceRecovery()\n    if (sourceRecovery) {\n      return sourceRecovery.then(async () => {\n        const settlement = this.setWindow(requestedWindow)\n        if (settlement !== true) await settlement\n      })\n    }\n    if (this.hasFailedSourceRecovery()) {\n      return Promise.reject(\n        this.lastSubsetError ?? new Error(`Source recovery failed`),\n      )\n    }\n    const windowOperationGeneration = ++this.windowOperationGeneration\n    const loadOperation =\n      this.liveQueryCollection?._sync.beginLoadSubsetOperation()\n    const previousOperation = this.activeWindowOperation\n    const operation: {\n      generation: number\n      failed: boolean\n      error?: unknown\n    } = { generation: windowOperationGeneration, failed: false }\n    this.activeWindowOperation = operation\n    this.windowFailed = false\n    if (this.pendingOrderedLoads.size === 0) this.orderedLoadFailed = false\n    try {\n      // The window and all source work it causes form one synchronous\n      // publication. This makes operation tracking see requests scheduled by\n      // the graph rather than declaring the window settled too early.\n      this.currentWindow = requestedWindow\n      withPublicationContext(() => {\n        windowFn(requestedWindow)\n        this.maybeRunGraphFn?.()\n      })\n      if (operation.failed) throw operation.error\n    } catch (error) {\n      if (windowOperationGeneration === this.windowOperationGeneration) {\n        this.windowFailed = true\n        this.currentWindow = this.settledWindow\n      }\n      loadOperation?.cancel()\n      throw error\n    } finally {\n      this.activeWindowOperation = previousOperation\n    }\n\n    const settlement = loadOperation?.wait() ?? true\n    if (settlement === true) {\n      this.settledWindow = requestedWindow\n      return true\n    }\n    return settlement.then(\n      () => {\n        if (windowOperationGeneration === this.windowOperationGeneration) {\n          this.settledWindow = requestedWindow\n        }\n      },\n      (error) => {\n        if (windowOperationGeneration === this.windowOperationGeneration) {\n          this.windowFailed = true\n          this.currentWindow = this.settledWindow\n        }\n        throw error\n      },\n    )\n  }\n\n  getWindow(): { offset: number; limit: number } | undefined {\n    // Only return window if this is a windowed query (has orderBy and windowFn)\n    const window = this.settledWindow ?? this.initialWindow\n    if (!this.windowFn || !window) {\n      return undefined\n    }\n    return {\n      offset: window.offset ?? 0,\n      limit: window.limit ?? 0,\n    }\n  }\n\n  isLazySource(sourceId: string): boolean {\n    return this.lazySources.has(sourceId)\n  }\n\n  beginDemand(planId: string): number {\n    const generation = (this.demandGenerations.get(planId) ?? 0) + 1\n    this.demandGenerations.set(planId, generation)\n    this.activeDemands.set(planId, {\n      generation,\n      settled: false,\n    })\n    return generation\n  }\n\n  settleDemand(planId: string, generation: number): void {\n    const demand = this.activeDemands.get(planId)\n    if (!demand || demand.generation !== generation || demand.settled) return\n    demand.settled = true\n    this.maybeRunGraphFn?.()\n  }\n\n  failDemand(planId: string, generation: number, error: unknown): void {\n    const demand = this.activeDemands.get(planId)\n    if (!demand || demand.generation !== generation) return\n    const normalized = this.recordSubsetError(error)\n    this.transitionToError(\n      `Subset demand '${planId}' failed: ${normalized.message}`,\n      normalized,\n    )\n  }\n\n  recordSubsetError(error: unknown, fatalBeforeReady = false): Error {\n    const normalized = normalizeError(error)\n    this.lastSubsetError = normalized\n    if (this.activeWindowOperation) {\n      this.activeWindowOperation.failed = true\n      this.activeWindowOperation.error = normalized\n      // A synchronous adapter failure can arrive before it returns a promise\n      // for the ordered-load tracker. Keep any private graph changes hidden.\n      this.orderedLoadFailed = true\n    }\n    if (fatalBeforeReady) {\n      this.transitionToError(\n        `Initial subset load failed: ${normalized.message}`,\n        normalized,\n      )\n    }\n    return normalized\n  }\n\n  trackSubsetLoadPromise(promise: Promise<unknown>): void {\n    this.liveQueryCollection!._sync.trackLoadPromise(promise)\n  }\n\n  trackSubsetLoadOperationPromise(promise: Promise<unknown>): void {\n    this.liveQueryCollection!._sync.trackLoadSubsetOperationPromise(promise)\n  }\n\n  hasActiveWindowOperation(): boolean {\n    return this.activeWindowOperation !== undefined\n  }\n\n  getActiveWindowOperationGeneration(): number | undefined {\n    return this.activeWindowOperation?.generation\n  }\n\n  scheduleGraphRunForSession(syncSession: number): void {\n    if (\n      syncSession !== this.syncSession ||\n      !this.currentSyncConfig ||\n      !this.currentSyncState\n    ) {\n      return\n    }\n    this.scheduleGraphRun()\n  }\n\n  trackOrderedLoadPromise(\n    promise: Promise<unknown>,\n    holdPublication = false,\n  ): void {\n    // Hold the last complete public snapshot during an initial load or an\n    // imperative window move. Source changes that arrive during the move join\n    // its private graph state and publish with the completed replacement.\n    if (\n      !holdPublication &&\n      !this.activeWindowOperation &&\n      this.liveQueryCollection?.status !== `loading` &&\n      this.pendingOrderedLoads.size === 0\n    ) {\n      return\n    }\n    const syncSession = this.syncSession\n    if (this.pendingOrderedLoads.size === 0) this.orderedLoadFailed = false\n    this.pendingOrderedLoads.add(promise)\n    const finish = (succeeded: boolean) => {\n      // Admission precedes mutation: cleanup retires this session's participants.\n      if (\n        syncSession !== this.syncSession ||\n        !this.pendingOrderedLoads.delete(promise)\n      ) {\n        return\n      }\n      if (!succeeded) this.orderedLoadFailed = true\n      if (!this.orderedLoadFailed && this.pendingOrderedLoads.size === 0) {\n        // The ordered chain already drove its source graph to quiescence.\n        // Flush the retained result without invoking the source loaders again.\n        this.scheduleGraphRun()\n      }\n    }\n    void promise.then(\n      () => finish(true),\n      () => finish(false),\n    )\n  }\n\n  retireDemand(planId: string): void {\n    this.activeDemands.delete(planId)\n  }\n\n  hasPendingSourceRecovery(): boolean {\n    return Object.values(this.subscriptions).some(\n      (subscription) => subscription.hasPendingTruncateReplacement,\n    )\n  }\n\n  private pendingSourceRecovery(): Promise<void> | undefined {\n    const pending = Object.values(this.subscriptions).flatMap((subscription) =>\n      subscription.pendingTruncateReplacement\n        ? [subscription.pendingTruncateReplacement]\n        : [],\n    )\n    return pending.length > 0\n      ? Promise.all(pending).then(() => undefined)\n      : undefined\n  }\n\n  private hasFailedSourceRecovery(): boolean {\n    return Object.values(this.subscriptions).some(\n      (subscription) => subscription.hasFailedTruncateReplacement,\n    )\n  }\n\n  getSyncSession(): number {\n    return this.syncSession\n  }\n\n  // The callback function is called after the graph has run.\n  // This gives the callback a chance to load more data if needed,\n  // that's used to optimize orderBy operators that set a limit,\n  // in order to load some more data if we still don't have enough rows after the pipeline has run.\n  // That can happen because even though we load N rows, the pipeline might filter some of these rows out\n  // causing the orderBy operator to receive less than N rows or even no rows at all.\n  // So this callback would notice that it doesn't have enough rows and load some more.\n  // Readiness follows source/demand state, not the callback's return value.\n  maybeRunGraph(callback?: () => void) {\n    if (this.isGraphRunning) {\n      // no nested runs of the graph\n      // which is possible if the `callback`\n      // would call `maybeRunGraph` e.g. after it has loaded some more data\n      return\n    }\n\n    // Should only be called when sync is active\n    if (!this.currentSyncConfig || !this.currentSyncState) {\n      throw new Error(\n        `maybeRunGraph called without active sync session. This should not happen.`,\n      )\n    }\n\n    this.isGraphRunning = true\n\n    try {\n      const syncSession = this.syncSession\n      const config = this.currentSyncConfig\n      const { begin, commit } = config\n      const syncState = this.currentSyncState\n      const isCurrentSession = () =>\n        syncSession === this.syncSession &&\n        this.currentSyncConfig === config &&\n        this.currentSyncState === syncState\n\n      // Don't run if the live query is in an error state\n      if (this.isInErrorState) {\n        return\n      }\n\n      // Always run the graph if subscribed (eager execution)\n      if (syncState.subscribedToAllCollections) {\n        let callbackCalled = false\n        const drainGraph = () => {\n          while (syncState.graph.pendingWork()) {\n            try {\n              syncState.graph.run()\n            } catch (error) {\n              if (isCurrentSession()) {\n                this.transitionToError(`Live query graph failed`, error)\n              }\n              throw error\n            }\n            if (!isCurrentSession()) return false\n            callback?.()\n            if (!isCurrentSession()) return false\n            callbackCalled = true\n          }\n          return true\n        }\n\n        if (!drainGraph()) return\n\n        // Ensure the callback runs at least once even when the graph has no pending work.\n        // This handles lazy loading scenarios where setWindow() increases the limit or\n        // an async loadSubset completes and we need to re-check if more data is needed.\n        // drainGraph changes this flag inside its closure.\n        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n        if (!callbackCalled) {\n          callback?.()\n          if (!isCurrentSession()) return\n        }\n\n        // A synchronous loader can write while this graph run is active. Its\n        // nested schedule is intentionally coalesced, so drain that new input\n        // here before publishing the transaction.\n        if (!drainGraph()) return\n\n        // Publish only after every operator has reached quiescence. A source\n        // change can reach sibling materializations in different graph steps;\n        // flushing between those steps would expose a mixed root snapshot.\n        syncState.flushPendingChanges?.()\n        if (!isCurrentSession()) return\n\n        // On the initial run, we may need to do an empty commit to ensure that\n        // the collection is initialized\n        if (syncState.messagesCount === 0) {\n          begin()\n          commit()\n        }\n\n        // After graph processing completes, check if we should mark ready.\n        // This is the canonical place to transition to ready state because:\n        // 1. All data has been processed through the graph\n        // 2. All source collections have had a chance to send their initial data\n        // This prevents marking ready before data is processed (fixes isReady=true with empty data)\n        this.updateLiveQueryStatus(config)\n      }\n    } finally {\n      this.isGraphRunning = false\n    }\n  }\n\n  /**\n   * Schedules a graph run with the transaction-scoped scheduler.\n   * Ensures each builder runs at most once per transaction, with automatic dependency tracking\n   * to run parent queries before child queries. Outside a transaction, runs immediately.\n   *\n   * Multiple calls during a transaction are coalesced into a single execution.\n   * Dependencies are auto-discovered from subscribed live queries, or can be overridden.\n   * Load callbacks are combined when entries merge.\n   *\n   * Uses the current sync session's config and syncState from instance properties.\n   *\n   * @param callback - Optional callback to load more data if needed\n   * @param options - Optional scheduling configuration\n   * @param options.contextId - Transaction ID to group work; defaults to active transaction\n   * @param options.jobId - Unique identifier for this job; defaults to this builder instance\n   * @param options.dependencies - Explicit dependency list; overrides auto-discovered dependencies\n   */\n  scheduleGraphRun(\n    callback?: () => void,\n    options?: {\n      contextId?: SchedulerContextId\n      jobId?: unknown\n      dependencies?: Array<CollectionConfigBuilder<any, any>>\n    },\n  ) {\n    const contextId =\n      options?.contextId ??\n      getActiveTransaction()?.id ??\n      getActivePublicationContext()\n    // Use the builder instance as the job ID for deduplication. This is memory-safe\n    // because the scheduler's context Map is deleted after flushing (no long-term retention).\n    const jobId = options?.jobId ?? this\n    // Snapshot before scheduling parents, which can reenter source setup.\n    const dependentBuilders = options?.dependencies ?? [\n      ...this.builderDependencies,\n    ]\n\n    // Ensure dependent builders are actually scheduled in this context so that\n    // dependency edges always point to a real job (or a deduped no-op if already scheduled).\n    if (contextId) {\n      for (const dep of dependentBuilders) {\n        if (typeof dep.scheduleGraphRun === `function`) {\n          dep.scheduleGraphRun(undefined, { contextId })\n        }\n      }\n    }\n\n    // We intentionally scope deduplication to the builder instance. Each instance\n    // owns caches and compiled pipelines, so sharing work across instances that\n    // merely reuse the same string id would execute the wrong builder's graph.\n\n    if (!this.currentSyncConfig || !this.currentSyncState) {\n      throw new Error(\n        `scheduleGraphRun called without active sync session. This should not happen.`,\n      )\n    }\n\n    // Manage our own state - get or create pending callbacks for this context\n    let pending = contextId ? this.pendingGraphRuns.get(contextId) : undefined\n    if (!pending || pending.syncSession !== this.syncSession) {\n      pending = {\n        syncSession: this.syncSession,\n        loadCallbacks: new Set(),\n      }\n      if (contextId) {\n        this.pendingGraphRuns.set(contextId, pending)\n      }\n    }\n\n    // Add callback if provided (this is what accumulates between schedules)\n    if (callback) {\n      pending.loadCallbacks.add(callback)\n    }\n\n    // Schedule execution (scheduler just orchestrates order, we manage state)\n    // For immediate execution (no contextId), pass pending directly since it won't be in the map\n    const pendingToPass = contextId ? undefined : pending\n    transactionScopedScheduler.schedule({\n      contextId,\n      jobId,\n      dependencies: dependentBuilders,\n      run: () => this.executeGraphRun(contextId, pendingToPass),\n    })\n  }\n\n  /**\n   * Clears pending graph run state for a specific context.\n   * Called when the scheduler clears a context (e.g., transaction rollback/abort).\n   */\n  clearPendingGraphRun(contextId: SchedulerContextId): void {\n    this.pendingGraphRuns.delete(contextId)\n  }\n\n  /**\n   * Returns true if this builder has a pending graph run for the given context.\n   */\n  hasPendingGraphRun(contextId: SchedulerContextId): boolean {\n    return this.pendingGraphRuns.has(contextId)\n  }\n\n  /**\n   * Executes a pending graph run. Called by the scheduler when dependencies are satisfied.\n   * Clears the pending state BEFORE execution so that any re-schedules during the run\n   * create fresh state and don't interfere with the current execution.\n   * Uses instance sync state - if sync has ended, gracefully returns without executing.\n   *\n   * @param contextId - Optional context ID to look up pending state\n   * @param pendingParam - For immediate execution (no context), pending state is passed directly\n   */\n  private executeGraphRun(\n    contextId?: SchedulerContextId,\n    pendingParam?: PendingGraphRun,\n  ): void {\n    // Get pending state: either from parameter (no context) or from map (with context)\n    // Remove from map BEFORE checking sync state to prevent leaking entries when sync ends\n    // before the transaction flushes (e.g., unsubscribe during in-flight transaction)\n    const pending =\n      pendingParam ??\n      (contextId ? this.pendingGraphRuns.get(contextId) : undefined)\n    if (contextId) {\n      this.pendingGraphRuns.delete(contextId)\n    }\n\n    // If no pending state, nothing to execute (context was cleared)\n    if (!pending) {\n      return\n    }\n\n    // If sync session has ended, don't execute (graph is finalized, subscriptions cleared)\n    if (\n      pending.syncSession !== this.syncSession ||\n      !this.currentSyncConfig ||\n      !this.currentSyncState\n    ) {\n      return\n    }\n\n    this.maybeRunGraph(() => runAllCallbacks(pending.loadCallbacks))\n  }\n\n  private getSyncConfig(): SyncConfig<TResult> {\n    return {\n      rowUpdateMode: `full`,\n      sync: this.syncFn.bind(this),\n    }\n  }\n\n  private syncFn(config: SyncMethods<TResult>) {\n    const syncSession = ++this.syncSession\n    // Store reference to the live query collection for error state transitions\n    this.liveQueryCollection = config.collection\n    // Reset error state from any previous sync session so a restarted sync can become ready again.\n    this.isInErrorState = false\n    this.fatalQueryError = false\n    this.erroredSourceIds.clear()\n    this.lastSubsetError = undefined\n    // Store config and syncState as instance properties for the duration of this sync session\n    this.currentSyncConfig = config\n\n    const syncState: SyncState = {\n      messagesCount: 0,\n      subscribedToAllCollections: false,\n      unsubscribeCallbacks: new Set<() => void>(),\n    }\n\n    let tornDown = false\n    const teardown = () => {\n      if (tornDown) return\n      tornDown = true\n      if (this.syncSession === syncSession) this.syncSession++\n\n      // Release every source in one attempt; the first failure wins after the\n      // peers finish. Each subscription release is itself one-shot, so the\n      // Collection's cleanup retry has nothing left to repeat here.\n      try {\n        runAllCallbacks(syncState.unsubscribeCallbacks)\n      } finally {\n        syncState.unsubscribeCallbacks.clear()\n        this.clearSyncSessionState()\n      }\n    }\n\n    try {\n      // Extend the pipeline such that it applies the incoming changes to the collection\n      const fullSyncState = this.extendPipelineWithChangeProcessing(\n        config,\n        syncState,\n      )\n      this.currentSyncState = fullSyncState\n\n      // Listen for scheduler context clears to clean up our pending state\n      // Re-register on each sync start so the listener is active for the sync session's lifetime\n      this.unsubscribeFromSchedulerClears = transactionScopedScheduler.onClear(\n        (contextId) => {\n          this.clearPendingGraphRun(contextId)\n        },\n      )\n\n      // Listen for loadingSubset changes on the live query collection BEFORE subscribing.\n      // This ensures we don't miss the event if subset loading completes synchronously.\n      // When isLoadingSubset becomes false, we may need to mark the collection as ready\n      // (if all source collections are already ready but we were waiting for subset load to complete)\n      const loadingSubsetUnsubscribe = config.collection.on(\n        `loadingSubset:change`,\n        (event) => {\n          if (!event.isLoadingSubset) {\n            // Subset loading finished, check if we can now mark ready\n            this.updateLiveQueryStatus(config)\n            if (this.hasPendingSourceRecovery()) this.maybeRunGraphFn?.()\n          }\n        },\n      )\n      syncState.unsubscribeCallbacks.add(loadingSubsetUnsubscribe)\n\n      const loadSubsetDataCallbacks = this.subscribeToAllCollections(\n        config,\n        fullSyncState,\n      )\n\n      this.maybeRunGraphFn = () =>\n        this.scheduleGraphRun(loadSubsetDataCallbacks)\n\n      // Initial run with callback to load more data if needed\n      this.scheduleGraphRun(loadSubsetDataCallbacks)\n    } catch (error) {\n      try {\n        teardown()\n      } catch {\n        // Preserve the setup failure. It is the error the caller can act on.\n      }\n      throw error\n    }\n\n    return teardown\n  }\n\n  private clearSyncSessionState(): void {\n    // Late window settlement belongs to the discarded graph, not its restart.\n    this.windowOperationGeneration++\n    // Clear current sync session state\n    this.currentSyncConfig = undefined\n    this.currentSyncState = undefined\n    this.maybeRunGraphFn = undefined\n    this.currentWindow = undefined\n    this.settledWindow = this.initialWindow\n    this.isInErrorState = false\n    this.fatalQueryError = false\n    this.erroredSourceIds.clear()\n\n    // Clear all pending graph runs to prevent memory leaks from in-flight transactions\n    // that may flush after the sync session ends\n    this.pendingGraphRuns.clear()\n\n    // Reset caches so a fresh graph/pipeline is compiled on next start\n    // This avoids reusing a finalized D2 graph across GC restarts\n    this.graphCache = undefined\n    this.inputsCache = undefined\n    this.pipelineCache = undefined\n    this.sourceWhereClausesCache = undefined\n    this.bucketFacadesCache = undefined\n\n    // Reset lazy source alias state\n    this.lazySources.clear()\n    this.demandGenerations.clear()\n    this.activeDemands.clear()\n    this.pendingOrderedLoads.clear()\n    this.orderedLoadFailed = false\n    this.windowFailed = false\n    this.optimizableOrderByCollections = {}\n    this.lazySourcesCallbacks = {}\n\n    // Clear subscription references to prevent memory leaks\n    // Note: Individual subscriptions are already unsubscribed via unsubscribeCallbacks\n    Object.keys(this.subscriptions).forEach(\n      (key) => delete this.subscriptions[key],\n    )\n\n    // Unregister from scheduler's onClear listener to prevent memory leaks\n    // The scheduler's listener Set would otherwise keep a strong reference to this builder\n    this.unsubscribeFromSchedulerClears?.()\n    this.unsubscribeFromSchedulerClears = undefined\n  }\n\n  /**\n   * Compiles the query pipeline with all declared aliases.\n   */\n  private compileBasePipeline() {\n    this.graphCache = new D2()\n    this.inputsCache = Object.fromEntries(\n      this.collectionSources.map((source) => [\n        source.sourceId,\n        this.graphCache!.newInput<any>(),\n      ]),\n    )\n\n    const compilation = compileQuery(\n      this.query,\n      this.inputsCache as Record<string, KeyedStream>,\n      this.collections,\n      this.subscriptions,\n      this.lazySourcesCallbacks,\n      this.lazySources,\n      this.optimizableOrderByCollections,\n      (windowFn: (options: WindowOptions) => void) => {\n        this.windowFn = windowFn\n        // `setWindow` mutates the compiled top-K operator, which is replaced\n        // whenever a cleaned-up live query compiles a fresh pipeline. Keep the\n        // desired window on the builder and replay it into each new operator.\n        if (this.currentWindow) {\n          windowFn(this.currentWindow)\n        }\n      },\n    )\n\n    const materialized = materializeCompilation(\n      compilation,\n      this.config.getKey,\n      this.hasJoins(this.query),\n    )\n    this.pipelineCache = materialized.pipeline\n    this.sourceWhereClausesCache = compilation.sourceWhereClauses\n    this.bucketFacadesCache = materialized.facades\n\n    const missingSources = this.collectionSources\n      .map((source) => source.sourceId)\n      .filter((sourceId) => !Object.hasOwn(this.inputsCache!, sourceId))\n    if (missingSources.length > 0) {\n      throw new MissingAliasInputsError(missingSources)\n    }\n  }\n\n  private maybeCompileBasePipeline() {\n    if (!this.graphCache || !this.inputsCache || !this.pipelineCache) {\n      this.compileBasePipeline()\n    }\n    return {\n      graph: this.graphCache!,\n      inputs: this.inputsCache!,\n      pipeline: this.pipelineCache!,\n    }\n  }\n\n  private extendPipelineWithChangeProcessing(\n    config: SyncMethods<TResult>,\n    syncState: SyncState,\n  ): FullSyncState {\n    const { begin, commit } = config\n    const { graph, inputs, pipeline } = this.maybeCompileBasePipeline()\n\n    // Accumulator for changes across all output callbacks within a single graph run.\n    // This allows us to batch all changes from intermediate join states into a single\n    // transaction, avoiding duplicate key errors when joins produce multiple outputs\n    // for the same key (e.g., first output with null, then output with joined data).\n    let pendingChanges: Map<unknown, Changes<TResult>> = new Map()\n\n    pipeline.pipe(\n      output((data) => {\n        const messages = data.getInner()\n        syncState.messagesCount += messages.length\n\n        // Accumulate changes from this output callback into the pending changes map.\n        // Changes for the same key are merged (inserts/deletes are added together).\n        messages.reduce(accumulateChanges<TResult>, pendingChanges)\n      }),\n    )\n\n    const bucketFacades = new BucketFacadeAdapter(\n      this.id,\n      this.bucketFacadesCache ?? [],\n      (count) => {\n        syncState.messagesCount += count\n      },\n    )\n    syncState.unsubscribeCallbacks.add(() => bucketFacades.cleanup())\n\n    // Flush pending changes and reset the accumulator.\n    // Called at the end of each graph run to commit all accumulated changes.\n    syncState.flushPendingChanges = () => {\n      const hasParentChanges = pendingChanges.size > 0\n      const hasChildChanges = bucketFacades.hasPendingChanges()\n\n      if (!hasParentChanges && !hasChildChanges) {\n        return\n      }\n\n      if (\n        this.windowFailed ||\n        this.orderedLoadFailed ||\n        this.hasPendingSourceRecovery() ||\n        this.pendingOrderedLoads.size > 0\n      ) {\n        return\n      }\n\n      let facadePublication:\n        | ReturnType<BucketFacadeAdapter[`flush`]>\n        | undefined\n      let rootPublication:\n        | ReturnType<Collection[`_deferPublication`]>\n        | undefined\n      try {\n        facadePublication = bucketFacades.flush()\n        rootPublication = hasParentChanges\n          ? config.collection._deferPublication()\n          : undefined\n        const changesToApply: Map<unknown, Changes<TResult>> = new Map(\n          [...pendingChanges].map(([key, changes]) => {\n            const resolved: Changes<TResult> = {\n              ...changes,\n              value: bucketFacades.resolve(changes.value),\n            }\n            if (changes.previousValue !== undefined) {\n              resolved.previousValue = bucketFacades.resolve(\n                changes.previousValue,\n              )\n            }\n            return [key, resolved]\n          }),\n        )\n        // New facades are not reachable until their root row is installed, so\n        // make them ready first. A facade failure then leaves the root intact,\n        // and the root commit is the final state change before publication.\n        facadePublication.prepare()\n        if (hasParentChanges) {\n          begin()\n          changesToApply.forEach(this.applyChanges.bind(this, config))\n          if (hasOrderOnlyMove(changesToApply)) {\n            markLayoutChange(config.collection)\n          }\n          commit()\n        }\n      } catch (error) {\n        rootPublication?.discard()\n        facadePublication?.rollback()\n        throw error\n      }\n      pendingChanges = new Map()\n\n      let publicationError: unknown\n      for (const publish of [\n        rootPublication?.publish,\n        facadePublication.publish,\n      ]) {\n        if (!publish) continue\n        try {\n          publish()\n        } catch (error) {\n          publicationError ??= error\n        }\n      }\n      if (publicationError !== undefined) throw publicationError\n    }\n    graph.finalize()\n\n    // Extend the sync state with the graph, inputs, and pipeline\n    syncState.graph = graph\n    syncState.inputs = inputs\n    syncState.pipeline = pipeline\n\n    return syncState as FullSyncState\n  }\n\n  private applyChanges(\n    config: SyncMethods<TResult>,\n    changes: {\n      deletes: number\n      inserts: number\n      value: TResult\n      orderByIndex: string | undefined\n    },\n    key: unknown,\n  ) {\n    const { write, collection } = config\n    const { deletes, inserts, value, orderByIndex } = changes\n\n    // Store the key of the result so that we can retrieve it in the\n    // getKey function\n    this.resultKeys.set(value, key)\n\n    // Store the orderBy index if it exists\n    if (orderByIndex !== undefined) {\n      this.orderByIndices.set(value, orderByIndex)\n    }\n\n    // Simple singular insert.\n    if (inserts && deletes === 0) {\n      write({\n        value,\n        type: `insert`,\n      })\n    } else if (\n      // Insert & update(s) (updates are a delete & insert)\n      inserts > deletes ||\n      // Just update(s) but the item is already in the collection (so\n      // was inserted previously).\n      (inserts === deletes && collection.has(collection.getKeyFromItem(value)))\n    ) {\n      write({\n        value,\n        type: `update`,\n      })\n      // Only delete is left as an option\n    } else if (deletes > 0) {\n      write({\n        value,\n        type: `delete`,\n      })\n    } else {\n      throw new Error(\n        `Could not apply changes: ${JSON.stringify(changes)}. This should never happen.`,\n      )\n    }\n  }\n\n  /**\n   * Handle status changes from source collections\n   */\n  private handleSourceStatusChange(\n    config: SyncMethods<TResult>,\n    sourceId: string,\n    collectionId: string,\n    event: AllCollectionEvents[`status:change`],\n  ) {\n    const { status } = event\n\n    // Handle error state - any source collection in error puts live query in error\n    if (status === `error`) {\n      this.erroredSourceIds.add(sourceId)\n      this.setErrorState(\n        `Source collection '${collectionId}' entered error state`,\n      )\n      return\n    }\n\n    // Handle manual cleanup - this should not happen due to GC prevention,\n    // but could happen if user manually calls cleanup()\n    if (status === `cleaned-up`) {\n      this.transitionToError(\n        `Source collection '${collectionId}' was manually cleaned up while live query '${this.id}' depends on it. ` +\n          `Live queries prevent automatic GC, so this was likely a manual cleanup() call.`,\n      )\n      return\n    }\n\n    if (status === `ready`) {\n      const recovered = this.erroredSourceIds.delete(sourceId)\n      if (\n        recovered &&\n        !this.fatalQueryError &&\n        this.erroredSourceIds.size === 0\n      ) {\n        this.isInErrorState = false\n        this.maybeRunGraphFn?.()\n      }\n    }\n\n    // Update ready status based on all source collections\n    this.updateLiveQueryStatus(config)\n  }\n\n  /**\n   * Update the live query status based on source collection statuses\n   */\n  private updateLiveQueryStatus(config: SyncMethods<TResult>) {\n    const { markReady } = config\n\n    // Don't update status if already in error\n    if (this.isInErrorState) {\n      return\n    }\n\n    const subscribedToAll = this.currentSyncState?.subscribedToAllCollections\n    const allReady = this.allRequiredSourcesReady()\n    const allDemandsSettled = [...this.activeDemands.values()].every(\n      (demand) => demand.settled,\n    )\n    const isLoading = this.liveQueryCollection?.isLoadingSubset\n    // Mark ready when:\n    // 1. All subscriptions are set up (subscribedToAllCollections)\n    // 2. All source collections are ready\n    // 3. Every active route demand has settled\n    // 4. The live query collection is not loading subset data\n    // This prevents marking the live query ready before its data is processed\n    // (fixes issue where useLiveQuery returns isReady=true with empty data)\n    if (subscribedToAll && allReady && allDemandsSettled && !isLoading) {\n      markReady()\n    }\n  }\n\n  /**\n   * Transition the live query to error state\n   */\n  private transitionToError(message: string, error?: unknown) {\n    this.fatalQueryError = true\n    this.setErrorState(message, error)\n  }\n\n  private setErrorState(message: string, error?: unknown) {\n    this.isInErrorState = true\n\n    // Log error to console for debugging\n    console.error(`[Live Query Error] ${message}`)\n\n    // Transition live query collection to error state\n    this.liveQueryCollection?._lifecycle.markError(error ?? new Error(message))\n  }\n\n  private allRequiredSourcesReady() {\n    return this.collectionSources.every(\n      (source) =>\n        // Only on-demand sources settle through route demand. Eager\n        // loadSubset calls return immediately, so they must reach ready.\n        (this.lazySources.has(source.sourceId) &&\n          source.collection.config.syncMode === `on-demand`) ||\n        source.collection.isReady(),\n    )\n  }\n\n  /**\n   * Creates one subscription per lexical collection source.\n   * Each source gets independent filters, even when aliases or collections repeat.\n   * Example: `{ employee: col, manager: col }` creates two separate subscriptions.\n   */\n  private subscribeToAllCollections(\n    config: SyncMethods<TResult>,\n    syncState: FullSyncState,\n  ) {\n    if (this.collectionSources.length === 0) {\n      throw new Error(\n        `Query '${this.id}' has no collection sources. This should not happen; please report.`,\n      )\n    }\n\n    const loaders = this.collectionSources.map((source) => {\n      const { sourceId, alias, collection } = source\n      const collectionId = collection.id\n\n      const dependencyBuilder = getCollectionBuilder(collection)\n      if (dependencyBuilder && dependencyBuilder !== this) {\n        this.builderDependencies.add(dependencyBuilder)\n      }\n\n      // CollectionSubscriber handles the actual subscription to the source collection\n      // and feeds data into the D2 graph inputs for this specific alias\n      const collectionSubscriber = new CollectionSubscriber(\n        sourceId,\n        alias,\n        collection,\n        this,\n      )\n\n      // Subscribe to status changes for status flow\n      const statusUnsubscribe = collection.on(`status:change`, (event) => {\n        this.handleSourceStatusChange(config, sourceId, collectionId, event)\n      })\n      syncState.unsubscribeCallbacks.add(statusUnsubscribe)\n\n      // The source may have failed before this live query subscribed. Register\n      // the listener first, then reconcile that current state so no transition\n      // can be missed between observation and subscription.\n      if (collection.status === `error`) {\n        this.handleSourceStatusChange(config, sourceId, collectionId, {\n          type: `status:change`,\n          collection,\n          status: `error`,\n          previousStatus: `error`,\n        })\n      }\n\n      const subscription = collectionSubscriber.subscribe()\n      this.subscriptions[sourceId] = subscription\n\n      const lazyCallbacks = this.lazySourcesCallbacks[sourceId]\n      if (lazyCallbacks) {\n        lazyCallbacks.setDemand = (plan, keys) =>\n          collectionSubscriber.setDemand(subscription, plan, keys)\n        for (const plan of lazyCallbacks.plans ?? []) {\n          if (plan.initialKeys.size > 0) {\n            lazyCallbacks.setDemand(plan, plan.initialKeys)\n          }\n        }\n      }\n\n      // Create a callback for loading more data if needed (used by OrderBy optimization)\n      const loadMore = collectionSubscriber.loadMoreIfNeeded.bind(\n        collectionSubscriber,\n        subscription,\n      )\n\n      return loadMore\n    })\n\n    // Mark as subscribed so the graph can start running\n    // (graph only runs when all collections are subscribed)\n    syncState.subscribedToAllCollections = true\n\n    // Note: We intentionally don't call updateLiveQueryStatus() here.\n    // The graph hasn't run yet, so marking ready would be premature.\n    // The canonical place to mark ready is after the graph processes data\n    // in maybeRunGraph(), which ensures data has been processed first.\n\n    return () => runAllCallbacks(loaders)\n  }\n}\n\nfunction createOrderByComparator<T extends object>(\n  orderByIndices: WeakMap<object, string>,\n) {\n  return (val1: T, val2: T): number => {\n    // Use the orderBy index stored in the WeakMap\n    const index1 = orderByIndices.get(val1)\n    const index2 = orderByIndices.get(val2)\n\n    // Compare fractional indices lexicographically\n    if (index1 && index2) {\n      if (index1 < index2) {\n        return -1\n      } else if (index1 > index2) {\n        return 1\n      } else {\n        return 0\n      }\n    }\n\n    // Fallback to no ordering if indices are missing\n    return 0\n  }\n}\n\nfunction accumulateChanges<T>(\n  acc: Map<unknown, Changes<T>>,\n  [[key, tupleData], multiplicity]: [\n    [unknown, [any, string | undefined]],\n    number,\n  ],\n) {\n  // All queries now consistently return [value, orderByIndex] format\n  // where orderByIndex is undefined for queries without ORDER BY\n  const [value, orderByIndex] = tupleData as [T, string | undefined]\n\n  const changes = acc.get(key) || {\n    deletes: 0,\n    inserts: 0,\n    value,\n    orderByIndex,\n  }\n  if (multiplicity < 0) {\n    changes.deletes += Math.abs(multiplicity)\n    // Remember the retracted (old) value + position so the flush can tell an\n    // order-only move apart from a real value change.\n    changes.previousValue = value\n    changes.previousOrderByIndex = orderByIndex\n  } else if (multiplicity > 0) {\n    changes.inserts += multiplicity\n    // Update value to the latest version for this key\n    changes.value = value\n    if (orderByIndex !== undefined) {\n      changes.orderByIndex = orderByIndex\n    }\n  }\n  acc.set(key, changes)\n  return acc\n}\n\n/**\n * Decide whether a flush contains an order-only move.\n *\n * An \"order-only move\" — a row updated in place whose `orderByIndex` moved but\n * whose projected value is deep-equal to before — is swallowed by the value-diff\n * and needs an explicit layout notification. The collection coalesces that\n * signal with any ordinary row publication per subscriber.\n */\nfunction hasOrderOnlyMove<T>(\n  changesToApply: Map<unknown, Changes<T>>,\n): boolean {\n  for (const changes of changesToApply.values()) {\n    const isUpdate = changes.inserts > 0 && changes.deletes > 0\n    if (\n      isUpdate &&\n      changes.previousValue !== undefined &&\n      deepEquals(changes.previousValue, changes.value) &&\n      changes.orderByIndex !== changes.previousOrderByIndex\n    ) {\n      return true\n    }\n  }\n  return false\n}\n\n/** Mark the collection's next commit as layout-changing. */\nfunction markLayoutChange(collection: { _markLayoutChange: () => void }): void {\n  collection._markLayoutChange()\n}\n"],"names":["buildQueryFromConfig","extractCollectionsFromQuery","extractCollectionSources","extractCollectionFromSource","LIVE_QUERY_INTERNAL","SetWindowRequiresOrderByError","SetWindowReentrancyError","settlement","withPublicationContext","error","normalizeError","getActiveTransaction","getActivePublicationContext","transactionScopedScheduler","runAllCallbacks","D2","compileQuery","materializeCompilation","MissingAliasInputsError","output","BucketFacadeAdapter","getCollectionBuilder","collectionSubscriber","CollectionSubscriber","deepEquals"],"mappings":";;;;;;;;;;;;;;;;;AA8EA,IAAI,6BAA6B;AAM1B,MAAM,wBAGX;AAAA,EA+FA,YACmB,QACjB;AADiB,SAAA,SAAA;AAtFnB,SAAiB,iCAAiB,QAAA;AAGlC,SAAiB,qCAAqB,QAAA;AAKtC,SAAQ,iBAAiB;AAUzB,SAAQ,iBAAiB;AACzB,SAAQ,kBAAkB;AAC1B,SAAiB,uCAAuB,IAAA;AAexC,SAAiB,0CAA0B,IAAA;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