diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index cf9cc79cc00..92dd9eaf5e3 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -11,31 +11,6 @@ import { Cache } from './types/Cache'; export type Transaction = (c: ApolloCache) => void; -export type BatchOptions> = { - // Same as the first parameter of performTransaction, except the cache - // argument will have the subclass type rather than ApolloCache. - transaction(cache: C): void; - - // Passing a string for this option creates a new optimistic layer with - // that string as its layer.id, just like passing a string for the - // optimisticId parameter of performTransaction. Passing true is the - // same as passing undefined to performTransaction, and passing false is - // the same as passing null. - optimistic: string | boolean; - - removeOptimistic?: string; - - // If you want to find out which watched queries were invalidated during - // this batch operation, pass this optional callback function. Returning - // false from the callback will prevent broadcasting this result. - onWatchUpdated?: ( - this: C, - watch: Cache.WatchOptions, - newDiff: Cache.DiffResult, - oldDiff?: Cache.DiffResult, - ) => any; -}; - export abstract class ApolloCache implements DataProxy { // required to implement // core API @@ -84,7 +59,7 @@ export abstract class ApolloCache implements DataProxy { // provide a default batch implementation that's just another way of calling // performTransaction. Subclasses of ApolloCache (such as InMemoryCache) can // override the batch method to do more interesting things with its options. - public batch(options: BatchOptions) { + public batch(options: Cache.BatchOptions) { const optimisticId = typeof options.optimistic === "string" ? options.optimistic : options.optimistic === false ? null : void 0; diff --git a/src/cache/core/types/Cache.ts b/src/cache/core/types/Cache.ts index 88432f120b1..deddf94421d 100644 --- a/src/cache/core/types/Cache.ts +++ b/src/cache/core/types/Cache.ts @@ -1,5 +1,6 @@ import { DataProxy } from './DataProxy'; import { Modifier, Modifiers } from './common'; +import { ApolloCache } from '../cache'; export namespace Cache { export type WatchCallback = ( @@ -52,6 +53,39 @@ export namespace Cache { broadcast?: boolean; } + export interface BatchOptions> { + // Same as the first parameter of performTransaction, except the cache + // argument will have the subclass type rather than ApolloCache. + transaction(cache: C): void; + + // Passing a string for this option creates a new optimistic layer, with the + // given string as its layer.id, just like passing a string for the + // optimisticId parameter of performTransaction. Passing true is the same as + // passing undefined to performTransaction (runing the batch operation + // against the current top layer of the cache), and passing false is the + // same as passing null (running the operation against root/non-optimistic + // cache data). + optimistic: string | boolean; + + // If you specify the ID of an optimistic layer using this option, that + // layer will be removed as part of the batch transaction, triggering at + // most one broadcast for both the transaction and the removal of the layer. + // Note: this option is needed because calling cache.removeOptimistic during + // the transaction function may not be not safe, since any modifications to + // cache layers may be discarded after the transaction finishes. + removeOptimistic?: string; + + // If you want to find out which watched queries were invalidated during + // this batch operation, pass this optional callback function. Returning + // false from the callback will prevent broadcasting this result. + onWatchUpdated?: ( + this: C, + watch: Cache.WatchOptions, + diff: Cache.DiffResult, + lastDiff: Cache.DiffResult | undefined, + ) => any; + } + export import DiffResult = DataProxy.DiffResult; export import ReadQueryOptions = DataProxy.ReadQueryOptions; export import ReadFragmentOptions = DataProxy.ReadFragmentOptions; diff --git a/src/cache/inmemory/inMemoryCache.ts b/src/cache/inmemory/inMemoryCache.ts index b5fee375c4b..782ee1b1a23 100644 --- a/src/cache/inmemory/inMemoryCache.ts +++ b/src/cache/inmemory/inMemoryCache.ts @@ -5,7 +5,7 @@ import { DocumentNode } from 'graphql'; import { wrap } from 'optimism'; import { equal } from '@wry/equality'; -import { ApolloCache, BatchOptions } from '../core/cache'; +import { ApolloCache } from '../core/cache'; import { Cache } from '../core/types/Cache'; import { MissingFieldError } from '../core/types/common'; import { @@ -37,7 +37,7 @@ export interface InMemoryCacheConfig extends ApolloReducerConfig { } type BroadcastOptions = Pick< - BatchOptions, + Cache.BatchOptions, | "optimistic" | "onWatchUpdated" > @@ -302,7 +302,7 @@ export class InMemoryCache extends ApolloCache { private txCount = 0; - public batch(options: BatchOptions) { + public batch(options: Cache.BatchOptions) { const { transaction, optimistic = true,