File tree Expand file tree Collapse file tree 1 file changed +16
-14
lines changed Expand file tree Collapse file tree 1 file changed +16
-14
lines changed Original file line number Diff line number Diff line change 1
1
import { Loader } from '@googlemaps/js-api-loader' ;
2
2
3
- let loader_ = null ;
4
-
3
+ let loadPromise_ ;
5
4
let resolveCustomPromise_ ;
6
5
7
6
const _customPromise = new Promise ( ( resolve ) => {
@@ -16,6 +15,11 @@ export default (bootstrapURLKeys, heatmapLibrary) => {
16
15
return _customPromise ;
17
16
}
18
17
18
+ // avoid api to be loaded multiple times
19
+ if ( loadPromise_ ) {
20
+ return loadPromise_ ;
21
+ }
22
+
19
23
if ( ! bootstrapURLKeys . libraries ) {
20
24
bootstrapURLKeys . libraries = [ ] ;
21
25
}
@@ -50,23 +54,21 @@ export default (bootstrapURLKeys, heatmapLibrary) => {
50
54
throw new Error ( 'google map cannot be loaded outside browser env' ) ;
51
55
}
52
56
53
- if ( ! loader_ ) {
54
- const { key, ...restKeys } = bootstrapURLKeys ;
57
+ const { key, ...restKeys } = bootstrapURLKeys ;
55
58
56
- loader_ = new Loader ( {
57
- // need to keep key for backwards compatibility
58
- apiKey : key || '' ,
59
- ...restKeys ,
60
- libraries,
61
- } ) ;
62
- }
59
+ const loader_ = new Loader ( {
60
+ // need to keep key for backwards compatibility
61
+ apiKey : key || '' ,
62
+ ...restKeys ,
63
+ libraries,
64
+ } ) ;
63
65
64
- const loadPromise = loader_ . load ( ) . then ( ( ) => {
66
+ loadPromise_ = loader_ . load ( ) . then ( ( ) => {
65
67
resolveCustomPromise_ ( window . google . maps ) ;
66
68
return window . google . maps ;
67
69
} ) ;
68
70
69
- resolveCustomPromise_ ( loadPromise ) ;
71
+ resolveCustomPromise_ ( loadPromise_ ) ;
70
72
71
- return loadPromise ;
73
+ return loadPromise_ ;
72
74
} ;
You can’t perform that action at this time.
0 commit comments