From 8235b5135e414c25f5a8b8937928ed3a66d23baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Gomes?= Date: Fri, 29 May 2020 10:30:04 +0100 Subject: [PATCH] Fix circular dependency in `wpcom-xhr-wrapper` `lib/wpcom-xhr-wrapper` was introducing a circular dependency through `lib/user/utils`: ``` client/lib/user/utils.js -> client/lib/user/index.js -> client/lib/user/user.js -> client/lib/user/support-user-interop.js -> client/lib/wp/browser.js -> client/lib/wpcom-xhr-wrapper/index.js -> client/lib/user/utils.js ``` Since `lib/wpcom-xhr-wrapper` is all async anyway, we can load `lib/user/utils` dynamically, and avoid the circular dependency. --- client/lib/wpcom-xhr-wrapper/index.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/client/lib/wpcom-xhr-wrapper/index.js b/client/lib/wpcom-xhr-wrapper/index.js index a262cdd5948746..3977761a27906e 100644 --- a/client/lib/wpcom-xhr-wrapper/index.js +++ b/client/lib/wpcom-xhr-wrapper/index.js @@ -8,15 +8,18 @@ import debugModule from 'debug'; */ const debug = debugModule( 'calypso:wpcom-xhr-wrapper' ); -export default function ( params, callback ) { - return import( /* webpackChunkName: "wpcom-xhr-request" */ 'wpcom-xhr-request' ).then( ( xhr ) => - xhr.default( params, function ( error, response, headers ) { - if ( error && error.name === 'InvalidTokenError' ) { - debug( 'Invalid token error detected, authorisation probably revoked - logging out' ); - require( 'lib/user/utils' ).logout(); - } +export default async function ( params, callback ) { + const xhr = ( await import( /* webpackChunkName: "wpcom-xhr-request" */ 'wpcom-xhr-request' ) ) + .default; + const userUtils = ( await import( /* webpackChunkName: "lib-user-utils" */ 'lib/user/utils' ) ) + .default; - callback( error, response, headers ); - } ) - ); + return xhr( params, function ( error, response, headers ) { + if ( error && error.name === 'InvalidTokenError' ) { + debug( 'Invalid token error detected, authorisation probably revoked - logging out' ); + userUtils.logout(); + } + + callback( error, response, headers ); + } ); }