{
  "version": 3,
  "sources": ["../../../src/QueryResult.ts"],
  "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as Atom from '@effect-atom/atom/Atom';\nimport * as Effect from 'effect/Effect';\nimport * as Option from 'effect/Option';\n\nimport { type CleanupFn } from '@dxos/async';\n\nimport type * as Entity from './Entity';\n\n/**\n * Individual query result entry.\n */\nexport type Entry<T> = {\n  id: string;\n\n  /**\n   * May not be present for remote results.\n   */\n  result?: T;\n\n  match?: {\n    // TODO(dmaretskyi): text positional info.\n\n    /**\n     * Higher means better match.\n     */\n    rank: number;\n  };\n\n  /**\n   * Query resolution metadata.\n   */\n  // TODO(dmaretskyi): Rename to meta?\n  resolution?: {\n    // TODO(dmaretskyi): Make this more generic.\n    source: 'remote' | 'local' | 'index';\n\n    /**\n     * Query resolution time in milliseconds.\n     */\n    time: number;\n  };\n};\n\n/**\n * Invidual query result entry for a database Entity.\n */\nexport type EntityEntry<T extends Entity.Unknown = Entity.Unknown> = Entry<T>;\n\nexport type RunOptions = {\n  timeout?: number;\n};\n\nexport type SubscriptionOptions = {\n  /**\n   * Fire the callback immediately.\n   */\n  fire?: boolean;\n};\n\n// TODO(burdon): Should T be constrained to Entity.Any?\nexport interface QueryResult<T> {\n  /**\n   * Currently available results along with their match metadata.\n   */\n  readonly entries: Entry<T>[];\n\n  /**\n   * Currently available results.\n   */\n  readonly results: T[];\n\n  /**\n   * Returns all known results.\n   */\n  run(opts?: RunOptions): Promise<T[]>;\n\n  /**\n   * Returns all known results along with their match metadata.\n   */\n  runEntries(opts?: RunOptions): Promise<Entry<T>[]>;\n\n  /**\n   * Returns currently available results synchronously.\n   */\n  runSync(): T[];\n\n  /**\n   * Returns currently available results synchronously along with their match metadata.\n   */\n  runSyncEntries(): Entry<T>[];\n\n  /**\n   * Returns first result.\n   */\n  first(opts?: RunOptions): Promise<T>;\n\n  /**\n   * Returns first result if there is one.\n   */\n  firstOrUndefined(opts?: RunOptions): Promise<T | undefined>;\n\n  /**\n   * Subscribes to changes in query results.\n   */\n  subscribe(callback?: (query: QueryResult<T>) => void, opts?: SubscriptionOptions): CleanupFn;\n\n  /**\n   * Self-updating atom. Updates automatically when query results change.\n   *\n   * Memoized per QueryResult instance — repeated accesses on the same instance return the same\n   * Atom. Safe only when the QueryResult is itself held stable across re-renders (e.g. behind a\n   * `useMemo`). It must NOT be used in graph-builder connectors/actions or other atom computes,\n   * where `db.query(...)` is called fresh on each re-evaluation: every run constructs a new\n   * QueryResult and so a new atom + subscription, leaking the previous ones. Use the memoized\n   * {@link atom} family there instead.\n   */\n  readonly atom: Atom.Atom<T[]>;\n}\n\n/**\n * Effect that returns a QueryResult when evaluated, but also has shorthand methods for running the query or getting the first result.\n */\nexport interface QueryResultEffect<T, E, R> extends Effect.Effect<QueryResult<T>, E, R> {\n  run: Effect.Effect<T[], E, R>;\n  first: Effect.Effect<Option.Option<T>, E, R>;\n\n  // TODO(dmaretskyi): Considering adding `atom`, but since `Database.query` is used in imperative code only, I dont think it will be useful.\n}\n"],
  "mappings": ";AAAA;",
  "names": []
}
