Skip to content

Commit

Permalink
fix: Add upkeep to platform/framework caches (#11505)
Browse files Browse the repository at this point in the history
  • Loading branch information
stocaaro authored Jun 15, 2023
1 parent 019b5b1 commit 03aa356
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"name": "Analytics (Pinpoint)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Analytics, AWSPinpointProvider }",
"limit": "30.9 kB"
"limit": "30.95 kB"
},
{
"name": "Analytics (Kinesis)",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"name": "API (rest client)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, RestAPI }",
"limit": "30.8 kB"
"limit": "30.9 kB"
}
],
"jest": {
Expand Down
3 changes: 3 additions & 0 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export class AuthClass {

addAuthCategoryToCognitoUserAgent();
addFrameworkToCognitoUserAgent(Platform.framework);
Platform.observeFrameworkChanges(() => {
addFrameworkToCognitoUserAgent(Platform.framework);
});
}

public getModuleName() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"name": "Core (Credentials)",
"path": "./lib-esm/index.js",
"import": "{ Credentials }",
"limit": "13.25 kB"
"limit": "13.3 kB"
},
{
"name": "Core (Signer)",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/AwsClients/CognitoIdentity/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getRetryDecider,
} from '../../clients/middleware/retry';
import { getAmplifyUserAgentString } from '../../Platform';
import { observeFrameworkChanges } from '../../Platform/detectFramework';

/**
* The service name used to sign requests if the API requires authentication.
Expand Down Expand Up @@ -65,6 +66,10 @@ export const defaultConfig = {
userAgentValue: getAmplifyUserAgentString(),
};

observeFrameworkChanges(() => {
defaultConfig.userAgentValue = getAmplifyUserAgentString();
});

/**
* @internal
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/Platform/detectFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@ import { detect } from './detection';
// We want to cache detection since the framework won't change
let frameworkCache: Framework | undefined;

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

// Setup the detection reset tracking / timeout delays
let resetTriggered = false;
const SSR_RESET_TIMEOUT = 10; // ms
const WEB_RESET_TIMEOUT = 10; // ms
const PRIME_FRAMEWORK_DELAY = 1_000; // ms

export const detectFramework = (): Framework => {
if (!frameworkCache) {
frameworkCache = detect();

// Everytime 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);
resetTimeout(Framework.WebUnknown, WEB_RESET_TIMEOUT);
}
return frameworkCache;
};

/**
* @internal Setup observer callback that will be called everytime the framework changes
*/
export const observeFrameworkChanges = (fcn: () => void) => {
frameworkChangeObservers.push(fcn);
};

export function clearCache() {
frameworkCache = undefined;
}
Expand All @@ -38,6 +51,7 @@ function resetTimeout(framework: Framework, delay: number) {
setTimeout(() => {
clearCache();
resetTriggered = true;
setTimeout(detectFramework, PRIME_FRAMEWORK_DELAY);
}, delay);
}
}
6 changes: 5 additions & 1 deletion packages/core/src/Platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { CustomUserAgentDetails, Framework } from './types';
import { version } from './version';
import { detectFramework } from './detectFramework';
import { detectFramework, observeFrameworkChanges } from './detectFramework';
import { UserAgent as AWSUserAgent } from '@aws-sdk/types';

const BASE_USER_AGENT = `aws-amplify`;
Expand All @@ -17,6 +17,10 @@ class PlatformBuilder {
get isReactNative() {
return this.framework === Framework.ReactNative;
}

observeFrameworkChanges(fcn: () => void) {
observeFrameworkChanges(fcn);
}
}

export const Platform = new PlatformBuilder();
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"name": "Geo (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Geo }",
"limit": "51.6 kB"
"limit": "51.7 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": "75.6 kB"
"limit": "76 kB"
}
],
"jest": {
Expand Down
4 changes: 2 additions & 2 deletions packages/notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@
"name": "Notifications (top-level class)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Notifications }",
"limit": "29.75 kB"
"limit": "29.8 kB"
},
{
"name": "Notifications (with Analytics)",
"path": "./lib-esm/index.js",
"import": "{ Amplify, Notifications, Analytics }",
"limit": "29.75 kB"
"limit": "29.8 kB"
}
]
}

0 comments on commit 03aa356

Please sign in to comment.