Skip to content

Commit

Permalink
chore: update dedup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
israx committed Jan 12, 2024
1 parent 3419b8f commit 4bbeba0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AuthConfig } from '@aws-amplify/core';
import {
assertTokenProviderConfig,
decodeJWT,
deDupCallback,
} from '@aws-amplify/core/internals/utils';
import { initiateAuth } from '../utils/clients/CognitoIdentityProvider';
import { getRegion } from '../utils/clients/CognitoIdentityProvider/utils';
Expand Down Expand Up @@ -40,7 +41,8 @@ export const refreshAuthTokens: TokenRefresher = async ({
userPoolClientId: authConfig.Cognito.userPoolClientId,
});

const { AuthenticationResult } = await initiateAuth(
const deDuplicatedInitiateAuth = deDupCallback(initiateAuth);
const { AuthenticationResult } = await deDuplicatedInitiateAuth(
{ region },
{
ClientId: authConfig?.Cognito?.userPoolClientId,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/libraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
retry,
urlSafeDecode,
urlSafeEncode,
deDupCallback,
} from './utils';
export { parseAWSExports } from './parseAWSExports';
export { LegacyConfig } from './singleton/types';
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/singleton/apis/fetchAuthSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import { fetchAuthSession as fetchAuthSessionInternal } from './internal/fetchAuthSession';
import { Amplify } from '../Amplify';
import { FetchAuthSessionOptions } from '../Auth/types';
import { debounceCallback } from '../../utils/debounceCallback';

export const fetchAuthSession = debounceCallback(
(options?: FetchAuthSessionOptions) => {
return fetchAuthSessionInternal(Amplify, options);
}
);
export const fetchAuthSession = (options?: FetchAuthSessionOptions) => {
return fetchAuthSessionInternal(Amplify, options);
};
34 changes: 34 additions & 0 deletions packages/core/src/utils/deDupCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/**
* returns inflight promise if there hasn't been resolved yet
*
* @param callback - callback to be deDup.
* @returns - the return type of the callback
*/
export const deDupCallback = <F extends (...args: any[]) => any>(
callback: F
) => {
let inflightPromise: Promise<Awaited<ReturnType<F>>> | undefined;
return async (...args: Parameters<F>): Promise<Awaited<ReturnType<F>>> => {
if (inflightPromise) return inflightPromise;

if (!inflightPromise) {
inflightPromise = new Promise(async (resolve, reject) => {
try {
const result = await callback(...args);
resolve(result);
} catch (error) {
reject(error);
}
});
}

try {
return await inflightPromise;
} finally {
inflightPromise = undefined;
}
};
};
62 changes: 0 additions & 62 deletions packages/core/src/utils/debounceCallback.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export {
export { urlSafeDecode } from './urlSafeDecode';
export { urlSafeEncode } from './urlSafeEncode';
export { deepFreeze } from './deepFreeze';
export { deDupCallback } from './deDupCallback';

0 comments on commit 4bbeba0

Please sign in to comment.