@@ -5,6 +5,10 @@ import {ScriptCache} from './lib/ScriptCache';
5
5
import GoogleApi from './lib/GoogleApi' ;
6
6
7
7
const defaultMapConfig = { } ;
8
+
9
+ const serialize = obj => JSON . stringify ( obj ) ;
10
+ const isSame = ( obj1 , obj2 ) => obj1 === obj2 || serialize ( obj1 ) === serialize ( obj2 ) ;
11
+
8
12
const defaultCreateCache = options => {
9
13
options = options || { } ;
10
14
const apiKey = options . apiKey ;
@@ -30,19 +34,57 @@ export const wrapper = input => WrappedComponent => {
30
34
class Wrapper extends React . Component {
31
35
constructor ( props , context ) {
32
36
super ( props , context ) ;
37
+
38
+ // Build options from input
33
39
const options = typeof input === 'function' ? input ( props ) : input ;
40
+
41
+ // Initialize required Google scripts and other configured options
42
+ this . initialize ( options ) ;
43
+
44
+ this . state = {
45
+ loaded : false ,
46
+ map : null ,
47
+ google : null ,
48
+ options : options
49
+ } ;
50
+ }
51
+
52
+ componentWillReceiveProps ( props ) {
53
+ // Do not update input if it's not dynamic
54
+ if ( typeof input !== 'function' ) {
55
+ return ;
56
+ }
57
+
58
+ // Get options to compare
59
+ const prevOptions = this . state . options ;
60
+ const options = typeof input === 'function' ? input ( props ) : input ;
61
+
62
+ // Ignore when options are not changed
63
+ if ( isSame ( options , prevOptions ) ) {
64
+ return ;
65
+ }
66
+
67
+ // Initialize with new options
68
+ this . initialize ( options ) ;
69
+
70
+ // Save new options in component state
71
+ this . setState ( { options : options } ) ;
72
+ }
73
+
74
+ initialize ( options ) {
75
+ // Load cache factory
34
76
const createCache = options . createCache || defaultCreateCache ;
35
77
78
+ // Build script
36
79
this . scriptCache = createCache ( options ) ;
37
80
this . scriptCache . google . onLoad ( this . onLoad . bind ( this ) ) ;
81
+
82
+ // Store information about loading container
38
83
this . LoadingContainer =
39
84
options . LoadingContainer || DefaultLoadingContainer ;
40
85
41
- this . state = {
42
- loaded : false ,
43
- map : null ,
44
- google : null
45
- } ;
86
+ // Remove information about previous API handlers
87
+ this . setState ( { loaded : false , google : null } ) ;
46
88
}
47
89
48
90
onLoad ( err , tag ) {
0 commit comments