File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -72,12 +72,19 @@ export const wrapper = input => WrappedComponent => {
7272 }
7373
7474 initialize ( options ) {
75+ // Avoid race condition: remove previous 'load' listener
76+ if ( this . unregisterLoadHandler ) {
77+ this . unregisterLoadHandler ( ) ;
78+ this . unregisterLoadHandler = null ;
79+ }
80+
7581 // Load cache factory
7682 const createCache = options . createCache || defaultCreateCache ;
7783
7884 // Build script
7985 this . scriptCache = createCache ( options ) ;
80- this . scriptCache . google . onLoad ( this . onLoad . bind ( this ) ) ;
86+ this . unregisterLoadHandler =
87+ this . scriptCache . google . onLoad ( this . onLoad . bind ( this ) ) ;
8188
8289 // Store information about loading container
8390 this . LoadingContainer =
Original file line number Diff line number Diff line change @@ -9,15 +9,27 @@ export const ScriptCache = (function(global) {
99
1010 Cache . _onLoad = function ( key ) {
1111 return ( cb ) => {
12+ let registered = true ;
13+
14+ function unregister ( ) {
15+ registered = false ;
16+ }
17+
1218 let stored = scriptMap . get ( key ) ;
19+
1320 if ( stored ) {
1421 stored . promise . then ( ( ) => {
15- stored . error ? cb ( stored . error ) : cb ( null , stored )
22+ if ( registered ) {
23+ stored . error ? cb ( stored . error ) : cb ( null , stored )
24+ }
25+
1626 return stored ;
1727 } ) ;
1828 } else {
1929 // TODO:
2030 }
31+
32+ return unregister ;
2133 }
2234 }
2335
You can’t perform that action at this time.
0 commit comments