@@ -14,6 +14,7 @@ declare module "@userfront/core" {
1414}
1515
1616let singleton = Userfront ;
17+ let isSingletonOverridden = false ;
1718
1819/**
1920 * Override the Userfront singleton imported from @userfront/core with an object of your choice.
@@ -22,6 +23,7 @@ let singleton = Userfront;
2223 */
2324export const overrideUserfrontSingleton = ( newSingleton : any ) => {
2425 singleton = newSingleton as typeof Userfront ;
26+ isSingletonOverridden = true ;
2527} ;
2628
2729// A type with the keys of all functions in Type
@@ -110,7 +112,21 @@ export const callUserfront = async ({ method, args = [] }: CallUserfront) => {
110112 return Promise . reject ( ) ;
111113 }
112114 try {
113- return await ( < any > _method ) ( ...args ) ;
115+ const res = await ( < any > _method ) ( ...args ) ;
116+ // Workaround to avoid flickering when CoreJS redirects:
117+ // wait for the current iteration of the event loop to finish,
118+ // and for the async task queue to finish,
119+ // and for the NEXT event loop cycle to finish,
120+ // then return.
121+ // TODO DEV-658 fix this in a nicer and more reliable way
122+ if ( isSingletonOverridden ) {
123+ // Don't wait if we've overridden the CoreJS singleton,
124+ // i.e. mocked it out for tests.
125+ return res ;
126+ }
127+ return new Promise ( ( resolve ) => {
128+ setTimeout ( ( ) => resolve ( res ) , 1 ) ;
129+ } ) ;
114130 } catch ( err : any ) {
115131 console . warn (
116132 `Method ${ method } on Userfront object threw. Error: ${ err . message } `
0 commit comments