Skip to content

Commit

Permalink
fix: better types for cache and dataloader (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay authored Feb 19, 2024
1 parent 161265c commit b8ffa7a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
18 changes: 9 additions & 9 deletions packages/cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, TValue>(
createCache: <TKey, TValue>(
options: CacheOptions<TKey, TValue>,
): Cache<TKey, TValue>;
) => Cache<TKey, TValue>;

/**
* Replicates a cache delete/clear from another server.
*/
writeReplicationEvent(event: ReplicationEvent): void;
writeReplicationEvent: (event: ReplicationEvent) => void;
}

/**
Expand All @@ -166,7 +166,7 @@ export interface Cache<TKey, TValue> {
*
* 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,
Expand All @@ -179,32 +179,32 @@ export interface Cache<TKey, TValue> {
* 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.
*
* 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'};
Expand Down
20 changes: 10 additions & 10 deletions packages/dataloader/src/MultiKeyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type SubPath<TKeys extends readonly unknown[]> = TKeys extends readonly [
export interface MultiKeyMap<TKeys extends Path, TValue>
extends CacheMap<TKeys, TValue> {
readonly size: number;
get(key: TKeys): TValue | undefined;
set(key: TKeys, value: TValue): void;
delete(key: TKeys | SubPath<TKeys>): void;
clear(): void;
get: (key: TKeys) => TValue | undefined;
set: (key: TKeys, value: TValue) => void;
delete: (key: TKeys | SubPath<TKeys>) => void;
clear: () => void;
}

export interface MultiKeyMapOptionWithMapKey<
Expand Down Expand Up @@ -59,10 +59,10 @@ export type MultiKeyMapOptions<
};

interface NormalizedMultiKeyMapOptions<TKeys extends Path, TValue, TMappedKey> {
getCache(
getCache: (
depth: number,
): CacheMapInput<TMappedKey, MultiKeyMapNode<TKeys, TValue>>;
getKeyMap(depth: number): (key: TKeys[number]) => TMappedKey;
) => CacheMapInput<TMappedKey, MultiKeyMapNode<TKeys, TValue>>;
getKeyMap: (depth: number) => (key: TKeys[number]) => TMappedKey;
}
const identityFn = <T>(arg: T): T => arg;
const DEFAULT_OPTIONS: NormalizedMultiKeyMapOptions<any, any, any> = {
Expand Down Expand Up @@ -159,9 +159,9 @@ const DELETE_NOOP = new DeleteResult(0, false);

interface MultiKeyMapNode<TKeys extends Path, TValue> {
get size(): number;
get(keys: TKeys): TValue | undefined;
set(keys: TKeys, value: TValue): boolean;
delete(keys: TKeys | SubPath<TKeys>): DeleteResult;
get: (keys: TKeys) => TValue | undefined;
set: (keys: TKeys, value: TValue) => boolean;
delete: (keys: TKeys | SubPath<TKeys>) => DeleteResult;
}

class BranchNode<TKeys extends Path, TValue, TMappedKey>
Expand Down
2 changes: 1 addition & 1 deletion packages/dataloader/src/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type BatchResponseFunction<TKey, TResult> = (
keys: TKey[],
) => Promise<TResult> | TResult;
export type BatchResponseMap<TKey, TResult> = {
get(key: TKey): Promise<TResult> | TResult;
get: (key: TKey) => Promise<TResult> | TResult;
};
export type BatchResponseArray<TResult> = readonly (
| Promise<TResult>
Expand Down
26 changes: 12 additions & 14 deletions packages/dataloader/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
export interface CacheMapInput<TKey, TValue> {
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<TKey, TValue> {
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<TKey, TValue> {
readonly size?: number;
get(key: TKey): Promise<TValue> | undefined;
set(key: TKey, value: Promise<TValue> | TValue): void;
delete(key: TKey): void;
clear(): void;
get: (key: TKey) => Promise<TValue> | undefined;
set: (key: TKey, value: Promise<TValue> | TValue) => void;
delete: (key: TKey) => void;
clear: () => void;
}

0 comments on commit b8ffa7a

Please sign in to comment.