{"version":3,"file":"health-tree.cjs","names":["getCollectionBuilder"],"sources":["../../../src/collection/health-tree.ts"],"sourcesContent":["import { getCollectionBuilder } from '../query/live/collection-registry.js'\nimport type { HealthNode } from '../types'\nimport type { CollectionImpl } from './index.js'\n\n/** A Collection that another Collection reads, under its query alias. */\nexport interface HealthSource {\n  readonly alias: string\n  /** Tells apart two sources under one alias, such as two includes. */\n  readonly id?: string\n  readonly collection: CollectionImpl<any, any, any, any, any>\n  readonly kind?: HealthNode[`kind`]\n}\n\n// One cached node per Collection and source. A node keeps its identity until\n// its own fields or one of its children change, so a change replaces only the\n// nodes on its path and readers can compare nodes with `===`.\nconst nodeCache = new WeakMap<object, Map<string, HealthNode>>()\n\n/**\n * Build the health tree for a Collection. A base Collection is one node. A live\n * query is a node with one child per source alias, in query order, and each\n * child that is itself a live query recurses. The tree is read from current\n * state, so every node mirrors its Collection at the moment it is read.\n */\nexport function buildHealthTree(\n  collection: CollectionImpl<any, any, any, any, any>,\n  { alias, id, kind }: Omit<HealthSource, `collection`> = {\n    alias: `root`,\n  },\n  // The reader that caches this node, so a source's node lives only as long\n  // as the query that reads it.\n  owner: object = collection,\n): HealthNode {\n  // A live query's tree shape comes from its query definition.\n  const builder = getCollectionBuilder(collection)\n  const sources = builder?.healthSources() ?? []\n  const { failureReason, error } = collection._lifecycle.problems\n  const children = sources.map((source) =>\n    buildHealthTree(source.collection, source, collection),\n  )\n  return healthNode(owner, id, {\n    alias,\n    collectionId: collection.id,\n    kind: kind ?? (builder ? `live-query` : `source`),\n    status: collection.status,\n    failureReason,\n    error,\n    reloading: collection._lifecycle.reloading,\n    sources: children,\n  })\n}\n\n/**\n * A node from its own facts and its children. It has errors when it or\n * anything below it has a `failureReason` or an `error`.\n */\nexport function healthNode(\n  owner: object,\n  id: string | undefined,\n  fields: Omit<HealthNode, `hasErrors`>,\n): HealthNode {\n  const { failureReason, error, sources } = fields\n  return cachedNode(owner, id ?? fields.alias, {\n    ...fields,\n    hasErrors:\n      failureReason !== null ||\n      error !== null ||\n      sources.some((child) => child.hasErrors),\n  })\n}\n\n/**\n * Every node in a health tree that shows a problem of its own, in tree order:\n * a `failureReason` or an `error`. A node whose only problems are below it is\n * not listed; its descendants are.\n */\nexport function findProblems(health: HealthNode): Array<HealthNode> {\n  if (!health.hasErrors) return []\n  const ownProblem = health.failureReason !== null || health.error !== null\n  return [\n    ...(ownProblem ? [health] : []),\n    ...health.sources.flatMap(findProblems),\n  ]\n}\n\n/** Return the cached node for this Collection and alias if nothing changed. */\nfunction cachedNode(owner: object, key: string, next: HealthNode): HealthNode {\n  let nodes = nodeCache.get(owner)\n  if (!nodes) nodeCache.set(owner, (nodes = new Map()))\n  const previous = nodes.get(key)\n  if (previous && sameNode(previous, next)) return previous\n  nodes.set(key, next)\n  return next\n}\n\nfunction sameNode(a: HealthNode, b: HealthNode): boolean {\n  return (\n    a.collectionId === b.collectionId &&\n    a.kind === b.kind &&\n    a.status === b.status &&\n    a.hasErrors === b.hasErrors &&\n    a.failureReason === b.failureReason &&\n    a.error === b.error &&\n    a.reloading === b.reloading &&\n    a.sources.length === b.sources.length &&\n    a.sources.every((source, index) => source === b.sources[index])\n  )\n}\n"],"mappings":";AAgBA,IAAM,4BAAY,IAAI,QAAyC;AAQ/D,SAAgB,gBACd,YACA,EAAE,OAAO,IAAI,SAA2C,EACtD,OAAO,OACT,GAGA,QAAgB,YACJ;CAEZ,MAAM,UAAUA,4BAAAA,qBAAqB,UAAU;CAC/C,MAAM,UAAU,SAAS,cAAc,KAAK,CAAC;CAC7C,MAAM,EAAE,eAAe,UAAU,WAAW,WAAW;CACvD,MAAM,WAAW,QAAQ,KAAK,WAC5B,gBAAgB,OAAO,YAAY,QAAQ,UAAU,CACvD;CACA,OAAO,WAAW,OAAO,IAAI;EAC3B;EACA,cAAc,WAAW;EACzB,MAAM,SAAS,UAAU,eAAe;EACxC,QAAQ,WAAW;EACnB;EACA;EACA,WAAW,WAAW,WAAW;EACjC,SAAS;CACX,CAAC;AACH;A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