-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.ts
508 lines (458 loc) · 12.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at.
*
* Http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import isEqual from "fast-deep-equal";
/**
* @ignore
*/
declare global {
interface Window {
__googleMapsCallback: (e: Event) => void;
}
}
export const DEFAULT_ID = "__googleMapsScriptId";
type Libraries = (
| "drawing"
| "geometry"
| "localContext"
| "places"
| "visualization"
)[];
/**
* The Google Maps JavaScript API
* [documentation](https://developers.google.com/maps/documentation/javascript/tutorial)
* is the authoritative source for [[LoaderOptions]].
/**
* Loader options
*/
export interface LoaderOptions {
/**
* See https://developers.google.com/maps/documentation/javascript/get-api-key.
*/
apiKey: string;
/**
* @deprecated See https://developers.google.com/maps/premium/overview.
*/
channel?: string;
/**
* @deprecated See https://developers.google.com/maps/premium/overview, use `apiKey` instead.
*/
client?: string;
/**
* In your application you can specify release channels or version numbers:
*
* The weekly version is specified with `version=weekly`. This version is
* updated once per week, and is the most current.
*
* ```
* const loader = Loader({apiKey, version: 'weekly'});
* ```
*
* The quarterly version is specified with `version=quarterly`. This version
* is updated once per quarter, and is the most predictable.
*
* ```
* const loader = Loader({apiKey, version: 'quarterly'});
* ```
*
* The version number is specified with `version=n.nn`. You can choose
* `version=3.40`, `version=3.39`, or `version=3.38`. Version numbers are
* updated once per quarter.
*
* ```
* const loader = Loader({apiKey, version: '3.40'});
* ```
*
* If you do not explicitly specify a version, you will receive the
* weekly version by default.
*/
version?: string;
/**
* The id of the script tag. Before adding a new script, the Loader will check for an existing one.
*/
id?: string;
/**
* When loading the Maps JavaScript API via the URL you may optionally load
* additional libraries through use of the libraries URL parameter. Libraries
* are modules of code that provide additional functionality to the main Maps
* JavaScript API but are not loaded unless you specifically request them.
*
* ```
* const loader = Loader({
* apiKey,
* libraries: ['drawing', 'geometry', 'places', 'visualization'],
* });
* ```
*
* Set the [list of libraries](https://developers.google.com/maps/documentation/javascript/libraries) for more options.
*/
libraries?: Libraries;
/**
* By default, the Maps JavaScript API uses the user's preferred language
* setting as specified in the browser, when displaying textual information
* such as the names for controls, copyright notices, driving directions and
* labels on maps. In most cases, it's preferable to respect the browser
* setting. However, if you want the Maps JavaScript API to ignore the
* browser's language setting, you can force it to display information in a
* particular language when loading the Maps JavaScript API code.
*
* For example, the following example localizes the language to Japan:
*
* ```
* const loader = Loader({apiKey, language: 'ja', region: 'JP'});
* ```
*
* See the [list of supported
* languages](https://developers.google.com/maps/faq#languagesupport). Note
* that new languages are added often, so this list may not be exhaustive.
*
*/
language?: string;
/**
* When you load the Maps JavaScript API from maps.googleapis.com it applies a
* default bias for application behavior towards the United States. If you
* want to alter your application to serve different map tiles or bias the
* application (such as biasing geocoding results towards the region), you can
* override this default behavior by adding a region parameter when loading
* the Maps JavaScript API code.
*
* The region parameter accepts Unicode region subtag identifiers which
* (generally) have a one-to-one mapping to country code Top-Level Domains
* (ccTLDs). Most Unicode region identifiers are identical to ISO 3166-1
* codes, with some notable exceptions. For example, Great Britain's ccTLD is
* "uk" (corresponding to the domain .co.uk) while its region identifier is
* "GB."
*
* For example, the following example localizes the map to the United Kingdom:
*
* ```
* const loader = Loader({apiKey, region: 'GB'});
* ```
*/
region?: string;
/**
* @deprecated Passing `mapIds` is no longer required in the script tag.
*/
mapIds?: string[];
/**
* Use a custom url and path to load the Google Maps API script.
*/
url?: string;
/**
* Use a cryptographic nonce attribute.
*/
nonce?: string;
/**
* The number of script load retries.
*/
retries?: number;
}
/**
* [[Loader]] makes it easier to add Google Maps JavaScript API to your application
* dynamically using
* [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
* It works by dynamically creating and appending a script node to the the
* document head and wrapping the callback function so as to return a promise.
*
* ```
* const loader = new Loader({
* apiKey: "",
* version: "weekly",
* libraries: ["places"]
* });
*
* loader.load().then((google) => {
* const map = new google.maps.Map(...)
* })
* ```
*/
export class Loader {
/**
* See [[LoaderOptions.version]]
*/
version: string;
/**
* See [[LoaderOptions.apiKey]]
*/
apiKey: string;
/**
* See [[LoaderOptions.channel]]
*/
channel: string;
/**
* See [[LoaderOptions.client]]
*/
client: string;
/**
* See [[LoaderOptions.id]]
*/
id: string;
/**
* See [[LoaderOptions.libraries]]
*/
libraries: Libraries;
/**
* See [[LoaderOptions.language]]
*/
language: string;
/**
* See [[LoaderOptions.region]]
*/
region: string;
/**
* See [[LoaderOptions.mapIds]]
*/
mapIds: string[];
/**
* See [[LoaderOptions.nonce]]
*/
nonce: string | null;
/**
* See [[LoaderOptions.retries]]
*/
retries: number;
/**
* See [[LoaderOptions.url]]
*/
url: string;
private CALLBACK = "__googleMapsCallback";
private callbacks: ((e: ErrorEvent) => void)[] = [];
private done = false;
private loading = false;
private onerrorEvent: ErrorEvent;
private static instance: Loader;
private errors: ErrorEvent[] = [];
/**
* Creates an instance of Loader using [[LoaderOptions]]. No defaults are set
* using this library, instead the defaults are set by the Google Maps
* JavaScript API server.
*
* ```
* const loader = Loader({apiKey, version: 'weekly', libraries: ['places']});
* ```
*/
constructor({
apiKey,
channel,
client,
id = DEFAULT_ID,
libraries = [],
language,
region,
version,
mapIds,
nonce,
retries = 3,
url = "https://maps.googleapis.com/maps/api/js",
}: LoaderOptions) {
this.version = version;
this.apiKey = apiKey;
this.channel = channel;
this.client = client;
this.id = id || DEFAULT_ID; // Do not allow empty string
this.libraries = libraries;
this.language = language;
this.region = region;
this.mapIds = mapIds;
this.nonce = nonce;
this.retries = retries;
this.url = url;
if (Loader.instance) {
if (!isEqual(this.options, Loader.instance.options)) {
throw new Error(
`Loader must not be called again with different options. ${JSON.stringify(
this.options
)} !== ${JSON.stringify(Loader.instance.options)}`
);
}
return Loader.instance;
}
Loader.instance = this;
}
get options(): LoaderOptions {
return {
version: this.version,
apiKey: this.apiKey,
channel: this.channel,
client: this.client,
id: this.id,
libraries: this.libraries,
language: this.language,
region: this.region,
mapIds: this.mapIds,
nonce: this.nonce,
url: this.url,
};
}
private get failed(): boolean {
return this.done && !this.loading && this.errors.length >= this.retries + 1;
}
/**
* CreateUrl returns the Google Maps JavaScript API script url given the [[LoaderOptions]].
*
* @ignore
*/
createUrl(): string {
let url = this.url;
url += `?callback=${this.CALLBACK}`;
if (this.apiKey) {
url += `&key=${this.apiKey}`;
}
if (this.channel) {
url += `&channel=${this.channel}`;
}
if (this.client) {
url += `&client=${this.client}`;
}
if (this.libraries.length > 0) {
url += `&libraries=${this.libraries.join(",")}`;
}
if (this.language) {
url += `&language=${this.language}`;
}
if (this.region) {
url += `®ion=${this.region}`;
}
if (this.version) {
url += `&v=${this.version}`;
}
if (this.mapIds) {
url += `&map_ids=${this.mapIds.join(",")}`;
}
return url;
}
/**
* Load the Google Maps JavaScript API script and return a Promise.
*/
load(): Promise<typeof google> {
return this.loadPromise();
}
/**
* Load the Google Maps JavaScript API script and return a Promise.
*
* @ignore
*/
loadPromise(): Promise<typeof google> {
return new Promise((resolve, reject) => {
this.loadCallback((err: ErrorEvent) => {
if (!err) {
resolve(window.google);
} else {
reject(err.error);
}
});
});
}
/**
* Load the Google Maps JavaScript API script with a callback.
*/
loadCallback(fn: (e: ErrorEvent) => void): void {
this.callbacks.push(fn);
this.execute();
}
/**
* Set the script on document.
*/
private setScript(): void {
if (document.getElementById(this.id)) {
// TODO wrap onerror callback for cases where the script was loaded elsewhere
this.callback();
return;
}
const url = this.createUrl();
const script = document.createElement("script");
script.id = this.id;
script.type = "text/javascript";
script.src = url;
script.onerror = this.loadErrorCallback.bind(this);
script.defer = true;
script.async = true;
if (this.nonce) {
script.nonce = this.nonce;
}
document.head.appendChild(script);
}
deleteScript(): void {
const script = document.getElementById(this.id);
if (script) {
script.remove();
}
}
/**
* Reset the loader state.
*/
private reset(): void {
this.deleteScript();
this.done = false;
this.loading = false;
this.errors = [];
this.onerrorEvent = null;
}
private resetIfRetryingFailed(): void {
if (this.failed) {
this.reset();
}
}
private loadErrorCallback(e: ErrorEvent): void {
this.errors.push(e);
if (this.errors.length <= this.retries) {
const delay = this.errors.length * 2 ** this.errors.length;
console.log(
`Failed to load Google Maps script, retrying in ${delay} ms.`
);
setTimeout(() => {
this.deleteScript();
this.setScript();
}, delay);
} else {
this.onerrorEvent = e;
this.callback();
}
}
private setCallback(): void {
window.__googleMapsCallback = this.callback.bind(this);
}
private callback(): void {
this.done = true;
this.loading = false;
this.callbacks.forEach((cb) => {
cb(this.onerrorEvent);
});
this.callbacks = [];
}
private execute(): void {
this.resetIfRetryingFailed();
if (this.done) {
this.callback();
} else {
// short circuit and warn if google.maps is already loaded
if (window.google && window.google.maps && window.google.maps.version) {
console.warn(
"Google Maps already loaded outside @googlemaps/js-api-loader." +
"This may result in undesirable behavior as options and script parameters may not match."
);
this.callback();
return;
}
if (this.loading) {
// do nothing but wait
} else {
this.loading = true;
this.setCallback();
this.setScript();
}
}
}
}