From 70c9c1ad16b8d83fc59dbb1ceedfdd68d4d3958a Mon Sep 17 00:00:00 2001 From: rowan Date: Fri, 2 May 2025 13:09:35 -0500 Subject: [PATCH] add caching for inventory searching --- src/inventory.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/inventory.ts b/src/inventory.ts index 965e9dd..c58dfeb 100644 --- a/src/inventory.ts +++ b/src/inventory.ts @@ -43,12 +43,24 @@ class ItemInstance { } export default class Inventory extends Node { + private static cache = new Map() + items: Map = new Map() static find_inventory(root: Node): Inventory | null | undefined { + if (Inventory.cache.has(root)) { + return Inventory.cache.get(root) + } + const child_enumerator: GArrayEnumerator = new GArrayEnumerator(root.get_children()) const children = Enumerable.from(child_enumerator) - return children.find(child => child instanceof Inventory) as Inventory + const inventory = children.find(child => child instanceof Inventory) as Inventory + + if (inventory != null) { + Inventory.cache.set(root, inventory) + } + + return inventory } add(item: ItemData, quantity: number = 1) {