Skip to content

Commit 31e64f9

Browse files
committed
#172: Rebuild distributable package
1 parent 62b6592 commit 31e64f9

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

dist/GoogleApiComponent.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@
7979
}
8080

8181
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+
8290
var defaultCreateCache = function defaultCreateCache(options) {
8391
options = options || {};
8492
var apiKey = options.apiKey;
@@ -106,31 +114,71 @@
106114
);
107115
};
108116

109-
var wrapper = exports.wrapper = function wrapper(options) {
117+
var wrapper = exports.wrapper = function wrapper(input) {
110118
return function (WrappedComponent) {
111-
var createCache = options.createCache || defaultCreateCache;
112-
113119
var Wrapper = function (_React$Component) {
114120
_inherits(Wrapper, _React$Component);
115121

116122
function Wrapper(props, context) {
117123
_classCallCheck(this, Wrapper);
118124

125+
// Build options from input
119126
var _this = _possibleConstructorReturn(this, (Wrapper.__proto__ || Object.getPrototypeOf(Wrapper)).call(this, props, context));
120127

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);
124132

125133
_this.state = {
126134
loaded: false,
127135
map: null,
128-
google: null
136+
google: null,
137+
options: options
129138
};
130139
return _this;
131140
}
132141

133142
_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+
this.setState({ options: options });
164+
}
165+
}, {
166+
key: 'initialize',
167+
value: function initialize(options) {
168+
// Load cache factory
169+
var createCache = options.createCache || defaultCreateCache;
170+
171+
// Build script
172+
this.scriptCache = createCache(options);
173+
this.scriptCache.google.onLoad(this.onLoad.bind(this));
174+
175+
// Store information about loading container
176+
this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer;
177+
178+
// Remove information about previous API handlers
179+
this.setState({ loaded: false, google: null });
180+
}
181+
}, {
134182
key: 'onLoad',
135183
value: function onLoad(err, tag) {
136184
this._gapi = window.google;

0 commit comments

Comments
 (0)