diff --git a/engine/lib/cache.test.ts b/engine/lib/cache.test.ts index a6868e5..5e6d4c5 100644 --- a/engine/lib/cache.test.ts +++ b/engine/lib/cache.test.ts @@ -1,10 +1,5 @@ import { expect, test, vi } from "vitest"; -import { - type Cache, - memoize, - withCache, - withCacheAsync, -} from "#/engine/lib/cache.ts"; +import { type Cache, memoize, withCache } from "#/engine/lib/cache.ts"; test("withCache", () => { const cache: Cache = new Map(); @@ -28,18 +23,18 @@ test("withCache", () => { expect(fn).toHaveBeenCalledTimes(3); }); -test("withCacheAsync", async () => { +test("async withCache", async () => { const cache: Cache> = new Map(); const fn = vi.fn(async (value: string): Promise => { return parseFloat(value); }); - const callback = withCacheAsync(fn, cache); + const callback = withCache(fn, cache); const one = "1"; - const res = callback("one", one); - const _ = callback("one", one); + const res = await callback("one", one); + const _ = await callback("one", one); - expect(await res).toBe(1); + expect(res).toBe(1); expect(fn).toHaveBeenCalledTimes(1); expect(await callback("two", "2")).toBe(2); @@ -78,10 +73,10 @@ test("async memoize", async () => { const [callback, cache] = memoize(fn); const one = "1"; - const res = callback(one); - const _ = callback(one); + const res = await callback(one); + const _ = await callback(one); - expect(await res).toBe(1); + expect(res).toBe(1); expect(fn).toHaveBeenCalledTimes(1); expect(await callback("2.0")).toBe(2); diff --git a/engine/lib/cache.ts b/engine/lib/cache.ts index af9e6f5..bff12ee 100644 --- a/engine/lib/cache.ts +++ b/engine/lib/cache.ts @@ -22,25 +22,6 @@ export function withCache) => T>( }; } -export function withCacheAsync< - T, - U extends (...args: Parameters) => Promise, ->( - fn: U, - cache: Cache>, -): (key: unknown, ...args: Parameters) => Promise { - return async (key, ...args) => { - if (cache.has(key)) { - return (await cache.get(key)) as T; - } - - const value = fn(...args); - cache.set(key, value); - - return await value; - }; -} - export function memoize(fn: (arg: U) => T): [(arg: U) => T, Cache] { const cache = new Map(); const callback = withCache(fn, cache);