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

Inconsistent store #43

Merged
merged 10 commits into from
Feb 27, 2018
3 changes: 3 additions & 0 deletions packages/aws-appsync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### vNext
- Inconsistent store [PR#43](https://github.com/awslabs/aws-mobile-appsync-sdk-js/pull/43)

### 1.0.11
- Add setimmediate as a dependency [PR#47](https://github.com/awslabs/aws-mobile-appsync-sdk-js/pull/47)

Expand Down
27 changes: 17 additions & 10 deletions packages/aws-appsync/src/cache/offline-cache.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*!
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* the License is located at
* http://aws.amazon.com/asl/
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import { Cache } from 'apollo-cache';
Expand All @@ -22,7 +22,7 @@ export default class MyCache extends InMemoryCache {
store;

/**
*
*
* @param {Store<NormalizedCache>} store
* @param {ApolloReducerConfig} config
*/
Expand All @@ -31,22 +31,29 @@ export default class MyCache extends InMemoryCache {

this.store = store;

store.subscribe(() => {
const { [NORMALIZED_CACHE_KEY]: normCache = {} } = this.store.getState();

this.cancelSubscription = store.subscribe(() => {
const { [NORMALIZED_CACHE_KEY]: normCache = {}, rehydrated = false } = this.store.getState();
super.restore({ ...normCache });
if (rehydrated) {
// console.log('Rehydrated! Cancelling subscription.');
this.cancelSubscription();
}
});
}

/**
*
*
* @param {Cache.WriteOptions} write
*/
write(write) {
super.write(write);
const data = super.extract(true);

this.store.dispatch(writeThunk(data));
if (this.data && typeof this.data.record === 'undefined') {
// do not persist contents of a RecordingCache
const data = super.extract(true);
this.store.dispatch(writeThunk(data));
} else {
// console.log('NO DISPATCH FOR RECORDINGCACHE')
}
}

reset() {
Expand Down
15 changes: 8 additions & 7 deletions packages/aws-appsync/src/client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*!
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* the License is located at
* http://aws.amazon.com/asl/
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import 'setimmediate';
Expand All @@ -27,8 +27,8 @@ class AWSAppSyncClient extends ApolloClient {
hydrated = () => this.hydratedPromise;

/**
*
* @param {string} url
*
* @param {string} url
* @param {ApolloClientOptions<InMemoryCache>} options
*/
constructor({ url, region, auth, conflictResolver, complexObjectsCredentials, disableOffline = false }, options) {
Expand Down Expand Up @@ -88,20 +88,20 @@ class AWSAppSyncClient extends ApolloClient {
}

/**
*
*
* @param {MutationOptions} options
* @returns {Promise<FetchResult>}
*/
mutate(options) {
const { refetchQueries, context: origContext = {}, ...otherOptions } = options;
const { update, refetchQueries, context: origContext = {}, ...otherOptions } = options;
const { AASContext: { doIt = false, ...restAASContext } = {} } = origContext;

const context = {
...origContext,
AASContext: {
doIt,
...restAASContext,
...(!doIt ? { refetchQueries } : {}),
...(!doIt ? { refetchQueries, update } : {}),
}
};

Expand All @@ -112,6 +112,7 @@ class AWSAppSyncClient extends ApolloClient {
const newOptions = {
...otherOptions,
optimisticResponse: data,
update,
...(doIt ? { refetchQueries } : {}),
context,
}
Expand Down
30 changes: 15 additions & 15 deletions packages/aws-appsync/src/link/offline-link.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*!
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of
* the License is located at
* http://aws.amazon.com/asl/
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import { readQueryFromStore, defaultNormalizedCacheFactory } from "apollo-cache-inmemory";
Expand All @@ -22,8 +22,8 @@ export class OfflineLink extends ApolloLink {
store;

/**
*
* @param {Store} store
*
* @param {Store} store
*/
constructor(store) {
this.store = store;
Expand Down Expand Up @@ -82,9 +82,9 @@ export class OfflineLink extends ApolloLink {
}

/**
*
* @param {Operation} operation
* @param {Store} theStore
*
* @param {Operation} operation
* @param {Store} theStore
*/
const processOfflineQuery = (operation, theStore) => {
const { [NORMALIZED_CACHE_KEY]: normalizedCache = {} } = theStore.getState();
Expand All @@ -102,13 +102,13 @@ const processOfflineQuery = (operation, theStore) => {
}

/**
*
* @param {Operation} operation
*
* @param {Operation} operation
* @param {Store} theStore
*/
const enqueueMutation = (operation, theStore, observer) => {
const { query: mutation, variables, update } = operation;
const { cache, optimisticResponse, AASContext: { doIt = false, refetchQueries } = {} } = operation.getContext();
const { query: mutation, variables } = operation;
const { cache, optimisticResponse, AASContext: { doIt = false, refetchQueries, update } = {} } = operation.getContext();

setImmediate(() => {
theStore.dispatch({
Expand All @@ -134,10 +134,10 @@ const enqueueMutation = (operation, theStore, observer) => {
}

/**
*
* @param {*} client
* @param {*} effect
* @param {*} action
*
* @param {*} client
* @param {*} effect
* @param {*} action
*/
export const offlineEffect = (client, effect, action) => {
const doIt = true;
Expand Down