Skip to content

Commit

Permalink
Improve generic type inference for cache.watch.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Sep 17, 2021
1 parent 0a6dfa5 commit bb1bda1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export type Transaction<T> = (c: ApolloCache<T>) => void;
export abstract class ApolloCache<TSerialized> implements DataProxy {
// required to implement
// core API
public abstract read<T, TVariables = any>(
query: Cache.ReadOptions<TVariables, T>,
): T | null;
public abstract write<TResult = any, TVariables = any>(
write: Cache.WriteOptions<TResult, TVariables>,
public abstract read<TData = any, TVariables = any>(
query: Cache.ReadOptions<TVariables, TData>,
): TData | null;
public abstract write<TData = any, TVariables = any>(
write: Cache.WriteOptions<TData, TVariables>,
): Reference | undefined;
public abstract diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T>;
public abstract watch(watch: Cache.WatchOptions): () => void;
public abstract watch<TData = any, TVariables = any>(
watch: Cache.WatchOptions<TData, TVariables>,
): () => void;
public abstract reset(): Promise<void>;

// Remove whole objects from the cache by passing just options.id, or
Expand Down
22 changes: 13 additions & 9 deletions src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Modifier, Modifiers } from './common';
import { ApolloCache } from '../cache';

export namespace Cache {
export type WatchCallback = (
diff: Cache.DiffResult<any>,
lastDiff?: Cache.DiffResult<any>,
export type WatchCallback<TData = any> = (
diff: Cache.DiffResult<TData>,
lastDiff?: Cache.DiffResult<TData>,
) => void;

export interface ReadOptions<TVariables = any, TData = any>
Expand All @@ -25,19 +25,23 @@ export namespace Cache {
result: TResult;
}

export interface DiffOptions extends ReadOptions {
export interface DiffOptions<
TData = any,
TVariables = any,
> extends ReadOptions<TVariables, TData> {
// The DiffOptions interface is currently just an alias for
// ReadOptions, though DiffOptions used to be responsible for
// declaring the returnPartialData option.
}

export interface WatchOptions<
Watcher extends object = Record<string, any>
> extends ReadOptions {
watcher?: Watcher;
TData = any,
TVariables = any,
> extends ReadOptions<TVariables, TData> {
watcher?: object;
immediate?: boolean;
callback: WatchCallback;
lastDiff?: DiffResult<any>;
callback: WatchCallback<TData>;
lastDiff?: DiffResult<TData>;
}

export interface EvictOptions {
Expand Down
8 changes: 6 additions & 2 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}
}

public diff<T>(options: Cache.DiffOptions): Cache.DiffResult<T> {
public diff<TData, TVariables = any>(
options: Cache.DiffOptions<TData, TVariables>,
): Cache.DiffResult<TData> {
return this.storeReader.diffQueryAgainstStore({
...options,
store: options.optimistic ? this.optimisticData : this.data,
Expand All @@ -256,7 +258,9 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
});
}

public watch(watch: Cache.WatchOptions): () => void {
public watch<TData = any, TVariables = any>(
watch: Cache.WatchOptions<TData, TVariables>,
): () => void {
if (!this.watches.size) {
// In case we previously called forgetCache(this) because
// this.watches became empty (see below), reattach this cache to any
Expand Down
4 changes: 2 additions & 2 deletions src/core/QueryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ export class QueryInfo {
// updateWatch method.
private cancel() {}

private lastWatch?: Cache.WatchOptions<QueryInfo>;
private lastWatch?: Cache.WatchOptions;

private updateWatch(variables = this.variables) {
const oq = this.observableQuery;
if (oq && oq.options.fetchPolicy === "no-cache") {
return;
}

const watchOptions: Cache.WatchOptions<QueryInfo> = {
const watchOptions: Cache.WatchOptions = {
// Although this.getDiffOptions returns Cache.DiffOptions instead of
// Cache.WatchOptions, all the overlapping options should be the same, so
// we can reuse getDiffOptions here, for consistency.
Expand Down

0 comments on commit bb1bda1

Please sign in to comment.