Skip to content

Commit

Permalink
Fix circular dependency in wpcom-xhr-wrapper (#42785)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
sgomes authored May 29, 2020
1 parent 6891b77 commit d46489e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions client/lib/wpcom-xhr-wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
}

0 comments on commit d46489e

Please sign in to comment.