From b8ffa7a5324ceaa68681ff3c1fe7603859ef4024 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Mon, 19 Feb 2024 15:50:33 +0000 Subject: [PATCH] fix: better types for cache and dataloader (#318) --- packages/cache/src/index.ts | 18 +++++++++--------- packages/dataloader/src/MultiKeyMap.ts | 20 ++++++++++---------- packages/dataloader/src/batch.ts | 2 +- packages/dataloader/src/types.ts | 26 ++++++++++++-------------- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/packages/cache/src/index.ts b/packages/cache/src/index.ts index 35e2f087..d0184941 100644 --- a/packages/cache/src/index.ts +++ b/packages/cache/src/index.ts @@ -137,14 +137,14 @@ export interface CacheRealm { * Creates a new cache within the realm. All caches within * a realm share the same capacity and eviction queue. */ - createCache( + createCache: ( options: CacheOptions, - ): Cache; + ) => Cache; /** * Replicates a cache delete/clear from another server. */ - writeReplicationEvent(event: ReplicationEvent): void; + writeReplicationEvent: (event: ReplicationEvent) => void; } /** @@ -166,7 +166,7 @@ export interface Cache { * * Returns undefined if the entry is not in the cache. */ - get(key: TKey): TValue | undefined; + get: (key: TKey) => TValue | undefined; /** * If the an item with this key is already in the cache, @@ -179,13 +179,13 @@ export interface Cache { * If the cache realm is full, the least recently used item will * be evicted. */ - set(key: TKey, value: TValue): TValue; + set: (key: TKey, value: TValue) => TValue; /** * Delete items from the cache and remove them from the * eviction queue. */ - delete(...keys: TKey[]): void; + delete: (...keys: TKey[]) => void; /** * Delete items where the serialized key has a given prefix. @@ -193,18 +193,18 @@ export interface Cache { * This will throw a runtime error if any keys are not * serialized to strings. */ - deletePrefix(prefix: string): void; + deletePrefix: (prefix: string) => void; /** * Clear all items from the cache and remove them from the * eviction queue. */ - clear(): void; + clear: () => void; /** * Dispose of the cache and remove it from the realm. */ - dispose(): void; + dispose: () => void; } type SerializedKey = {__type: 'SerializedKey'}; diff --git a/packages/dataloader/src/MultiKeyMap.ts b/packages/dataloader/src/MultiKeyMap.ts index 91bafe6e..2dd3f337 100644 --- a/packages/dataloader/src/MultiKeyMap.ts +++ b/packages/dataloader/src/MultiKeyMap.ts @@ -11,10 +11,10 @@ type SubPath = TKeys extends readonly [ export interface MultiKeyMap extends CacheMap { readonly size: number; - get(key: TKeys): TValue | undefined; - set(key: TKeys, value: TValue): void; - delete(key: TKeys | SubPath): void; - clear(): void; + get: (key: TKeys) => TValue | undefined; + set: (key: TKeys, value: TValue) => void; + delete: (key: TKeys | SubPath) => void; + clear: () => void; } export interface MultiKeyMapOptionWithMapKey< @@ -59,10 +59,10 @@ export type MultiKeyMapOptions< }; interface NormalizedMultiKeyMapOptions { - getCache( + getCache: ( depth: number, - ): CacheMapInput>; - getKeyMap(depth: number): (key: TKeys[number]) => TMappedKey; + ) => CacheMapInput>; + getKeyMap: (depth: number) => (key: TKeys[number]) => TMappedKey; } const identityFn = (arg: T): T => arg; const DEFAULT_OPTIONS: NormalizedMultiKeyMapOptions = { @@ -159,9 +159,9 @@ const DELETE_NOOP = new DeleteResult(0, false); interface MultiKeyMapNode { get size(): number; - get(keys: TKeys): TValue | undefined; - set(keys: TKeys, value: TValue): boolean; - delete(keys: TKeys | SubPath): DeleteResult; + get: (keys: TKeys) => TValue | undefined; + set: (keys: TKeys, value: TValue) => boolean; + delete: (keys: TKeys | SubPath) => DeleteResult; } class BranchNode diff --git a/packages/dataloader/src/batch.ts b/packages/dataloader/src/batch.ts index 23aa26c9..dca8e350 100644 --- a/packages/dataloader/src/batch.ts +++ b/packages/dataloader/src/batch.ts @@ -7,7 +7,7 @@ export type BatchResponseFunction = ( keys: TKey[], ) => Promise | TResult; export type BatchResponseMap = { - get(key: TKey): Promise | TResult; + get: (key: TKey) => Promise | TResult; }; export type BatchResponseArray = readonly ( | Promise diff --git a/packages/dataloader/src/types.ts b/packages/dataloader/src/types.ts index 091468de..1ca46942 100644 --- a/packages/dataloader/src/types.ts +++ b/packages/dataloader/src/types.ts @@ -1,23 +1,21 @@ export interface CacheMapInput { readonly size?: number; - get(key: TKey): TValue | undefined; - set(key: TKey, value: TValue): unknown; - delete(key: TKey): unknown; - clear?(): unknown; + get: (key: TKey) => TValue | undefined; + set: (key: TKey, value: TValue) => unknown; + delete: (key: TKey) => unknown; + clear?: () => unknown; } - export interface CacheMap { readonly size?: number; - get(key: TKey): TValue | undefined; - set(key: TKey, value: TValue): void; - delete(key: TKey): void; - clear(): void; + get: (key: TKey) => TValue | undefined; + set: (key: TKey, value: TValue) => void; + delete: (key: TKey) => void; + clear: () => void; } - export interface AsyncCacheMap { readonly size?: number; - get(key: TKey): Promise | undefined; - set(key: TKey, value: Promise | TValue): void; - delete(key: TKey): void; - clear(): void; + get: (key: TKey) => Promise | undefined; + set: (key: TKey, value: Promise | TValue) => void; + delete: (key: TKey) => void; + clear: () => void; }