Skip to content

Commit

Permalink
Promote BatchOptions from ApolloCache module to Cache.BatchOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Apr 20, 2021
1 parent ad87f2b commit 73b5818
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
27 changes: 1 addition & 26 deletions src/cache/core/cache.ts
Expand Up @@ -11,31 +11,6 @@ import { Cache } from './types/Cache';

export type Transaction<T> = (c: ApolloCache<T>) => void;

export type BatchOptions<C extends ApolloCache<any>> = {
// 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<any>,
oldDiff?: Cache.DiffResult<any>,
) => any;
};

export abstract class ApolloCache<TSerialized> implements DataProxy {
// required to implement
// core API
Expand Down Expand Up @@ -84,7 +59,7 @@ export abstract class ApolloCache<TSerialized> 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<this>) {
public batch(options: Cache.BatchOptions<this>) {
const optimisticId =
typeof options.optimistic === "string" ? options.optimistic :
options.optimistic === false ? null : void 0;
Expand Down
34 changes: 34 additions & 0 deletions 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 = (
Expand Down Expand Up @@ -52,6 +53,39 @@ export namespace Cache {
broadcast?: boolean;
}

export interface BatchOptions<C extends ApolloCache<any>> {
// 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<any>,
lastDiff: Cache.DiffResult<any> | undefined,
) => any;
}

export import DiffResult = DataProxy.DiffResult;
export import ReadQueryOptions = DataProxy.ReadQueryOptions;
export import ReadFragmentOptions = DataProxy.ReadFragmentOptions;
Expand Down
6 changes: 3 additions & 3 deletions src/cache/inmemory/inMemoryCache.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -37,7 +37,7 @@ export interface InMemoryCacheConfig extends ApolloReducerConfig {
}

type BroadcastOptions = Pick<
BatchOptions<InMemoryCache>,
Cache.BatchOptions<InMemoryCache>,
| "optimistic"
| "onWatchUpdated"
>
Expand Down Expand Up @@ -302,7 +302,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {

private txCount = 0;

public batch(options: BatchOptions<InMemoryCache>) {
public batch(options: Cache.BatchOptions<InMemoryCache>) {
const {
transaction,
optimistic = true,
Expand Down

0 comments on commit 73b5818

Please sign in to comment.