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

fix(core): fail to clean up framework observers #11782

Merged
Merged
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
2 changes: 1 addition & 1 deletion packages/api-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"name": "API (GraphQL client)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, GraphQLAPI }",
"limit": "89.51 kB"
"limit": "89.52 kB"
}
],
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"name": "API (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, API }",
"limit": "90.27 kB"
"limit": "90.28 kB"
}
],
"jest": {
Expand Down
45 changes: 42 additions & 3 deletions packages/core/__tests__/Platform-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import { detectFramework, clearCache } from '../src/Platform/detectFramework';
import * as detection from '../src/Platform/detection';

describe('Platform test', () => {
beforeEach(() => clearCache());
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd prefer using afterEach (see previous suggestion) for better isolation between tests, but not a blocker.

});

beforeEach(() => {
clearCache();
});

describe('isReactNative test', () => {
test('happy case', () => {
expect(Platform.isReactNative).toBe(false);
Expand Down Expand Up @@ -70,8 +81,6 @@ describe('Platform test', () => {

describe('detectFramework', () => {
test('retries detection after 10ms', () => {
jest.useFakeTimers();

jest.spyOn(detection, 'detect');

detectFramework();
Expand All @@ -81,3 +90,33 @@ describe('Platform test', () => {
});
});
});

describe('detectFramework observers', () => {
let module;

beforeAll(() => {
jest.resetModules();
module = require('../src/Platform/detectFramework');
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

HuiSF marked this conversation as resolved.
Show resolved Hide resolved
test('it notifies and cleans up the observers and rejects new observer after detection completes', () => {
const mockObserver = jest.fn();
module.observeFrameworkChanges(mockObserver);
expect(module.frameworkChangeObservers.length).toBe(1);

module.detectFramework();
expect(mockObserver).toHaveBeenCalledTimes(1);
jest.runOnlyPendingTimers();
module.detectFramework();
expect(mockObserver).toHaveBeenCalledTimes(2);
expect(module.frameworkChangeObservers.length).toBe(0);

module.observeFrameworkChanges(mockObserver);
expect(module.frameworkChangeObservers.length).toBe(0);
});
});
23 changes: 20 additions & 3 deletions packages/core/src/Platform/detectFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { detect } from './detection';
// We want to cache detection since the framework won't change
let frameworkCache: Framework | undefined;

const frameworkChangeObservers: (() => void)[] = [];
export const frameworkChangeObservers: (() => void)[] = [];

// Setup the detection reset tracking / timeout delays
let resetTriggered = false;
Expand All @@ -19,8 +19,19 @@ export const detectFramework = (): Framework => {
if (!frameworkCache) {
frameworkCache = detect();

// Everytime we update the cache, call each observer function
frameworkChangeObservers.forEach(fcn => fcn());
if (resetTriggered) {
// The final run of detectFramework:
// Starting from this point, the `frameworkCache` becomes "final".
// So we don't need to notify the observers again so the observer
// can be removed after the final notice.
while (frameworkChangeObservers.length) {
frameworkChangeObservers.pop()();
}
} else {
// The first run of detectFramework:
// Every time we update the cache, call each observer function
frameworkChangeObservers.forEach(fcn => fcn());
}

// Retry once for either Unknown type after a delay (explained below)
resetTimeout(Framework.ServerSideUnknown, SSR_RESET_TIMEOUT);
Expand All @@ -33,6 +44,12 @@ export const detectFramework = (): Framework => {
* @internal Setup observer callback that will be called everytime the framework changes
*/
export const observeFrameworkChanges = (fcn: () => void) => {
// When the `frameworkCache` won't be updated again, we ignore all incoming
// observers.
if (resetTriggered) {
return;
}

frameworkChangeObservers.push(fcn);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/geo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"name": "Geo (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Geo }",
"limit": "52.1 kB"
"limit": "52.12 kB"
}
],
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion packages/interactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"name": "Interactions (top-level class with Lex v2)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Interactions, AWSLexV2Provider }",
"limit": "76 kB"
"limit": "76.02 kB"
}
],
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion packages/predictions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"name": "Predictions (Identify provider)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Predictions, AmazonAIIdentifyPredictionsProvider }",
"limit": "73.55 kB"
"limit": "73.57 kB"
},
{
"name": "Predictions (Interpret provider)",
Expand Down
4 changes: 2 additions & 2 deletions packages/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@
"name": "PubSub (IoT provider)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, PubSub, AWSIoTProvider }",
"limit": "81.37 kB"
"limit": "81.39 kB"
},
{
"name": "PubSub (Mqtt provider)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, PubSub, MqttOverWSProvider }",
"limit": "81.24 kB"
"limit": "81.26 kB"
}
],
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"name": "Storage (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Storage }",
"limit": "38.95 kB"
"limit": "38.96 kB"
}
],
"jest": {
Expand Down