{"version":3,"file":"live-query-options.cjs","sources":["../../src/live-query-options.ts"],"sourcesContent":["import { BaseQueryBuilder } from './query/builder/index.js'\nimport { collectSourceRefs, requireCollectionSource } from './query/ir.js'\nimport { isCollection, isFailedStatus } from './live-query-primitives.js'\nimport {\n  DEFAULT_GC_TIME_MS,\n  UNSUBSCRIBED_GC_FLOOR_MS,\n} from './collection/lifecycle.js'\nimport { createLiveQueryCollection } from './query/live-query-collection.js'\nimport { LIVE_QUERY_INTERNAL } from './query/live/internal.js'\nimport { getBuilderFromConfig } from './query/live/collection-registry.js'\nimport {\n  getQueryIdentity,\n  getStableQueryBuilderHash,\n  getStableValueHash,\n} from './query/ir-stable-identity.js'\nimport { getStringCollationIdentity } from './query/runtime-reference-identity.js'\nimport { codedMessage, devBuild } from './error-message.js'\nimport { InvalidPoolOptionError, UnhashableQueryIRError } from './errors.js'\nimport type { Collection, CollectionImpl } from './collection/index.js'\nimport type { CollectionOptions, DbClient } from './client.js'\nimport type { PooledLiveQueries } from './query/pooled-live-query.js'\nimport type {\n  Context,\n  InitialQueryBuilder,\n  LiveQueryCollectionConfig,\n  LiveQueryHookConfig,\n  QueryBuilder,\n} from './query/index.js'\n\n/**\n * A hook config's pool: `pooledLiveQueries` or nothing. Any other value, such\n * as the removed boolean form, contradicts the hook contract, so every reader\n * rejects it the same way, whatever the config's other options. An adapter\n * that reads the pool from its own options, as Angular does, checks it here.\n */\nexport function configPool(config: object): PooledLiveQueries | undefined {\n  const { pool } = config as { pool?: unknown }\n  if (\n    pool !== undefined &&\n    typeof (pool as Partial<PooledLiveQueries> | null)\n      ?.createPooledLiveQuery !== `function`\n  )\n    throw new InvalidPoolOptionError()\n  return pool as PooledLiveQueries | undefined\n}\n\nexport type LiveQueryKey = ReadonlyArray<unknown>\n\nexport type LiveQueryOptions = LiveQueryCollectionConfig<any> & {\n  queryKey?: LiveQueryKey\n}\n\nexport type DeferredLiveQueryCollections = Set<\n  CollectionImpl<any, string | number, any, any, any>\n>\n\n/**\n * Release the sync starts a rejected query input deferred, then rethrow its\n * rejection. A framework hook calls this around its own render work, so a\n * query the hook rejects leaves no source deferred and a later reader can\n * start it. A render that throws after the hook returns, such as in a sibling\n * hook, is not a rejection of this input: like any render that never commits,\n * it releases its deferral through `releaseDeferredOnReclaim` when garbage\n * collection reclaims its live query. As for a\n * `DbClient` preload, the rejection wins over a source whose resumed start\n * fails; that source records its own failure.\n */\nexport function rejectLiveQueryInput(\n  collections: DeferredLiveQueryCollections | undefined,\n  rejection: unknown,\n): never {\n  try {\n    if (collections) resumeDeferredLiveQueryCollections(collections)\n  } catch {\n    // Report the rejection, not a later start failure.\n  }\n  throw rejection\n}\n\n/**\n * Release a render's deferred sync starts when the live query it built is\n * reclaimed. A render that commits releases them first, when it subscribes.\n * A render that never commits, because a sibling threw or it suspended, leaves\n * its live query unsubscribed until garbage collection cleans it up; that\n * cleanup releases the starts, so a direct reader's demand, made before or\n * after, then starts the sources. A live query that garbage collection never\n * reclaims, because its config disables GC or does not start its sync,\n * releases after the unsubscribed GC floor instead. Releases only the starts\n * deferred so far, not ones a later render of the same hook defers. Pass only\n * a live query the render built, and call the returned function when the\n * render commits, so a committed hook keeps no listener, timer, or copied\n * sources.\n */\nexport function releaseDeferredOnReclaim(\n  liveQuery: Pick<Collection<any, any, any>, `once` | `config` | `status`>,\n  deferredCollections: DeferredLiveQueryCollections,\n): (() => void) | undefined {\n  if (deferredCollections.size === 0) return undefined\n  const pending: DeferredLiveQueryCollections = new Set(deferredCollections)\n  const release = () => {\n    try {\n      resumeDeferredLiveQueryCollections(pending)\n    } catch {\n      // A source whose resumed start fails records its own failure.\n    }\n  }\n  // The lifecycle's own rule: GC reclaims only a started collection with a\n  // finite, positive gcTime.\n  const gcTime = liveQuery.config.gcTime ?? DEFAULT_GC_TIME_MS\n  if (gcTime > 0 && Number.isFinite(gcTime) && liveQuery.status !== `idle`) {\n    return liveQuery.once(`status:cleaned-up`, release)\n  }\n  const timer = setTimeout(release, UNSUBSCRIBED_GC_FLOOR_MS)\n  return () => clearTimeout(timer)\n}\n\n/** Release every deferred source, then report the first startup failure. */\nexport function resumeDeferredLiveQueryCollections(\n  collections: DeferredLiveQueryCollections,\n  reportSourceErrors = false,\n): void {\n  const pending = Array.from(collections)\n  collections.clear()\n  let firstFailure: { error: unknown } | undefined\n  for (const collection of pending) {\n    try {\n      collection._resumeSyncStart()\n      // An explicit preload owns source failure reporting even when graph\n      // demand already released the gate. Ordinary hooks read the status.\n      if (reportSourceErrors && isFailedStatus(collection.status)) {\n        throw collection._lifecycle.getSyncError()\n      }\n    } catch (error) {\n      firstFailure ??= { error }\n    }\n  }\n  if (firstFailure) throw firstFailure.error\n}\n\ntype PreparedLiveQueryConfigInput = Omit<\n  LiveQueryHookConfig<Context>,\n  `query`\n> & {\n  query:\n    | QueryBuilder<Context>\n    | ((q: InitialQueryBuilder) => QueryBuilder<Context> | undefined | null)\n  queryKey?: LiveQueryKey\n  client?: DbClient\n}\n\nexport function prepareLiveQueryValue(\n  value: unknown,\n  dbClient: DbClient | undefined,\n  deferredCollections?: DeferredLiveQueryCollections,\n): unknown {\n  if (typeof value === `function`) {\n    return prepareLiveQueryValue(\n      value(new BaseQueryBuilder() as InitialQueryBuilder),\n      dbClient,\n      deferredCollections,\n    )\n  }\n\n  if (value instanceof BaseQueryBuilder) {\n    return value._bindCollectionSources(\n      dbClient\n        ? (options) => {\n            const config = options as CollectionOptions<\n              any,\n              string | number,\n              any,\n              any\n            >\n            const collection = (\n              deferredCollections\n                ? dbClient._materializeCollectionForRender(config)\n                : dbClient.collection(config)\n            ) as CollectionImpl<any, string | number, any, any, any>\n            if (deferredCollections && collection._deferSyncStart())\n              deferredCollections.add(collection)\n            return collection\n          }\n        : undefined,\n    )\n  }\n\n  if (\n    value &&\n    typeof value === `object` &&\n    !isCollection(value) &&\n    `query` in value\n  ) {\n    const pool = configPool(value)\n    const {\n      query,\n      queryKey: _queryKey,\n      client: _client,\n      pool: _pool,\n      ...config\n    } = value as PreparedLiveQueryConfigInput\n\n    const preparedQuery = prepareLiveQueryValue(\n      query,\n      dbClient,\n      deferredCollections,\n    )\n\n    if (preparedQuery === undefined || preparedQuery === null) {\n      return preparedQuery\n    }\n\n    return {\n      ...config,\n      // A DbClient query never pools, so its identity must not depend on\n      // pool: a server preload and a client hook must hash alike.\n      ...(pool && !dbClient ? { pool } : {}),\n      query: preparedQuery,\n    }\n  }\n\n  return value\n}\n\n/** Concrete source objects in query-position order for a prepared value. */\nexport function getPreparedLiveQuerySources(\n  value: unknown,\n  onUnboundDescriptor?: (alias: string) => never,\n): Array<Collection> {\n  if (isCollection(value)) return [value]\n  const query = queryConfig(value)?.query ?? value\n  if (query instanceof BaseQueryBuilder) {\n    return collectSourceRefs(query._getQuery()).map((source) =>\n      source.type === `descriptorRef` && onUnboundDescriptor\n        ? onUnboundDescriptor(source.alias)\n        : requireCollectionSource(source),\n    )\n  }\n  return isCollection(query) ? [query] : []\n}\n\n/** Local row behavior, kept separate from the portable query identity. */\nexport type LiveQueryRowOptions = readonly [getKey: unknown, schema: unknown]\n\nexport function getLiveQueryRowOptions(value: unknown): LiveQueryRowOptions {\n  if (isCollection(value)) {\n    const { config } = value\n    const internal = config.utils?.[LIVE_QUERY_INTERNAL] as\n      { hasCustomGetKey?: boolean } | undefined\n    return [\n      internal?.hasCustomGetKey ? config.getKey : undefined,\n      config.schema,\n    ]\n  }\n  const config = value as\n    { getKey?: unknown; schema?: unknown } | null | undefined\n  return [config?.getKey, config?.schema]\n}\n\nexport function getPreparedLiveQueryIdentity(\n  value: unknown,\n  pool?: PooledLiveQueries,\n): unknown {\n  if (isCollection(value)) return [`collection`, value.id]\n  if (value instanceof BaseQueryBuilder) {\n    // A pooled query's fields and literals identify its rows without\n    // canonicalizing its whole IR on every render.\n    const pooled = pool?.getPooledQueryIdentity(value)\n    return pooled === undefined\n      ? [`query`, getStableQueryBuilderHash(value)]\n      : [`pooled`, pooled]\n  }\n  if (value && typeof value === `object` && `query` in value) {\n    const config = value as LiveQueryCollectionConfig<any>\n    return [\n      // getKey and schema bind for this query identity. Inline options must\n      // not turn a normal framework render into a new Collection.\n      `config`,\n      getPreparedLiveQueryIdentity(config.query, configPool(config)),\n      [`singleResult`, config.singleResult === true],\n      [\n        `defaultStringCollation`,\n        getStringCollationIdentity(config.defaultStringCollation),\n      ],\n    ]\n  }\n  if (value === undefined || value === null) return [`disabled`]\n  return [`value`, value]\n}\n\nexport function getLiveQueryHash(\n  preparedValue: unknown,\n  queryKey?: LiveQueryKey,\n): string {\n  if (queryKey?.length) {\n    const query = queryConfig(preparedValue)?.query\n    const ir = isCollection(preparedValue)\n      ? getBuilderFromConfig(preparedValue.config)?.getWindow()\n      : query instanceof BaseQueryBuilder\n        ? query._getQuery()\n        : undefined\n    const identity: Array<unknown> = [`queryKey`, queryKey]\n    if (ir && (ir.limit !== undefined || (ir.offset ?? 0) !== 0))\n      identity.push({ limit: ir.limit, offset: ir.offset ?? 0 })\n    return getStableValueHash(identity, `queryKey`)\n  }\n  const identity = isCollection(preparedValue)\n    ? [`collection`, preparedValue.id]\n    : [`derived`, getPreparedLiveQueryIdentity(preparedValue)]\n\n  return getStableValueHash(identity, `queryKey`)\n}\n\n// Each entry belongs to one framework binding or an unclaimed retry resource.\n// DbClient snapshots remain separate; peers never share a mutable window owner.\nconst resolvedQueryValues = new WeakMap<\n  object,\n  { collection: object; config: NonNullable<ReturnType<typeof queryConfig>> }\n>()\n\nfunction queryConfig(\n  value: unknown,\n): (Record<string, unknown> & { query?: unknown }) | undefined {\n  return value instanceof BaseQueryBuilder\n    ? { query: value }\n    : value && typeof value === `object`\n      ? (value as Record<string, unknown>)\n      : undefined\n}\n\nfunction moveExistingWindow(\n  after: ReturnType<typeof queryConfig>,\n  previous: Collection<object, string | number, {}> | null | undefined,\n  owner: object | undefined,\n) {\n  if (\n    !previous ||\n    (previous.status !== `ready` && previous.status !== `loading`)\n  )\n    return\n  const retained = owner && resolvedQueryValues.get(owner)\n  if (!retained || retained.collection !== previous) return\n  const before = retained.config\n  if (\n    !(before.query instanceof BaseQueryBuilder) ||\n    !(after?.query instanceof BaseQueryBuilder)\n  )\n    return\n  const oldQuery = before.query._getQuery()\n  const query = after.query._getQuery()\n  const limit = query.limit\n  const offset = query.offset ?? 0\n  // Adding/removing the outer limit, findOne, and opaque plans keep their\n  // ordinary compilation path. A move needs a finite ordered target window.\n  if (\n    after.singleResult === true ||\n    query.singleResult ||\n    !query.orderBy?.length ||\n    oldQuery.limit === undefined ||\n    limit === undefined ||\n    !Number.isSafeInteger(limit) ||\n    limit < 0 ||\n    !Number.isSafeInteger(offset) ||\n    offset < 0\n  )\n    return\n  const keys = Object.keys(before)\n  if (\n    keys.length !== Object.keys(after).length ||\n    keys.some((key) => key !== `query` && before[key] !== after[key])\n  )\n    return\n  // IR source IDs establish semantic identity, but a keyed query may legally\n  // swap the object under an ID. Reuse must preserve the exact source objects.\n  const sources = getPreparedLiveQuerySources(before)\n  const nextSources = getPreparedLiveQuerySources(after)\n  if (\n    sources.length !== nextSources.length ||\n    sources.some((source, index) => source !== nextSources[index])\n  )\n    return\n  try {\n    if (\n      getQueryIdentity({ ...oldQuery, limit: undefined, offset: undefined }) !==\n      getQueryIdentity({ ...query, limit: undefined, offset: undefined })\n    )\n      return\n  } catch (error) {\n    if (error instanceof UnhashableQueryIRError) return\n    throw error\n  }\n  const builder = getBuilderFromConfig(previous.config)\n  if (!builder) return\n  return () => {\n    // A framework can commit synchronously from a Collection publication\n    // callback. Move after that publication, while this remains the binding's\n    // last resolved result and a consumer still asks for it.\n    queueMicrotask(() => {\n      if (\n        resolvedQueryValues.get(owner)?.collection !== previous ||\n        previous.subscriberCount === 0\n      )\n        return\n      const result = builder.setWindow({ offset, limit })\n      // The Collection reports request failures through its health tree. A\n      // superseded move or unmount can reject its waiter without a hook caller.\n      if (result !== true) void result.catch(() => {})\n    })\n  }\n}\n\n/**\n * Resolve an adapter's query value to what its observer watches: `null` for a\n * disabled query, an existing Collection with sync started, or a live query.\n * A query builder whose shape a shared partition can serve gets a pooled view\n * instead of its own live-query Collection.\n */\nexport function resolveLiveQueryValue(\n  value: unknown,\n  {\n    gcTime,\n    pool,\n    startSync = true,\n    previousCollection,\n    windowOwner,\n    onWindowChange,\n  }: {\n    gcTime?: number\n    // `false` keeps a hook that cannot pool, such as a Suspense or DbClient\n    // query, from pooling even when its config passes `pooledLiveQueries`.\n    // A pool here has passed `configPool`.\n    pool?: PooledLiveQueries | false\n    startSync?: boolean\n    /** @internal undefined skips window tracking; null starts an owned result. */\n    previousCollection?: Collection<object, string | number, {}> | null\n    /** @internal Identity of one framework binding; never shared by peers. */\n    windowOwner?: object\n    /** @internal React/Preact defer mutation until their render commits. */\n    onWindowChange?: (move: () => void) => void\n  } = {},\n): Collection<any, any, any> | null {\n  if (previousCollection !== undefined)\n    value = prepareLiveQueryValue(value, undefined)\n  const input = queryConfig(value)\n  const move = moveExistingWindow(input, previousCollection, windowOwner)\n  if (move) {\n    if (onWindowChange) onWindowChange(move)\n    else move()\n    return previousCollection!\n  }\n  if (value === undefined || value === null) return null\n  if (isCollection(value)) {\n    value.startSyncImmediate()\n    return value\n  }\n  if (input) {\n    const requested = configPool(input)\n    const { pool: _pool, ...given } = input as LiveQueryHookConfig<any>\n    const pooling = pool === false ? undefined : (requested ?? pool)\n    // A pooled view needs the built query. Build a query function once and\n    // compile the same builder if the partition cannot serve it.\n    const built =\n      pooling && typeof given.query === `function`\n        ? given.query(new BaseQueryBuilder() as InitialQueryBuilder)\n        : undefined\n    const config =\n      built instanceof BaseQueryBuilder ? { ...given, query: built } : given\n    // A query and lifetime alone can pool; other options shape a Collection.\n    const pooled =\n      pooling &&\n      config.query instanceof BaseQueryBuilder &&\n      Object.keys(config).every((key) => key === `query` || key === `gcTime`)\n        ? pooling.createPooledLiveQuery(config.query, {\n            gcTime: config.gcTime ?? gcTime,\n          })\n        : undefined\n    const collection =\n      pooled ??\n      createLiveQueryCollection({\n        ...config,\n        startSync: config.startSync ?? startSync,\n        gcTime: config.gcTime ?? gcTime,\n      })\n    if (previousCollection !== undefined && windowOwner)\n      resolvedQueryValues.set(windowOwner, { collection, config: input })\n    return collection\n  }\n  throw new Error(\n    devBuild() && process.env.NODE_ENV !== `production`\n      ? `A live query must be a QueryBuilder, LiveQueryCollectionConfig, Collection, undefined, or null. Got: ${typeof value}`\n      : codedMessage(102, { type: typeof value }),\n  )\n}\n"],"names":["InvalidPoolOptionError","DEFAULT_GC_TIME_MS","UNSUBSCRIBED_GC_FLOOR_MS","isFailedStatus","BaseQueryBuilder","isCollection","collectSourceRefs","requireCollectionSource","config","internal","LIVE_QUERY_INTERNAL","getStableQueryBuilderHash","getStringCollationIdentity","ir","getBuilderFromConfig","identity","getStableValueHash","index","getQueryIdentity","UnhashableQueryIRError","createLiveQueryCollection","devBuild","codedMessage"],"mappings":";;;;;;;;;;;;;AAmCO,SAAS,WAAW,QAA+C;AACxE,QAAM,EAAE,SAAS;AACjB,MACE,SAAS,UACT,OAAQ,MACJ,0BAA0B;AAE9B,UAAM,IAAIA,OAAAA,uBAAA;AACZ,SAAO;AACT;AAuBO,SAAS,qBACd,aACA,WACO;AACP,MAAI;AACF,QAAI,gDAAgD,WAAW;AAAA,EACjE,QAAQ;AAAA,EAER;AACA,QAAM;AACR;AAgBO,SAAS,yBACd,WACA,qBAC0B;AAC1B,MAAI,oBAAoB,SAAS,EAAG,QAAO;AAC3C,QAAM,UAAwC,IAAI,IAAI,mBAAmB;AACzE,QAAM,UAAU,MAAM;AACpB,QAAI;AACF,yCAAmC,OAAO;AAAA,IAC5C,QAAQ;AAAA,IAER;AAAA,EACF;AAGA,QAAM,SAAS,UAAU,OAAO,UAAUC,UAAAA;AAC1C,MAAI,SAAS,KAAK,OAAO,SAAS,MAAM,KAAK,UAAU,WAAW,QAAQ;AACxE,WAAO,UAAU,KAAK,qBAAqB,OAAO;AAAA,EACpD;AACA,QAAM,QAAQ,WAAW,SAASC,kCAAwB;AAC1D,SAAO,MAAM,aAAa,KAAK;AACjC;AAGO,SA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