Skip to content

Commit

Permalink
fix(data): update DataStore to send correct Control Messages when sta…
Browse files Browse the repository at this point in the history
…rting (#12861)
  • Loading branch information
david-mcafee authored Jan 19, 2024
1 parent 5fc446e commit 551a58c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"type": "node",
"request": "launch",
// The debugger will only run tests for the package specified here:
"cwd": "${workspaceFolder}/packages/api-graphql",
"cwd": "${workspaceFolder}/packages/datastore",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
// Optionally specify a single test file to run/debug:
"AWSAppSyncRealTimeProvider.test.ts",
"connectivityHandling.test.ts",
"--runInBand",
"--testTimeout",
"600000", // 10 min timeout so jest doesn't error while we're stepping through code
Expand Down
17 changes: 17 additions & 0 deletions packages/datastore/__tests__/conflictResolutionBehavior.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { warpTime, unwarpTime, pause } from './helpers';
import { UpdateSequenceHarness } from './helpers/UpdateSequenceHarness';
import { isNode } from '../src/datastore/utils';

/**
* When DataStore starts, DataStore will send Control Messages based on the
* environment that tell it what to do. In this testing environment, both
* `isNode` and `isBrowser` incorrectly evaluate to `true`. Here we set `isNode`
* to `false`, without mocking the other utils.
*/
jest.mock('../src/datastore/utils', () => {
const originalModule = jest.requireActual('../src/datastore/utils');

return {
__esModule: true,
...originalModule,
isNode: () => false,
};
});

/**
* NOTE:
Expand Down
17 changes: 17 additions & 0 deletions packages/datastore/__tests__/connectivityHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ import {
} from './helpers';
import { Predicates } from '../src/predicates';
import { syncExpression } from '../src/types';
import { isNode } from '../src/datastore/utils';

/**
* When DataStore starts, DataStore will send Control Messages based on the
* environment that tell it what to do. In this testing environment, both
* `isNode` and `isBrowser` incorrectly evaluate to `true`. Here we set `isNode`
* to `false`, without mocking the other utils.
*/
jest.mock('../src/datastore/utils', () => {
const originalModule = jest.requireActual('../src/datastore/utils');

return {
__esModule: true,
...originalModule,
isNode: () => false,
};
});

/**
* Surfaces errors sooner and outputs them more clearly if/when
Expand Down
13 changes: 9 additions & 4 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
} from '../types';
// tslint:disable:no-duplicate-imports
import type { __modelMeta__ } from '../types';
import { isNode } from './utils';

import {
DATASTORE,
Expand Down Expand Up @@ -1553,10 +1554,14 @@ class DataStore {
.start({ fullSyncInterval: fullSyncIntervalInMilliseconds })
.subscribe({
next: ({ type, data }) => {
// In the Browser, we can begin returning data once subscriptions are in place.
const readyType = isBrowser()
? ControlMessage.SYNC_ENGINE_STORAGE_SUBSCRIBED
: ControlMessage.SYNC_ENGINE_SYNC_QUERIES_READY;
/**
* In Node, we need to wait for queries to be synced to prevent returning empty arrays.
* In non-Node environments (the browser or React Native), 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) {
this.initResolve();
Expand Down
7 changes: 7 additions & 0 deletions packages/datastore/src/datastore/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export const isNode = () =>
typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null;

0 comments on commit 551a58c

Please sign in to comment.