|
79 | 79 | } |
80 | 80 |
|
81 | 81 | var defaultMapConfig = {}; |
| 82 | + |
| 83 | + var serialize = function serialize(obj) { |
| 84 | + return JSON.stringify(obj); |
| 85 | + }; |
| 86 | + var isSame = function isSame(obj1, obj2) { |
| 87 | + return obj1 === obj2 || serialize(obj1) === serialize(obj2); |
| 88 | + }; |
| 89 | + |
82 | 90 | var defaultCreateCache = function defaultCreateCache(options) { |
83 | 91 | options = options || {}; |
84 | 92 | var apiKey = options.apiKey; |
|
106 | 114 | ); |
107 | 115 | }; |
108 | 116 |
|
109 | | - var wrapper = exports.wrapper = function wrapper(options) { |
| 117 | + var wrapper = exports.wrapper = function wrapper(input) { |
110 | 118 | return function (WrappedComponent) { |
111 | | - var createCache = options.createCache || defaultCreateCache; |
112 | | - |
113 | 119 | var Wrapper = function (_React$Component) { |
114 | 120 | _inherits(Wrapper, _React$Component); |
115 | 121 |
|
116 | 122 | function Wrapper(props, context) { |
117 | 123 | _classCallCheck(this, Wrapper); |
118 | 124 |
|
| 125 | + // Build options from input |
119 | 126 | var _this = _possibleConstructorReturn(this, (Wrapper.__proto__ || Object.getPrototypeOf(Wrapper)).call(this, props, context)); |
120 | 127 |
|
121 | | - _this.scriptCache = createCache(options); |
122 | | - _this.scriptCache.google.onLoad(_this.onLoad.bind(_this)); |
123 | | - _this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer; |
| 128 | + var options = typeof input === 'function' ? input(props) : input; |
| 129 | + |
| 130 | + // Initialize required Google scripts and other configured options |
| 131 | + _this.initialize(options); |
124 | 132 |
|
125 | 133 | _this.state = { |
126 | 134 | loaded: false, |
127 | 135 | map: null, |
128 | | - google: null |
| 136 | + google: null, |
| 137 | + options: options |
129 | 138 | }; |
130 | 139 | return _this; |
131 | 140 | } |
132 | 141 |
|
133 | 142 | _createClass(Wrapper, [{ |
| 143 | + key: 'componentWillReceiveProps', |
| 144 | + value: function componentWillReceiveProps(props) { |
| 145 | + // Do not update input if it's not dynamic |
| 146 | + if (typeof input !== 'function') { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // Get options to compare |
| 151 | + var prevOptions = this.state.options; |
| 152 | + var options = typeof input === 'function' ? input(props) : input; |
| 153 | + |
| 154 | + // Ignore when options are not changed |
| 155 | + if (isSame(options, prevOptions)) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Initialize with new options |
| 160 | + this.initialize(options); |
| 161 | + |
| 162 | + // Save new options in component state, |
| 163 | + // and remove information about previous API handlers |
| 164 | + this.setState({ |
| 165 | + options: options, |
| 166 | + loaded: false, |
| 167 | + google: null |
| 168 | + }); |
| 169 | + } |
| 170 | + }, { |
| 171 | + key: 'initialize', |
| 172 | + value: function initialize(options) { |
| 173 | + // Avoid race condition: remove previous 'load' listener |
| 174 | + if (this.unregisterLoadHandler) { |
| 175 | + this.unregisterLoadHandler(); |
| 176 | + this.unregisterLoadHandler = null; |
| 177 | + } |
| 178 | + |
| 179 | + // Load cache factory |
| 180 | + var createCache = options.createCache || defaultCreateCache; |
| 181 | + |
| 182 | + // Build script |
| 183 | + this.scriptCache = createCache(options); |
| 184 | + this.unregisterLoadHandler = this.scriptCache.google.onLoad(this.onLoad.bind(this)); |
| 185 | + |
| 186 | + // Store information about loading container |
| 187 | + this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer; |
| 188 | + } |
| 189 | + }, { |
134 | 190 | key: 'onLoad', |
135 | 191 | value: function onLoad(err, tag) { |
136 | 192 | this._gapi = window.google; |
|
0 commit comments