Skip to content

Commit edbed21

Browse files
committed
#172: Avoid race condition - allow unregistering 'load' event
1 parent 31e64f9 commit edbed21

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/GoogleApiComponent.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff 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 =

src/lib/ScriptCache.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)