From f51987231f7716519cc2cd325c4c8e5447dcb578 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Fri, 10 Nov 2023 11:05:16 +0100 Subject: [PATCH 1/9] make the `Cache` class used configurable --- src/cache.ts | 8 +++++++- src/index.ts | 32 ++++++++++++++++++++++++++------ src/tests/api.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 9338ab5e..1a4791b6 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -1,3 +1,5 @@ +import type { CommonCache } from "."; + interface Node { key: K; value: V; @@ -7,7 +9,7 @@ interface Node { function defaultDispose() {} -export class Cache { +export class Cache implements CommonCache { private map = new Map>(); private newest: Node | null = null; private oldest: Node | null = null; @@ -26,6 +28,10 @@ export class Cache { return node && node.value; } + public get size(): number { + return this.map.size + } + private getNode(key: K): Node | undefined { const node = this.map.get(key); diff --git a/src/index.ts b/src/index.ts index 8333aa8c..d73f12a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Trie } from "@wry/trie"; -import { Cache } from "./cache.js"; +import { Cache as DefaultCache } from "./cache.js"; import { Entry, AnyEntry } from "./entry.js"; import { parentEntrySlot } from "./context.js"; @@ -88,10 +88,28 @@ export type OptimisticWrapperFunction< makeCacheKey: (...args: TKeyArgs) => TCacheKey; }; +export interface CommonCache { + has(key: K): boolean; + get(key: K): V | undefined; + set(key: K, value: V): V; + delete(key: K): boolean; + clean(): void; + readonly size: number; +} + +export interface CommonCacheConstructor { + new >(max?: number, dispose?: (value: V, key?: K) => void): CommonCache; +} + +type NoInfer = [T][T extends any ? 0 : never]; + + + export type OptimisticWrapOptions< TArgs extends any[], TKeyArgs extends any[] = TArgs, TCacheKey = any, + TResult = any, > = { // The maximum number of cache entries that should be retained before the // cache begins evicting the oldest ones. @@ -102,13 +120,14 @@ export type OptimisticWrapOptions< // The makeCacheKey function takes the same arguments that were passed to // the wrapper function and returns a single value that can be used as a key // in a Map to identify the cached result. - makeCacheKey?: (...args: TKeyArgs) => TCacheKey; + makeCacheKey?: (...args: NoInfer) => TCacheKey; // If provided, the subscribe function should either return an unsubscribe // function or return nothing. subscribe?: (...args: TArgs) => void | (() => any); + Cache?: CommonCacheConstructor, NoInfer, NoInfer> }; -const caches = new Set>(); +const caches = new Set>(); export function wrap< TArgs extends any[], @@ -117,10 +136,11 @@ export function wrap< TCacheKey = any, >(originalFunction: (...args: TArgs) => TResult, { max = Math.pow(2, 16), - makeCacheKey = defaultMakeCacheKey, + makeCacheKey = (defaultMakeCacheKey as () => TCacheKey), keyArgs, subscribe, -}: OptimisticWrapOptions = Object.create(null)) { + Cache = DefaultCache +}: OptimisticWrapOptions = Object.create(null)) { const cache = new Cache>( max, entry => entry.dispose(), @@ -168,7 +188,7 @@ export function wrap< Object.defineProperty(optimistic, "size", { get() { - return cache["map"].size; + return cache.size; }, configurable: false, enumerable: false, diff --git a/src/tests/api.ts b/src/tests/api.ts index 1d6b8526..cb1366f8 100644 --- a/src/tests/api.ts +++ b/src/tests/api.ts @@ -4,6 +4,7 @@ import { wrap, defaultMakeCacheKey, OptimisticWrapperFunction, + CommonCache, } from "../index"; import { wrapYieldingFiberMethods } from '@wry/context'; import { dep } from "../dep"; @@ -36,6 +37,41 @@ describe("optimism", function () { assert.strictEqual(test("a"), "aNaCl"); }); + it("can manually set the `Cache` implementation", () => { + let cache!: Cache; + + class Cache implements CommonCache { + private _cache = new Map() + constructor() { + cache = this; + } + has = this._cache.has.bind(this._cache); + get = this._cache.get.bind(this._cache); + delete = this._cache.delete.bind(this._cache); + get size(){ return this._cache.size } + set(key: K, value: V): V { + this._cache.set(key, value); + return value; + } + clean(){}; + } + + const wrapped = wrap( + (obj: { value: string }) => obj.value + " transformed", + { + makeCacheKey(obj) { + return obj.value; + }, + Cache, + } + ); + assert.ok(cache instanceof Cache); + assert.strictEqual(wrapped({ value: "test" }), "test transformed"); + assert.strictEqual(wrapped({ value: "test" }), "test transformed"); + cache.get("test").value[0] = "test modified"; + assert.strictEqual(wrapped({ value: "test" }), "test modified"); + }); + it("works with two layers of functions", function () { const files: { [key: string]: string } = { "a.js": "a", From 57909462cc4f55499f09a8b161dbb382889c0510 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Thu, 16 Nov 2023 18:31:43 +0100 Subject: [PATCH 2/9] add `@wry/caches` dependency, use `StrongCache` as default --- package-lock.json | 20 ++++++++ package.json | 1 + src/cache.ts | 121 ---------------------------------------------- src/index.ts | 4 +- 4 files changed, 23 insertions(+), 123 deletions(-) delete mode 100644 src/cache.ts diff --git a/package-lock.json b/package-lock.json index 73e896cc..3b349d54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.17.5", "license": "MIT", "dependencies": { + "@wry/caches": "^1.0.0", "@wry/context": "^0.7.0", "@wry/trie": "^0.4.3", "tslib": "^2.3.0" @@ -35,6 +36,17 @@ "integrity": "sha512-JJulVEQXmiY9Px5axXHeYGLSjhkZEnD+MDPDGbCbIAbMslkKwmygtZFy1X6s/075Yo94sf8GuSlFfPzysQrWZQ==", "dev": true }, + "node_modules/@wry/caches": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@wry/caches/-/caches-1.0.0.tgz", + "integrity": "sha512-FHRUDe2tqrXAj6A/1D39No68lFWbbnh+NCpG9J/6idhL/2Mb/AaxBTYg/sbUVImEo8a4mWeOewUlB1W7uLjByA==", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/@wry/context": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/@wry/context/-/context-0.7.3.tgz", @@ -1194,6 +1206,14 @@ "integrity": "sha512-JJulVEQXmiY9Px5axXHeYGLSjhkZEnD+MDPDGbCbIAbMslkKwmygtZFy1X6s/075Yo94sf8GuSlFfPzysQrWZQ==", "dev": true }, + "@wry/caches": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@wry/caches/-/caches-1.0.0.tgz", + "integrity": "sha512-FHRUDe2tqrXAj6A/1D39No68lFWbbnh+NCpG9J/6idhL/2Mb/AaxBTYg/sbUVImEo8a4mWeOewUlB1W7uLjByA==", + "requires": { + "tslib": "^2.3.0" + } + }, "@wry/context": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/@wry/context/-/context-0.7.3.tgz", diff --git a/package.json b/package.json index 9a3a2e73..9fb4b49c 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "typescript": "^5.0.2" }, "dependencies": { + "@wry/caches": "^1.0.0", "@wry/context": "^0.7.0", "@wry/trie": "^0.4.3", "tslib": "^2.3.0" diff --git a/src/cache.ts b/src/cache.ts deleted file mode 100644 index 1a4791b6..00000000 --- a/src/cache.ts +++ /dev/null @@ -1,121 +0,0 @@ -import type { CommonCache } from "."; - -interface Node { - key: K; - value: V; - newer: Node | null; - older: Node | null; -} - -function defaultDispose() {} - -export class Cache implements CommonCache { - private map = new Map>(); - private newest: Node | null = null; - private oldest: Node | null = null; - - constructor( - private max = Infinity, - public dispose: (value: V, key: K) => void = defaultDispose, - ) {} - - public has(key: K): boolean { - return this.map.has(key); - } - - public get(key: K): V | undefined { - const node = this.getNode(key); - return node && node.value; - } - - public get size(): number { - return this.map.size - } - - private getNode(key: K): Node | undefined { - const node = this.map.get(key); - - if (node && node !== this.newest) { - const { older, newer } = node; - - if (newer) { - newer.older = older; - } - - if (older) { - older.newer = newer; - } - - node.older = this.newest; - node.older!.newer = node; - - node.newer = null; - this.newest = node; - - if (node === this.oldest) { - this.oldest = newer; - } - } - - return node; - } - - public set(key: K, value: V): V { - let node = this.getNode(key); - if (node) { - return node.value = value; - } - - node = { - key, - value, - newer: null, - older: this.newest - }; - - if (this.newest) { - this.newest.newer = node; - } - - this.newest = node; - this.oldest = this.oldest || node; - - this.map.set(key, node); - - return node.value; - } - - public clean() { - while (this.oldest && this.map.size > this.max) { - this.delete(this.oldest.key); - } - } - - public delete(key: K): boolean { - const node = this.map.get(key); - if (node) { - if (node === this.newest) { - this.newest = node.older; - } - - if (node === this.oldest) { - this.oldest = node.newer; - } - - if (node.newer) { - node.newer.older = node.older; - } - - if (node.older) { - node.older.newer = node.newer; - } - - this.map.delete(key); - this.dispose(node.value, key); - - return true; - } - - return false; - } -} diff --git a/src/index.ts b/src/index.ts index d73f12a3..1ad9d9cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Trie } from "@wry/trie"; -import { Cache as DefaultCache } from "./cache.js"; +import { StrongCache } from "@wry/caches"; import { Entry, AnyEntry } from "./entry.js"; import { parentEntrySlot } from "./context.js"; @@ -139,7 +139,7 @@ export function wrap< makeCacheKey = (defaultMakeCacheKey as () => TCacheKey), keyArgs, subscribe, - Cache = DefaultCache + Cache = StrongCache }: OptimisticWrapOptions = Object.create(null)) { const cache = new Cache>( max, From a087354c4105c440d2efd765aad0106a823c6e3e Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Thu, 16 Nov 2023 18:36:59 +0100 Subject: [PATCH 3/9] update import in tests --- src/tests/cache.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/tests/cache.ts b/src/tests/cache.ts index 89d67789..17a52236 100644 --- a/src/tests/cache.ts +++ b/src/tests/cache.ts @@ -1,9 +1,9 @@ import * as assert from "assert"; -import { Cache } from "../cache"; +import { StrongCache as Cache } from "@wry/caches"; describe("least-recently-used cache", function () { it("can hold lots of elements", function () { - const cache = new Cache; + const cache = new Cache(); const count = 1000000; for (let i = 0; i < count; ++i) { @@ -72,8 +72,10 @@ describe("least-recently-used cache", function () { if (sequence.length > 0) { assert.strictEqual((cache as any).newest.key, sequence[0]); - assert.strictEqual((cache as any).oldest.key, - sequence[sequence.length - 1]); + assert.strictEqual( + (cache as any).oldest.key, + sequence[sequence.length - 1] + ); } } From 88ed90bd262276007abeb79639f6a41042883bc8 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 16 Nov 2023 12:47:15 -0500 Subject: [PATCH 4/9] Import CommonCache from `@wry/caches` instead of declaring locally. --- src/index.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1ad9d9cf..2b0df402 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { Trie } from "@wry/trie"; -import { StrongCache } from "@wry/caches"; +import { StrongCache, CommonCache } from "@wry/caches"; import { Entry, AnyEntry } from "./entry.js"; import { parentEntrySlot } from "./context.js"; @@ -88,15 +88,7 @@ export type OptimisticWrapperFunction< makeCacheKey: (...args: TKeyArgs) => TCacheKey; }; -export interface CommonCache { - has(key: K): boolean; - get(key: K): V | undefined; - set(key: K, value: V): V; - delete(key: K): boolean; - clean(): void; - readonly size: number; -} - +export { CommonCache } export interface CommonCacheConstructor { new >(max?: number, dispose?: (value: V, key?: K) => void): CommonCache; } From f57dec535922a6463215c131dcff96bba08eef5b Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 16 Nov 2023 12:50:22 -0500 Subject: [PATCH 5/9] Move NoInfer type to helpers.ts. --- src/helpers.ts | 2 ++ src/index.ts | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 1a4dad50..0c01ebc6 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,3 +1,5 @@ +export type NoInfer = [T][T extends any ? 0 : never]; + export const { hasOwnProperty, } = Object.prototype; diff --git a/src/index.ts b/src/index.ts index 2b0df402..c8ec07a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { Trie } from "@wry/trie"; import { StrongCache, CommonCache } from "@wry/caches"; import { Entry, AnyEntry } from "./entry.js"; import { parentEntrySlot } from "./context.js"; +import type { NoInfer } from "./helpers.js"; // These helper functions are important for making optimism work with // asynchronous code. In order to register parent-child dependencies, @@ -93,10 +94,6 @@ export interface CommonCacheConstructor new >(max?: number, dispose?: (value: V, key?: K) => void): CommonCache; } -type NoInfer = [T][T extends any ? 0 : never]; - - - export type OptimisticWrapOptions< TArgs extends any[], TKeyArgs extends any[] = TArgs, From 789bdec16c6810dff30dbc5860a8489ba643ec9a Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 17 Nov 2023 12:50:53 -0500 Subject: [PATCH 6/9] Accept any CommonCache instance as optional cache option. --- src/index.ts | 13 ++++--------- src/tests/api.ts | 9 +++------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index c8ec07a7..915d1b3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -90,9 +90,6 @@ export type OptimisticWrapperFunction< }; export { CommonCache } -export interface CommonCacheConstructor { - new >(max?: number, dispose?: (value: V, key?: K) => void): CommonCache; -} export type OptimisticWrapOptions< TArgs extends any[], @@ -113,7 +110,7 @@ export type OptimisticWrapOptions< // If provided, the subscribe function should either return an unsubscribe // function or return nothing. subscribe?: (...args: TArgs) => void | (() => any); - Cache?: CommonCacheConstructor, NoInfer, NoInfer> + cache?: CommonCache, Entry, NoInfer>>; }; const caches = new Set>(); @@ -128,13 +125,11 @@ export function wrap< makeCacheKey = (defaultMakeCacheKey as () => TCacheKey), keyArgs, subscribe, - Cache = StrongCache -}: OptimisticWrapOptions = Object.create(null)) { - const cache = new Cache>( + cache = new StrongCache( max, entry => entry.dispose(), - ); - + ), +}: OptimisticWrapOptions = Object.create(null)) { const optimistic = function (): TResult { const key = makeCacheKey.apply( null, diff --git a/src/tests/api.ts b/src/tests/api.ts index cb1366f8..6cafa998 100644 --- a/src/tests/api.ts +++ b/src/tests/api.ts @@ -38,13 +38,8 @@ describe("optimism", function () { }); it("can manually set the `Cache` implementation", () => { - let cache!: Cache; - class Cache implements CommonCache { private _cache = new Map() - constructor() { - cache = this; - } has = this._cache.has.bind(this._cache); get = this._cache.get.bind(this._cache); delete = this._cache.delete.bind(this._cache); @@ -56,13 +51,15 @@ describe("optimism", function () { clean(){}; } + const cache = new Cache(); + const wrapped = wrap( (obj: { value: string }) => obj.value + " transformed", { + cache, makeCacheKey(obj) { return obj.value; }, - Cache, } ); assert.ok(cache instanceof Cache); From 76a371ee937a5b3438a803871745db135c6cfa0c Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 27 Nov 2023 19:38:09 -0500 Subject: [PATCH 7/9] Accept either cache instance or constructor for wrapOptions.cache. Answering "why not both?" to this discussion: https://github.com/benjamn/optimism/pull/615#discussion_r1396103379 --- src/index.ts | 39 +++++++++++++++++++++++++++--------- src/tests/api.ts | 51 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 915d1b3a..137cda8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,7 +55,7 @@ export type OptimisticWrapperFunction< readonly size: number; // Snapshot of wrap options used to create this wrapper function. - options: OptimisticWrapOptions; + options: OptionsWithCacheInstance; // "Dirty" any cached Entry stored for the given arguments, marking that Entry // and its ancestors as potentially needing to be recomputed. The .dirty(...) @@ -90,6 +90,9 @@ export type OptimisticWrapperFunction< }; export { CommonCache } +export interface CommonCacheConstructor extends Function { + new >(max?: number, dispose?: (value: V, key?: K) => void): CommonCache; +} export type OptimisticWrapOptions< TArgs extends any[], @@ -110,7 +113,17 @@ export type OptimisticWrapOptions< // If provided, the subscribe function should either return an unsubscribe // function or return nothing. subscribe?: (...args: TArgs) => void | (() => any); - cache?: CommonCache, Entry, NoInfer>>; + cache?: CommonCache, Entry, NoInfer>> + | CommonCacheConstructor, NoInfer, NoInfer>; +}; + +export interface OptionsWithCacheInstance< + TArgs extends any[], + TKeyArgs extends any[] = TArgs, + TCacheKey = any, + TResult = any, +> extends OptimisticWrapOptions { + cache: CommonCache, Entry, NoInfer>>; }; const caches = new Set>(); @@ -125,11 +138,20 @@ export function wrap< makeCacheKey = (defaultMakeCacheKey as () => TCacheKey), keyArgs, subscribe, - cache = new StrongCache( - max, - entry => entry.dispose(), - ), + cache: cacheOption, }: OptimisticWrapOptions = Object.create(null)) { + let cache: CommonCache>; + if (cacheOption) { + cache = typeof cacheOption === "function" + ? new cacheOption(max) + : cacheOption; + } else { + cache = new StrongCache>( + max, + entry => entry.dispose(), + ); + } + const optimistic = function (): TResult { const key = makeCacheKey.apply( null, @@ -171,9 +193,7 @@ export function wrap< } as OptimisticWrapperFunction; Object.defineProperty(optimistic, "size", { - get() { - return cache.size; - }, + get: () => cache.size, configurable: false, enumerable: false, }); @@ -183,6 +203,7 @@ export function wrap< makeCacheKey, keyArgs, subscribe, + cache, }); function dirtyKey(key: TCacheKey) { diff --git a/src/tests/api.ts b/src/tests/api.ts index 6cafa998..985765a8 100644 --- a/src/tests/api.ts +++ b/src/tests/api.ts @@ -37,7 +37,7 @@ describe("optimism", function () { assert.strictEqual(test("a"), "aNaCl"); }); - it("can manually set the `Cache` implementation", () => { + it("can manually specify a cache instance", () => { class Cache implements CommonCache { private _cache = new Map() has = this._cache.has.bind(this._cache); @@ -69,6 +69,36 @@ describe("optimism", function () { assert.strictEqual(wrapped({ value: "test" }), "test modified"); }); + it("can manually specify a cache constructor", () => { + class Cache implements CommonCache { + private _cache = new Map() + has = this._cache.has.bind(this._cache); + get = this._cache.get.bind(this._cache); + delete = this._cache.delete.bind(this._cache); + get size(){ return this._cache.size } + set(key: K, value: V): V { + this._cache.set(key, value); + return value; + } + clean(){}; + } + + const wrapped = wrap( + (obj: { value: string }) => obj.value + " transformed", + { + cache: Cache, + makeCacheKey(obj) { + return obj.value; + }, + } + ); + assert.ok(wrapped.options.cache instanceof Cache); + assert.strictEqual(wrapped({ value: "test" }), "test transformed"); + assert.strictEqual(wrapped({ value: "test" }), "test transformed"); + wrapped.options.cache.get("test").value[0] = "test modified"; + assert.strictEqual(wrapped({ value: "test" }), "test modified"); + }); + it("works with two layers of functions", function () { const files: { [key: string]: string } = { "a.js": "a", @@ -725,7 +755,7 @@ describe("optimism", function () { assert.strictEqual(sumFirst.forget(9), false); }); - it("exposes optimistic.size property, returning cache.map.size", function () { + it("exposes optimistic.{size,options.cache.size} properties", function () { const d = dep(); const fib = wrap((n: number): number => { d("shared"); @@ -736,7 +766,12 @@ describe("optimism", function () { }, }); - assert.strictEqual(fib.size, 0); + function size() { + assert.strictEqual(fib.options.cache.size, fib.size); + return fib.size; + } + + assert.strictEqual(size(), 0); assert.strictEqual(fib(0), 0); assert.strictEqual(fib(1), 1); @@ -748,22 +783,22 @@ describe("optimism", function () { assert.strictEqual(fib(7), 13); assert.strictEqual(fib(8), 21); - assert.strictEqual(fib.size, 9); + assert.strictEqual(size(), 9); fib.dirty(6); // Merely dirtying an Entry does not remove it from the LRU cache. - assert.strictEqual(fib.size, 9); + assert.strictEqual(size(), 9); fib.forget(6); // Forgetting an Entry both dirties it and removes it from the LRU cache. - assert.strictEqual(fib.size, 8); + assert.strictEqual(size(), 8); fib.forget(4); - assert.strictEqual(fib.size, 7); + assert.strictEqual(size(), 7); // This way of calling d.dirty causes any parent Entry objects to be // forgotten (removed from the LRU cache). d.dirty("shared", "forget"); - assert.strictEqual(fib.size, 0); + assert.strictEqual(size(), 0); }); }); From 2b36aa56d9125ee6412d38344bdc8a8c97ffecc6 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 27 Nov 2023 19:48:13 -0500 Subject: [PATCH 8/9] Bump npm version to 0.18.0-pre.0. --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3b349d54..ea8a0018 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "optimism", - "version": "0.17.5", + "version": "0.18.0-pre.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "optimism", - "version": "0.17.5", + "version": "0.18.0-pre.0", "license": "MIT", "dependencies": { "@wry/caches": "^1.0.0", diff --git a/package.json b/package.json index 9fb4b49c..5f136e28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "optimism", - "version": "0.17.5", + "version": "0.18.0-pre.0", "author": "Ben Newman ", "description": "Composable reactive caching with efficient invalidation.", "keywords": [ From f0133c0f95cb138ca71a96b35b4549695814a51e Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Tue, 28 Nov 2023 10:54:39 -0500 Subject: [PATCH 9/9] Simplify options.cache normalization logic. https://github.com/benjamn/optimism/pull/615#discussion_r1407530343 --- src/index.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 137cda8f..c640ff07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -138,19 +138,12 @@ export function wrap< makeCacheKey = (defaultMakeCacheKey as () => TCacheKey), keyArgs, subscribe, - cache: cacheOption, + cache: cacheOption = StrongCache, }: OptimisticWrapOptions = Object.create(null)) { - let cache: CommonCache>; - if (cacheOption) { - cache = typeof cacheOption === "function" - ? new cacheOption(max) + const cache: CommonCache> = + typeof cacheOption === "function" + ? new cacheOption(max, entry => entry.dispose()) : cacheOption; - } else { - cache = new StrongCache>( - max, - entry => entry.dispose(), - ); - } const optimistic = function (): TResult { const key = makeCacheKey.apply(