{
  "version": 3,
  "sources": ["../../../../src/packages/reviewing.dependencies-hierarchy/DependenciesCache.ts"],
  "sourcesContent": ["import type { PackageNode } from './PackageNode.ts';\nimport { serializeTreeNodeId, type TreeNodeId } from './TreeNodeId.ts';\n\nexport type GetDependenciesCacheEntryArgs = {\n  readonly parentId: TreeNodeId;\n  readonly requestedDepth: number;\n};\n\nexport type TraversalResultFullyVisited = {\n  readonly dependencies: PackageNode[];\n\n  /**\n   * Describes the height of the parent node in the fully enumerated dependency\n   * tree. A height of 0 means no entries are present in the dependencies array.\n   * A height of 1 means entries in the dependencies array do not have any of\n   * their own dependencies.\n   */\n  readonly height: number;\n};\n\nexport type TraversalResultPartiallyVisited = {\n  readonly dependencies: PackageNode[];\n\n  /**\n   * Describes how deep the dependencies tree was previously traversed. Since\n   * the traversal result was limited by a max depth, there are likely more\n   * dependencies present deeper in the tree not shown.\n   *\n   * A depth of 0 would indicate no entries in the dependencies array. A depth\n   * of 1 means entries in the dependencies array do not have any of their own\n   * dependencies.\n   */\n  readonly depth: number;\n};\n\nexport type CacheHit = {\n  readonly dependencies: PackageNode[];\n  readonly height: number | 'unknown';\n  // Circular dependencies are not stored in the cache.\n  readonly circular: false;\n};\n\n/**\n * A cache for the dependencies of a package.\n *\n * ## Depth Considerations\n *\n * Since the enumerated dependency tree can be limited by a max depth argument,\n * several considerations have to be made when caching.\n *\n *   - If a package is visited with a requested depth greater than the cached\n *     depth, the cache cannot be used. The tree needs to be enumerated again\n *     deeper.\n *   - If a package is visited with a requested depth less than the cached\n *     depth, the cache probably can't be used. This depends on how strict the\n *     depth constraint is and whether it's acceptable to exceed the max depth.\n *     This cache assumes the max depth should not be exceeded.\n *   - Cycles may or may not be cached. It depends on whether the cycle is\n *     introduced by a package outside of the cached tree.\n *\n * This cache adds an optimization when a dependency tree has been fully\n * enumerated and wasn't limited by a max depth argument. In that case,\n * dependency trees cached can be used when the max depth argument is greater\n * than or equal to the height of the tree root.\n *\n * ## Future Optimizations\n *\n * The necessity of this cache may be removed in the future with a refactor of\n * the `pnpm list` command. This cache attempts to optimize runtime to O(# of\n * unique packages), but the list command is O(# of nodes) anyway since every\n * node needs to be printed. It's possible a generator function could be\n * returned here to avoid computing large trees in-memory before passing to\n * downstream commands.\n */\nexport class DependenciesCache {\n  private readonly fullyVisitedCache = new Map<\n    string,\n    TraversalResultFullyVisited\n  >();\n\n  /**\n   *  Maps cacheKey -> visitedDepth -> dependencies\n   */\n  private readonly partiallyVisitedCache = new Map<\n    string,\n    Map<number, PackageNode[]>\n  >();\n\n  get(args: GetDependenciesCacheEntryArgs): CacheHit | undefined {\n    const cacheKey = serializeTreeNodeId(args.parentId);\n\n    // The fully visited cache is only usable if the height doesn't exceed the\n    // requested depth. Otherwise the final dependencies listing will print\n    // entries with a greater depth than requested.\n    //\n    // If that is the case, the partially visited cache should be checked to see\n    // if dependencies were requested at that exact depth before.\n    const fullyVisitedEntry = this.fullyVisitedCache.get(cacheKey);\n    if (\n      fullyVisitedEntry !== undefined &&\n      fullyVisitedEntry.height <= args.requestedDepth\n    ) {\n      return {\n        dependencies: fullyVisitedEntry.dependencies,\n        height: fullyVisitedEntry.height,\n        circular: false,\n      };\n    }\n\n    const partiallyVisitedEntry = this.partiallyVisitedCache\n      .get(cacheKey)\n      ?.get(args.requestedDepth);\n    if (partiallyVisitedEntry != null) {\n      return {\n        dependencies: partiallyVisitedEntry,\n        height: 'unknown',\n        circular: false,\n      };\n    }\n\n    return undefined;\n  }\n\n  addFullyVisitedResult(\n    treeNodeId: TreeNodeId,\n    result: TraversalResultFullyVisited\n  ): void {\n    const cacheKey = serializeTreeNodeId(treeNodeId);\n    this.fullyVisitedCache.set(cacheKey, result);\n  }\n\n  addPartiallyVisitedResult(\n    treeNodeId: TreeNodeId,\n    result: TraversalResultPartiallyVisited\n  ): void {\n    const cacheKey = serializeTreeNodeId(treeNodeId);\n    const dependenciesByDepth =\n      this.partiallyVisitedCache.get(cacheKey) ?? new Map();\n    if (!this.partiallyVisitedCache.has(cacheKey)) {\n      this.partiallyVisitedCache.set(cacheKey, dependenciesByDepth);\n    }\n\n    dependenciesByDepth.set(result.depth, result.dependencies);\n  }\n}\n"],
  "mappings": "AACA,SAAS,2BAA4C;AAyE9C,MAAM,kBAAkB;AAAA,EACZ,oBAAoB,oBAAI,IAGvC;AAAA;AAAA;AAAA;AAAA,EAKe,wBAAwB,oBAAI,IAG3C;AAAA,EAEF,IAAI,MAA2D;AAC7D,UAAM,WAAW,oBAAoB,KAAK,QAAQ;AAQlD,UAAM,oBAAoB,KAAK,kBAAkB,IAAI,QAAQ;AAC7D,QACE,sBAAsB,UACtB,kBAAkB,UAAU,KAAK,gBACjC;AACA,aAAO;AAAA,QACL,cAAc,kBAAkB;AAAA,QAChC,QAAQ,kBAAkB;AAAA,QAC1B,UAAU;AAAA,MACZ;AAAA,IACF;AAEA,UAAM,wBAAwB,KAAK,sBAChC,IAAI,QAAQ,GACX,IAAI,KAAK,cAAc;AAC3B,QAAI,yBAAyB,MAAM;AACjC,aAAO;AAAA,QACL,cAAc;AAAA,QACd,QAAQ;AAAA,QACR,UAAU;AAAA,MACZ;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,sBACE,YACA,QACM;AACN,UAAM,WAAW,oBAAoB,UAAU;AAC/C,SAAK,kBAAkB,IAAI,UAAU,MAAM;AAAA,EAC7C;AAAA,EAEA,0BACE,YACA,QACM;AACN,UAAM,WAAW,oBAAoB,UAAU;AAC/C,UAAM,sBACJ,KAAK,sBAAsB,IAAI,QAAQ,KAAK,oBAAI,IAAI;AACtD,QAAI,CAAC,KAAK,sBAAsB,IAAI,QAAQ,GAAG;AAC7C,WAAK,sBAAsB,IAAI,UAAU,mBAAmB;AAAA,IAC9D;AAEA,wBAAoB,IAAI,OAAO,OAAO,OAAO,YAAY;AAAA,EAC3D;AACF;",
  "names": []
}
