Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom network detection #499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/aws-appsync/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ import { createRetryLink } from './link/retry-link';
import { boundEnqueueDeltaSync, buildSync, DELTASYNC_KEY, hashForOptions } from "./deltaSync";
import { Subscription } from 'apollo-client/util/Observable';
import { PERMANENT_ERROR_KEY } from './link/retry-link';
import { OfflineStatusChangeCallbackCreator } from './store';
import defaultOfflineConfig from '@redux-offline/redux-offline/lib/defaults';

const { detectNetwork: defaultDetectNetwork } = defaultOfflineConfig;

export { defaultDataIdFromObject };

class CatchErrorLink extends ApolloLink {

private link: ApolloLink;

constructor(linkGenerator: () => ApolloLink) {
try {
super();
Expand Down Expand Up @@ -146,6 +149,7 @@ export interface AWSAppSyncClientOptions {
cacheOptions?: ApolloReducerConfig,
disableOffline?: boolean,
offlineConfig?: OfflineConfig,
detectNetwork?: OfflineStatusChangeCallbackCreator,
}

export type OfflineConfig = Pick<Partial<StoreOptions<any>>, 'storage' | 'callback' | 'keyPrefix'> & {
Expand Down Expand Up @@ -193,6 +197,7 @@ class AWSAppSyncClient<TCacheShape extends NormalizedCacheObject> extends Apollo
callback = () => { },
storeCacheRootMutation = false,
} = {},
detectNetwork = defaultDetectNetwork,
}: AWSAppSyncClientOptions, options?: Partial<ApolloClientOptions<TCacheShape>>) {
const { cache: customCache = undefined, link: customLink = undefined } = options || {};

Expand All @@ -216,7 +221,8 @@ class AWSAppSyncClient<TCacheShape extends NormalizedCacheObject> extends Apollo
dataIdFromObject,
storage,
keyPrefix,
callback
callback,
detectNetwork,
});
const cache: ApolloCache<any> = disableOffline
? (customCache || new InMemoryCache(cacheOptions))
Expand Down
6 changes: 4 additions & 2 deletions packages/aws-appsync/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { OfflineAction, NetInfo, NetworkCallback } from '@redux-offline/redux-of
import { offlineEffectConfig as deltaSyncConfig } from "./deltaSync";
import { Observable } from 'apollo-link';

const { detectNetwork } = defaultOfflineConfig;

const logger = rootLogger.extend('store');

Expand All @@ -30,6 +29,7 @@ export type StoreOptions<TCacheShape extends NormalizedCacheObject> = {
keyPrefix?: string,
storage?: any,
callback: OfflineCallback,
detectNetwork?: OfflineStatusChangeCallbackCreator
};

export const DEFAULT_KEY_PREFIX = REDUX_PERSIST_KEY_PREFIX;
Expand All @@ -41,6 +41,7 @@ const newStore = <TCacheShape extends NormalizedCacheObject>({
keyPrefix,
storage,
callback = () => { },
detectNetwork,
}: StoreOptions<TCacheShape>): Store<any> => {
logger('Creating store');

Expand Down Expand Up @@ -83,9 +84,10 @@ const newStore = <TCacheShape extends NormalizedCacheObject>({
store,
clientGetter(),
callback,
detectNetwork as OfflineStatusChangeCallbackCreator
detectNetwork
),
discard: (error, action, retries) => discard(callback, error, action, retries),
detectNetwork
})
)
);
Expand Down