Skip to content

Commit

Permalink
Fixed issue where multiple calls to connectDataConnectEmulator throws…
Browse files Browse the repository at this point in the history
… an error
  • Loading branch information
maneesht committed Dec 3, 2024
1 parent ffbf5a6 commit a2c6bea
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/auth/src/platform_browser/strategies/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ class PopupOperation extends AbstractPopupRedirectOperation {

private pollUserCancellation(): void {
const poll = (): void => {
if(typeof window !== 'undefined' && this.authWindow?.window?.location.host !== window.location.host) {
console.error('Unable to determine whether window was closed or not');
}
if (this.authWindow?.window?.closed) {
// Make sure that there is sufficient time for whatever action to
// complete. The window could have closed but the sign in network
Expand Down
7 changes: 6 additions & 1 deletion packages/data-connect/src/api/DataConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class DataConnect {

// @internal
enableEmulator(transportOptions: TransportOptions): void {
if (this._initialized) {
if (this._initialized && !areTransportOptionsEqual(this._transportOptions, transportOptions)) {
logError('enableEmulator called after initialization');
throw new DataConnectError(
Code.ALREADY_INITIALIZED,
Expand All @@ -191,6 +191,11 @@ export class DataConnect {
}
}

// @internal
export function areTransportOptionsEqual(transportOptions1: TransportOptions, transportOptions2: TransportOptions) {

Check failure on line 195 in packages/data-connect/src/api/DataConnect.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function

Check failure on line 195 in packages/data-connect/src/api/DataConnect.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Chrome and Node

Missing return type on function

Check failure on line 195 in packages/data-connect/src/api/DataConnect.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Firefox

Missing return type on function

Check failure on line 195 in packages/data-connect/src/api/DataConnect.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Webkit

Missing return type on function
return JSON.stringify(transportOptions1) === JSON.stringify(transportOptions2);
}

/**
* Connect to the DataConnect Emulator
* @param dc Data Connect instance
Expand Down
22 changes: 17 additions & 5 deletions packages/data-connect/src/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,39 @@ export function queryRef<Data, Variables>(
queryName: string,
variables: Variables
): QueryRef<Data, Variables>;

export function queryRef<Data, Variables>(
dcInstance: DataConnect,
serializedRef: SerializedRef<Data, Variables>
): QueryRef<Data, Variables>;

/**
* Execute Query
* @param dcInstance Data Connect instance to use.
* @param queryName Query to execute
* @param queryNameOrSerializedRef Query to execute
* @param variables Variables to execute with
* @param initialCache initial cache to use for client hydration
* @returns `QueryRef`
*/
export function queryRef<Data, Variables>(
dcInstance: DataConnect,
queryName: string,
queryNameOrSerializedRef: string | SerializedRef<Data, Variables>,
variables?: Variables,
initialCache?: QueryResult<Data, Variables>
): QueryRef<Data, Variables> {
dcInstance.setInitialized();
dcInstance._queryManager.track(queryName, variables, initialCache);
if(typeof queryNameOrSerializedRef === 'string') {
dcInstance._queryManager.track(queryNameOrSerializedRef, variables, initialCache);
} else {
dcInstance._queryManager.track(queryNameOrSerializedRef.refInfo.name, queryNameOrSerializedRef.refInfo.variables, queryNameOrSerializedRef);
}
const vars = typeof queryNameOrSerializedRef !== 'string'? queryNameOrSerializedRef.refInfo.variables : variables;
const name = typeof queryNameOrSerializedRef !== 'string'? queryNameOrSerializedRef.refInfo.name : queryNameOrSerializedRef;
return {
dataConnect: dcInstance,
refType: QUERY_STR,
name: queryName,
variables: variables as Variables
name,
variables: vars
};
}
/**
Expand Down
13 changes: 12 additions & 1 deletion packages/data-connect/test/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import {
subscribe,
terminate,
SOURCE_CACHE,
SOURCE_SERVER
SOURCE_SERVER,
toQueryRef
} from '../src';

import { setupQueries } from './emulatorSeeder';
import { getConnectionConfig, initDatabase, PROJECT_ID } from './util';
import { serialize } from 'v8';

use(chaiAsPromised);

Expand Down Expand Up @@ -136,6 +138,15 @@ describe('DataConnect Tests', async () => {
expect(result.data).to.eq(queryResult.data);
expect(result.source).to.eq(SOURCE_CACHE);
});

it(`returns the result source as cache when data already exists`, async () => {
const taskListQuery = queryRef<TaskListResponse>(dc, 'listPosts');
const queryResult = await executeQuery(taskListQuery);
const serializedRef = queryResult.toJSON();
const newRef = queryRef(dc, serializedRef);
expect(newRef.name).to.eq(serializedRef.refInfo.name);
expect(newRef.variables).to.eq(serializedRef.refInfo.variables);
});
it(`returns the proper JSON when calling .toJSON()`, async () => {
const taskListQuery = queryRef<TaskListResponse>(dc, 'listPosts');
await executeQuery(taskListQuery);
Expand Down
52 changes: 52 additions & 0 deletions packages/data-connect/test/unit/transportoptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { TransportOptions, areTransportOptionsEqual, connectDataConnectEmulator, getDataConnect} from '../../src/api/DataConnect';
import { app } from '../util';
import { queryRef } from '../../src';
describe.only('Transport Options', () => {
it('should return false if transport options are not equal', () => {

Check failure on line 6 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'describe.only' is restricted from being used

Check failure on line 6 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Chrome and Node

'describe.only' is restricted from being used

Check failure on line 6 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Firefox

'describe.only' is restricted from being used

Check failure on line 6 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Webkit

'describe.only' is restricted from being used
const transportOptions1: TransportOptions = {
host: 'h',
port: 1,
sslEnabled: false
};
const transportOptions2: TransportOptions = {
host: 'h2',
port: 2,
sslEnabled: false
};
expect(areTransportOptionsEqual(transportOptions1, transportOptions2)).to.eq(false);
});
it('should return true if transport options are equal', () => {
const transportOptions1: TransportOptions = {
host: 'h',
port: 1,
sslEnabled: false
};
const transportOptions2: TransportOptions = {
host: 'h',
port: 1,
sslEnabled: false
};
expect(areTransportOptionsEqual(transportOptions1, transportOptions2)).to.eq(true);
});
it.only('should throw if emulator is connected to with new transport options', () => {
const dc = getDataConnect(app, {

Check failure on line 33 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'it.only' is restricted from being used

Check failure on line 33 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Chrome and Node

'it.only' is restricted from being used

Check failure on line 33 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Firefox

'it.only' is restricted from being used

Check failure on line 33 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Webkit

'it.only' is restricted from being used
connector: 'c',
location: 'l',
service: 's'
});
expect(() => connectDataConnectEmulator(dc, 'h', 80, false)).to.not.throw();
queryRef(dc, 'query');
expect(() => connectDataConnectEmulator(dc, 'h2', 80, false)).to.throw('DataConnect instance already initialized!');
});
it.only('should not throw if emulator is connected to with the same transport options', () => {
const dc = getDataConnect(app, {

Check failure on line 43 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'it.only' is restricted from being used

Check failure on line 43 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Chrome and Node

'it.only' is restricted from being used

Check failure on line 43 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Firefox

'it.only' is restricted from being used

Check failure on line 43 in packages/data-connect/test/unit/transportoptions.test.ts

View workflow job for this annotation

GitHub Actions / Test Packages With Changed Files in Webkit

'it.only' is restricted from being used
connector: 'c',
location: 'l',
service: 's'
});
expect(() => connectDataConnectEmulator(dc, 'h', 80, false)).to.not.throw();
queryRef(dc, 'query');
expect(() => connectDataConnectEmulator(dc, 'h', 80, false)).to.not.throw();
});
});

0 comments on commit a2c6bea

Please sign in to comment.