From b6e2d721066fe3372f0f5280d125dd6afc499d74 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 15 Apr 2020 16:15:18 -0700 Subject: [PATCH 01/19] Add jest config to aws-amplify --- packages/aws-amplify/package.json | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index 83785e0d5aa..ecf42c315fb 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -46,5 +46,47 @@ "@aws-amplify/storage": "^3.1.6", "@aws-amplify/ui": "^2.0.2", "@aws-amplify/xr": "^2.1.6" + }, + "jest": { + "globals": { + "ts-jest": { + "diagnostics": false, + "tsConfig": { + "lib": [ + "es5", + "es2015", + "dom", + "esnext.asynciterable", + "es2017.object" + ], + "allowJs": true + } + } + }, + "transform": { + "^.+\\.(js|jsx|ts|tsx)$": "ts-jest" + }, + "preset": "ts-jest", + "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", + "moduleFileExtensions": [ + "ts", + "tsx", + "js", + "json", + "jsx" + ], + "testEnvironment": "jsdom", + "testURL": "http://localhost/", + "coverageThreshold": { + "global": { + "branches": 0, + "functions": 0, + "lines": 0, + "statements": 0 + } + }, + "coveragePathIgnorePatterns": [ + "/node_modules/" + ] } } From 1158231825db1a821212e20e0f91a9ae7784d5d4 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 15 Apr 2020 16:15:30 -0700 Subject: [PATCH 02/19] Add exports-test.ts --- .../aws-amplify/src/__tests__/exports-test.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 packages/aws-amplify/src/__tests__/exports-test.ts diff --git a/packages/aws-amplify/src/__tests__/exports-test.ts b/packages/aws-amplify/src/__tests__/exports-test.ts new file mode 100644 index 00000000000..3994ade929a --- /dev/null +++ b/packages/aws-amplify/src/__tests__/exports-test.ts @@ -0,0 +1,94 @@ +import * as exported from '..'; + +describe('aws-amplify', () => { + describe('import * as exported', () => { + it('should match snapshot', () => { + expect(Object.keys(exported)).toMatchInlineSnapshot(` + Array [ + "Amplify", + "Analytics", + "AWSPinpointProvider", + "AWSKinesisProvider", + "AWSKinesisFirehoseProvider", + "AmazonPersonalizeProvider", + "Auth", + "Storage", + "StorageClass", + "API", + "APIClass", + "graphqlOperation", + "PubSub", + "Cache", + "Interactions", + "a", + "button", + "signInButton", + "googleSignInButton", + "signInButtonIcon", + "auth0SignInButton", + "facebookSignInButton", + "amazonSignInButton", + "oAuthSignInButton", + "signInButtonContent", + "formContainer", + "formSection", + "formField", + "formRow", + "hint", + "input", + "inputLabel", + "label", + "radio", + "navBar", + "navRight", + "nav", + "navItem", + "photoPickerButton", + "photoPlaceholder", + "photoPlaceholderIcon", + "container", + "actionRow", + "sectionHeader", + "sectionHeaderHint", + "sectionBody", + "sectionHeaderContent", + "sectionFooter", + "sectionFooterPrimaryContent", + "sectionFooterSecondaryContent", + "selectInput", + "strike", + "strikeContent", + "toast", + "toastClose", + "totpQrcode", + "sumerianSceneContainer", + "sumerianScene", + "loadingOverlay", + "loadingContainer", + "loadingLogo", + "loadingSceneName", + "loadingBar", + "loadingBarFill", + "sceneErrorText", + "sceneBar", + "sceneName", + "sceneActions", + "actionButton", + "tooltip", + "actionIcon", + "autoShowTooltip", + "XR", + "Predictions", + "Logger", + "Hub", + "JS", + "ClientDevice", + "Signer", + "I18n", + "ServiceWorker", + "default", + ] + `); + }); + }); +}); From a07e0accac043b186891205f2672607283c56b41 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 15 Apr 2020 16:16:40 -0700 Subject: [PATCH 03/19] aws-amplify has named export DataStore --- packages/aws-amplify/src/__tests__/exports-test.ts | 1 + packages/aws-amplify/src/index.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/aws-amplify/src/__tests__/exports-test.ts b/packages/aws-amplify/src/__tests__/exports-test.ts index 3994ade929a..2a4b36f94b6 100644 --- a/packages/aws-amplify/src/__tests__/exports-test.ts +++ b/packages/aws-amplify/src/__tests__/exports-test.ts @@ -17,6 +17,7 @@ describe('aws-amplify', () => { "API", "APIClass", "graphqlOperation", + "DataStore", "PubSub", "Cache", "Interactions", diff --git a/packages/aws-amplify/src/index.ts b/packages/aws-amplify/src/index.ts index 67ee516358a..9f3e8e2addd 100644 --- a/packages/aws-amplify/src/index.ts +++ b/packages/aws-amplify/src/index.ts @@ -33,6 +33,7 @@ export { export { Auth } from '@aws-amplify/auth'; export { Storage, StorageClass } from '@aws-amplify/storage'; export { API, APIClass, graphqlOperation } from '@aws-amplify/api'; +export { DataStore } from '@aws-amplify/datastore'; export { PubSub } from '@aws-amplify/pubsub'; export { default as Cache } from '@aws-amplify/cache'; export { Interactions } from '@aws-amplify/interactions'; From 7dd9dde94e9c10439ab85aa9301f5028186879df Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 15 Apr 2020 16:43:23 -0700 Subject: [PATCH 04/19] Move __tests__ so build script ignores them --- packages/aws-amplify/{src => }/__tests__/exports-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename packages/aws-amplify/{src => }/__tests__/exports-test.ts (98%) diff --git a/packages/aws-amplify/src/__tests__/exports-test.ts b/packages/aws-amplify/__tests__/exports-test.ts similarity index 98% rename from packages/aws-amplify/src/__tests__/exports-test.ts rename to packages/aws-amplify/__tests__/exports-test.ts index 2a4b36f94b6..7410e789aae 100644 --- a/packages/aws-amplify/src/__tests__/exports-test.ts +++ b/packages/aws-amplify/__tests__/exports-test.ts @@ -1,4 +1,4 @@ -import * as exported from '..'; +import * as exported from '../src'; describe('aws-amplify', () => { describe('import * as exported', () => { From aaa31f5c5ed6f7b10592860a4b4a5abd93a4aa69 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 15 Apr 2020 16:48:24 -0700 Subject: [PATCH 05/19] Add Predicates from DataStore --- packages/aws-amplify/__tests__/exports-test.ts | 3 ++- packages/aws-amplify/src/index.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/aws-amplify/__tests__/exports-test.ts b/packages/aws-amplify/__tests__/exports-test.ts index 7410e789aae..243ac72a3d3 100644 --- a/packages/aws-amplify/__tests__/exports-test.ts +++ b/packages/aws-amplify/__tests__/exports-test.ts @@ -1,7 +1,7 @@ import * as exported from '../src'; describe('aws-amplify', () => { - describe('import * as exported', () => { + describe('import * keys', () => { it('should match snapshot', () => { expect(Object.keys(exported)).toMatchInlineSnapshot(` Array [ @@ -18,6 +18,7 @@ describe('aws-amplify', () => { "APIClass", "graphqlOperation", "DataStore", + "Predicates", "PubSub", "Cache", "Interactions", diff --git a/packages/aws-amplify/src/index.ts b/packages/aws-amplify/src/index.ts index 9f3e8e2addd..4e8f336b8b4 100644 --- a/packages/aws-amplify/src/index.ts +++ b/packages/aws-amplify/src/index.ts @@ -33,7 +33,7 @@ export { export { Auth } from '@aws-amplify/auth'; export { Storage, StorageClass } from '@aws-amplify/storage'; export { API, APIClass, graphqlOperation } from '@aws-amplify/api'; -export { DataStore } from '@aws-amplify/datastore'; +export { DataStore, Predicates } from '@aws-amplify/datastore'; export { PubSub } from '@aws-amplify/pubsub'; export { default as Cache } from '@aws-amplify/cache'; export { Interactions } from '@aws-amplify/interactions'; From 9ce2624984cdb33e45c6acaad60ac9908af942b8 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Thu, 16 Apr 2020 16:31:38 -0700 Subject: [PATCH 06/19] Mark datastore as having sideEffects (initSchema) --- packages/datastore/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/datastore/package.json b/packages/datastore/package.json index ac0269eb69b..bcae163faef 100644 --- a/packages/datastore/package.json +++ b/packages/datastore/package.json @@ -11,7 +11,7 @@ "publishConfig": { "access": "public" }, - "sideEffects": false, + "sideEffects": true, "scripts": { "test": "tslint 'src/**/*.ts' && jest -w 1 --coverage", "build-with-test": "npm test && npm run build", From a733a4ea535756d15811a5de4d4f59e5538f4aec Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Thu, 16 Apr 2020 16:45:26 -0700 Subject: [PATCH 07/19] Node is always "online" --- packages/core/src/Util/Reachability.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/core/src/Util/Reachability.ts b/packages/core/src/Util/Reachability.ts index 3ea69a85ab6..a47abc76aa5 100644 --- a/packages/core/src/Util/Reachability.ts +++ b/packages/core/src/Util/Reachability.ts @@ -1,3 +1,4 @@ +import { JS } from '@aws-amplify/core'; import Observable from 'zen-observable-ts'; type NetworkStatus = { @@ -6,6 +7,14 @@ type NetworkStatus = { export default class ReachabilityNavigator implements Reachability { networkMonitor(): Observable { + if (JS.browserOrNode().isNode) { + return new Observable(observer => { + observer.next({ online: true }); + + return () => {}; + }); + } + return new Observable(observer => { observer.next({ online: window.navigator.onLine }); From b1e41c89eea5981b025dad605214d105f4b37ddb Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Thu, 16 Apr 2020 16:46:31 -0700 Subject: [PATCH 08/19] getDefaultAdapter uses fake-indexeddb for Node --- packages/datastore/package.json | 4 ++-- .../src/storage/adapter/getDefaultAdapter/index.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/datastore/package.json b/packages/datastore/package.json index bcae163faef..48de8607939 100644 --- a/packages/datastore/package.json +++ b/packages/datastore/package.json @@ -35,13 +35,13 @@ }, "homepage": "https://aws-amplify.github.io/", "devDependencies": { - "@types/uuid": "3.4.5", - "fake-indexeddb": "3.0.0" + "@types/uuid": "3.4.5" }, "dependencies": { "@aws-amplify/api": "^3.1.6", "@aws-amplify/core": "^3.2.3", "@aws-amplify/pubsub": "^3.0.7", + "fake-indexeddb": "3.0.0", "idb": "4.0.4", "immer": "6.0.1", "uuid": "3.3.2", diff --git a/packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts b/packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts index 8e09cdfafbc..67cf5307213 100644 --- a/packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts +++ b/packages/datastore/src/storage/adapter/getDefaultAdapter/index.ts @@ -1,11 +1,15 @@ +import { JS } from '@aws-amplify/core'; import { Adapter } from '..'; const getDefaultAdapter: () => Adapter = () => { - if (window.indexedDB) { + const { isBrowser, isNode } = JS.browserOrNode(); + if (isNode) { + require('fake-indexeddb/auto'); return require('../indexeddb').default; } - if (process && process.env) { - throw new Error('Node is not supported'); + + if (isBrowser && window.indexedDB) { + return require('../indexeddb').default; } }; From fea8126ac48e96c299bf34ab2e7451bd8a861c79 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Thu, 16 Apr 2020 17:02:37 -0700 Subject: [PATCH 09/19] Node support for WebSocket (via isomorphic-ws) --- packages/datastore/package.json | 2 ++ .../src/Providers/AmazonAIConvertPredictionsProvider.ts | 2 ++ packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts | 1 + 3 files changed, 5 insertions(+) diff --git a/packages/datastore/package.json b/packages/datastore/package.json index 48de8607939..ad2658540f7 100644 --- a/packages/datastore/package.json +++ b/packages/datastore/package.json @@ -44,7 +44,9 @@ "fake-indexeddb": "3.0.0", "idb": "4.0.4", "immer": "6.0.1", + "isomorphic-ws": "^4.0.1", "uuid": "3.3.2", + "ws": "^7.2.3", "zen-observable-ts": "0.8.19", "zen-push": "0.2.1" }, diff --git a/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts b/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts index 16228431b86..f2418127b85 100644 --- a/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts +++ b/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts @@ -24,6 +24,8 @@ import { MessageHeaderValue, } from '@aws-sdk/eventstream-marshaller'; import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-node'; +import WebSocket from 'isomorphic-ws'; + const logger = new Logger('AmazonAIConvertPredictionsProvider'); const eventBuilder = new EventStreamMarshaller(toUtf8, fromUtf8); diff --git a/packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts b/packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts index a9d49ce267c..9da286dcfb0 100644 --- a/packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts +++ b/packages/pubsub/src/Providers/AWSAppSyncRealTimeProvider.ts @@ -28,6 +28,7 @@ import { } from '@aws-amplify/core'; import Cache from '@aws-amplify/cache'; import Auth from '@aws-amplify/auth'; +import WebSocket from 'isomorphic-ws'; import { AbstractPubSubProvider } from './PubSubProvider'; import { CONTROL_MSG } from '../index'; From 8dd5fe2b2a2bd2740744fba86a731dc2ad606f61 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 11:37:11 -0700 Subject: [PATCH 10/19] Warn when schema has been initialized rather than throw HMR will re-run imports, so sideEffects have to be graceful --- packages/datastore/src/datastore/datastore.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index faa7a9a4d74..df61e18e43d 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -97,7 +97,9 @@ let storageClasses: TypeConstructorMap; const initSchema = (userSchema: Schema) => { if (schema !== undefined) { - throw new Error('The schema has already been initialized'); + console.warn('The schema has already been initialized'); + + return userClasses; } logger.log('validating schema', { schema: userSchema }); From 0baf79629adfff64a3c76818ae6997e48735d06d Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 11:38:08 -0700 Subject: [PATCH 11/19] Add model.fromJSON([] | {}) for creating existing instances from JSON --- packages/datastore/src/datastore/datastore.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index df61e18e43d..c9b8e1764e5 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -323,6 +323,24 @@ const createModelClass = ( draft.id = source.id; }); } + + static fromJSON(json: T | T[]) { + if (Array.isArray(json)) { + return json.map(init => this.fromJSON(init)); + } + + const { id, ...init } = json; + + // ! This is a double-init because `id` can't be provided via `new`. + return produce( + new clazz(init as ModelInit), + (draft: Draft) => { + initializeInstance(init, modelDefinition, draft); + + draft.id = id; + } + ); + } }); clazz[immerable] = true; From 10a81414d9b49356ad2d0df08b91408f77e9642f Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 11:56:21 -0700 Subject: [PATCH 12/19] Revert "Mark datastore as having sideEffects (initSchema)" This reverts commit 9ce2624984cdb33e45c6acaad60ac9908af942b8. --- packages/datastore/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/datastore/package.json b/packages/datastore/package.json index ad2658540f7..5c2828964de 100644 --- a/packages/datastore/package.json +++ b/packages/datastore/package.json @@ -11,7 +11,7 @@ "publishConfig": { "access": "public" }, - "sideEffects": true, + "sideEffects": false, "scripts": { "test": "tslint 'src/**/*.ts' && jest -w 1 --coverage", "build-with-test": "npm test && npm run build", From 4733b5ccbe7a12ad6b9dc5015453da22c07bbcda Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 12:05:56 -0700 Subject: [PATCH 13/19] Remove `isomorphic-ws` from Predictions until formally supported & tested --- .../src/Providers/AmazonAIConvertPredictionsProvider.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts b/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts index f2418127b85..695777d90b2 100644 --- a/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts +++ b/packages/predictions/src/Providers/AmazonAIConvertPredictionsProvider.ts @@ -24,7 +24,6 @@ import { MessageHeaderValue, } from '@aws-sdk/eventstream-marshaller'; import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-node'; -import WebSocket from 'isomorphic-ws'; const logger = new Logger('AmazonAIConvertPredictionsProvider'); const eventBuilder = new EventStreamMarshaller(toUtf8, fromUtf8); From 644a5f10faeed7dbcacdeaf900cfc8b7866723b4 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 14:45:01 -0700 Subject: [PATCH 14/19] Use modelInstanceCreator for fromJSON --- packages/datastore/src/datastore/datastore.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index c9b8e1764e5..1a54608a6d2 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -329,17 +329,7 @@ const createModelClass = ( return json.map(init => this.fromJSON(init)); } - const { id, ...init } = json; - - // ! This is a double-init because `id` can't be provided via `new`. - return produce( - new clazz(init as ModelInit), - (draft: Draft) => { - initializeInstance(init, modelDefinition, draft); - - draft.id = id; - } - ); + return modelInstanceCreator(clazz, json); } }); From a267d05e662d69cff6c4fea6d27fc2fc1add3304 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Fri, 17 Apr 2020 15:16:58 -0700 Subject: [PATCH 15/19] Add DataStore.toJSON() --- packages/datastore/src/datastore/datastore.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index 1a54608a6d2..095f5cc5aff 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -886,6 +886,10 @@ function getNamespace(): SchemaNamespace { return namespace; } +const toJSON = (model: T | T[]): JSON => { + return JSON.parse(JSON.stringify(model)); +}; + class DataStore { static getModuleName() { return 'DataStore'; @@ -896,6 +900,7 @@ class DataStore { static observe = observe; static configure = configure; static clear = clear; + static toJSON = toJSON; } export { initSchema, DataStore }; From 6f92a3401517d2f7f8a8915d6473c08a7dc131d1 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Wed, 20 May 2020 11:20:36 -0700 Subject: [PATCH 16/19] Simplify with Observerable.from --- packages/core/src/Util/Reachability.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/core/src/Util/Reachability.ts b/packages/core/src/Util/Reachability.ts index a47abc76aa5..963accfdec9 100644 --- a/packages/core/src/Util/Reachability.ts +++ b/packages/core/src/Util/Reachability.ts @@ -8,11 +8,7 @@ type NetworkStatus = { export default class ReachabilityNavigator implements Reachability { networkMonitor(): Observable { if (JS.browserOrNode().isNode) { - return new Observable(observer => { - observer.next({ online: true }); - - return () => {}; - }); + return Observable.from({ online: true }); } return new Observable(observer => { From fb25ade83f58fab10410813ff231d42616158d5c Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Tue, 16 Jun 2020 10:41:55 -0700 Subject: [PATCH 17/19] Fix TypeScript error with Observable.from({ online: true }) Argument of type '{ online: boolean; }' is not assignable to parameter of type 'Observable | ObservableLike | ArrayLike'. Object literal may only specify known properties, and 'online' does not exist in type 'Observable | ObservableLike | ArrayLike'.ts(2345) --- packages/core/src/Util/Reachability.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Util/Reachability.ts b/packages/core/src/Util/Reachability.ts index da64cda46ae..be015c11539 100644 --- a/packages/core/src/Util/Reachability.ts +++ b/packages/core/src/Util/Reachability.ts @@ -12,7 +12,9 @@ export default class ReachabilityNavigator implements Reachability { networkMonitor(netInfo?: any): Observable { if (JS.browserOrNode().isNode) { - return Observable.from({ online: true }); + return new Observable(observer => { + observer.next({ online: true }); + }); } return new Observable(observer => { From bf5d5d5914498b753b64e08da28e7eca3417565a Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Tue, 16 Jun 2020 11:20:07 -0700 Subject: [PATCH 18/19] DataStore awaits SYNC_ENGINE_SYNC_QUERIES_READY when in Node --- packages/datastore/src/datastore/datastore.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/datastore/src/datastore/datastore.ts b/packages/datastore/src/datastore/datastore.ts index 8f81c7fe1f1..d497200c180 100644 --- a/packages/datastore/src/datastore/datastore.ts +++ b/packages/datastore/src/datastore/datastore.ts @@ -1,4 +1,4 @@ -import { Amplify, ConsoleLogger as Logger, Hub } from '@aws-amplify/core'; +import { Amplify, ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core'; import { Draft, immerable, produce, setAutoFreeze } from 'immer'; import { v4 as uuid4 } from 'uuid'; import Observable, { ZenObservable } from 'zen-observable-ts'; @@ -52,6 +52,7 @@ setAutoFreeze(true); const logger = new Logger('DataStore'); const ulid = monotonicUlidFactory(Date.now()); +const { isNode } = JS.browserOrNode(); declare class Setting { constructor(init: ModelInit); @@ -871,7 +872,13 @@ async function start(): Promise { .start({ fullSyncInterval: fullSyncIntervalInMilliseconds }) .subscribe({ next: ({ type, data }) => { - if (type === ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED) { + // In Node, we need to wait for queries to be synced to prevent returning empty arrays. + // In the Browser, we can begin returning data once subscriptions are in place. + const readyType = isNode + ? ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY + : ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED; + + if (type === readyType) { initResolve(); } From a34bd42b256905bbf1073906295909726bdbafa2 Mon Sep 17 00:00:00 2001 From: Eric Clemmons Date: Tue, 16 Jun 2020 12:01:27 -0700 Subject: [PATCH 19/19] Correct `Observable.from` usage in 6f92a34 --- packages/core/src/Util/Reachability.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/Util/Reachability.ts b/packages/core/src/Util/Reachability.ts index be015c11539..e279ca51997 100644 --- a/packages/core/src/Util/Reachability.ts +++ b/packages/core/src/Util/Reachability.ts @@ -12,9 +12,7 @@ export default class ReachabilityNavigator implements Reachability { networkMonitor(netInfo?: any): Observable { if (JS.browserOrNode().isNode) { - return new Observable(observer => { - observer.next({ online: true }); - }); + return Observable.from([{ online: true }]); } return new Observable(observer => {