google-maps-js-api-loader
Advanced tools
Comparing version 2.2.0 to 2.2.1
import type { LoaderOptions, Library } from "./types"; | ||
export type { LoaderOptions, Library }; | ||
export declare enum LoaderStatus { | ||
NONE = 0, | ||
LOADING = 1, | ||
LOADED = 2, | ||
ERROR = 3 | ||
NONE = 0, | ||
LOADING = 1, | ||
LOADED = 2, | ||
ERROR = 3, | ||
} | ||
declare type OnError = (err: ErrorEvent | Error) => void; | ||
declare class Loader { | ||
private static _resolve?; | ||
private static _reject?; | ||
private static _options?; | ||
private static [1]?; | ||
private static [2]?; | ||
private static [3]?; | ||
private static _runListeners; | ||
private static _cleanup; | ||
static setOptions(options: LoaderOptions): void; | ||
/** | ||
* Starts listening for given {@link status} changes and calls the given {@link callback} when it does | ||
* @returns a function that can be used to remove the listener, which can then be invoked in cleanup logic | ||
*/ | ||
static addListener(status: LoaderStatus.LOADING | LoaderStatus.LOADED, callback: () => void): () => void; | ||
static addListener(status: LoaderStatus.ERROR, callback: OnError): () => void; | ||
/** Current status of {@link Loader} */ | ||
static status: LoaderStatus; | ||
/** | ||
* Promise of loading | ||
* | ||
* **Resolves** if {@link load} is success | ||
* | ||
* **Rejects** | ||
* | ||
* - if {@link google.maps} was loaded outside of this library | ||
* - if no options was {@link setOptions set} | ||
* - if script loading failed | ||
*/ | ||
static readonly completion: Promise<void>; | ||
/** | ||
* On the first call, it starts loading {@link google.maps} with the given {@link options} | ||
* @returns {@link completion} | ||
*/ | ||
static load(): Promise<void>; | ||
private static _resolve?; | ||
private static _reject?; | ||
private static _options?; | ||
private static [LoaderStatus.LOADING]?; | ||
private static [LoaderStatus.LOADED]?; | ||
private static [LoaderStatus.ERROR]?; | ||
private static _runListeners; | ||
private static _cleanup; | ||
static setOptions(options: LoaderOptions): void; | ||
/** | ||
* Starts listening for given {@link status} changes and calls the given {@link callback} when it does | ||
* @returns a function that can be used to remove the listener, which can then be invoked in cleanup logic | ||
*/ | ||
static addListener( | ||
status: LoaderStatus.LOADING | LoaderStatus.LOADED, | ||
callback: () => void | ||
): () => void; | ||
static addListener(status: LoaderStatus.ERROR, callback: OnError): () => void; | ||
/** Current status of {@link Loader} */ | ||
static status: LoaderStatus; | ||
/** | ||
* Promise of loading | ||
* | ||
* **Resolves** if {@link load} is success | ||
* | ||
* **Rejects** | ||
* | ||
* - if {@link google.maps} was loaded outside of this library | ||
* - if no options was {@link setOptions set} | ||
* - if script loading failed | ||
*/ | ||
static readonly completion: Promise<void>; | ||
/** | ||
* On the first call, it starts loading {@link google.maps} with the given {@link options} | ||
* @returns {@link completion} | ||
*/ | ||
static load(): Promise<void>; | ||
} | ||
export default Loader; |
@@ -1,2 +0,2 @@ | ||
const e=function(){};var t;!function(e){e[e.NONE=0]="NONE",e[e.LOADING=1]="LOADING",e[e.LOADED=2]="LOADED",e[e.ERROR=3]="ERROR"}(t||(t={}));class n{static t(e,t,o){n.status=e,t(o);const r=n[e].values();for(let t=n[e].size;t--;)r.next().value(o)}static o(e){n[e].clear(),delete n[e]}static setOptions(t){n.setOptions=e,n.i=t}static addListener(t,o){const r=n[t];return r?(r.add(o),()=>{r.delete(o)}):e}static load(){if(!n.status){var t;let o;const r=n.i,s=n.l;if(r?null!=(t=window.google)&&t.maps&&(o="Google Maps already loaded"):o="no options was provided",o)s(new Error(o));else{n.t(1,e),n.o(1);let{retryCount:t=2}=r;const{retryDelay:o=2e3}=r,i=new URL(r.url||"https://maps.googleapis.com/maps/api/js"),a={key:r.apiKey,v:r.version,libraries:r.libraries,channel:r.channel,client:r.client,language:r.language,region:r.region,map_ids:r.mapIds,auth_referrer_policy:r.authReferrerPolicy,callback:"__gmlc"};for(const e in a){const t=a[e];t&&i.searchParams.set(e,"string"==typeof t?t:t.join(","))}const c=()=>{const e=e=>{t--?setTimeout(()=>{c(),a.removeChild(n)},o):s(e)},n=document.createElement("script"),{head:a}=document;n.type="text/javascript","defer"in r&&(n.defer=r.defer),"async"in r&&(n.async=r.async),"nonce"in r&&(n.nonce=r.nonce),n.src=i.toString(),n.addEventListener("load",()=>{n.removeEventListener("error",e)},{once:!0}),n.addEventListener("error",e),a.appendChild(n)};window.__gmlc=n.p,c()}delete n.i,delete n.l,delete n.p}return n.completion}}n.p=void 0,n.l=void 0,n.i=void 0,n[1]=new Set,n[2]=new Set,n[3]=new Set,n.status=0,n.completion=new Promise((e,t)=>{function o(e,t){return o=>{n.t(e,t,o),n.o(2),n.o(3),delete n.t,delete n.o,delete window.__gmlc}}n.p=o(2,e),n.l=o(3,t)});export{t as LoaderStatus,n as default}; | ||
const e=function(){},t={NONE:0,LOADING:1,LOADED:2,ERROR:3};class n{static t(e,t,o){n.status=e,t(o);const s=n[e].values();for(let t=n[e].size;t--;)s.next().value(o)}static o(e){n[e].clear(),delete n[e]}static setOptions(t){n.i=t,n.setOptions=e}static addListener(t,o){const s=n[t];return s?(s.add(o),()=>{s.delete(o)}):e}static load(){if(!n.status){var t;let o;const s=n.i,i=n.l;if(s?null!=(t=window.google)&&t.maps&&(o="google.maps already loaded"):o="no options was set",o)i(new Error(o));else{n.t(1,e),n.o(1);const t=new URL(s.url||"https://maps.googleapis.com/maps/api/js"),o={key:s.apiKey,v:s.version,libraries:s.libraries,channel:s.channel,client:s.client,language:s.language,region:s.region,map_ids:s.mapIds,auth_referrer_policy:s.authReferrerPolicy,callback:"__gmlc"};let{retryCount:a=2}=s;const{retryDelay:l=2e3}=s;for(const e in o){const n=o[e];n&&t.searchParams.set(e,"string"==typeof n?n:n.join(","))}const r=()=>{const e=document.createElement("script"),{head:n}=document;e.type="text/javascript",e.onload=()=>{e.onerror=null,e.onload=null},e.onerror=t=>{a--?setTimeout(()=>{r(),n.removeChild(e)},l):i(t)},void 0!==s.defer&&(e.defer=s.defer),void 0!==s.async&&(e.async=s.async),void 0!==s.nonce&&(e.nonce=s.nonce),e.src=t.toString(),n.appendChild(e)};window.__gmlc=n.u,r()}delete n.i,delete n.l,delete n.u}return n.completion}}n.u=void 0,n.l=void 0,n.i=void 0,n[1]=new Set,n[2]=new Set,n[3]=new Set,n.status=0,n.completion=new Promise((e,t)=>{function o(e,t){return o=>{n.t(e,t,o),n.o(2),n.o(3),delete window.__gmlc}}n.u=o(2,e),n.l=o(3,t)});export{t as LoaderStatus,n as default}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var e,n=function(){};!function(e){e[e.NONE=0]="NONE",e[e.LOADING=1]="LOADING",e[e.LOADED=2]="LOADED",e[e.ERROR=3]="ERROR"}(e||(e={}));var o=/*#__PURE__*/function(){function e(){}return e.o=function(n,o,t){e.status=n,o(t);for(var i=e[n].values(),r=e[n].size;r--;)i.next().value(t)},e.t=function(n){e[n].clear(),delete e[n]},e.setOptions=function(o){e.setOptions=n,e.i=o},e.addListener=function(o,t){var i=e[o];return i?(i.add(t),function(){i.delete(t)}):n},e.load=function(){if(!e.status){var o,t,i=e.i,r=e.u;if(i?null!=(o=window.google)&&o.maps&&(t="Google Maps already loaded"):t="no options was provided",t)r(new Error(t));else{e.o(1,n),e.t(1);var a=i.retryCount,c=void 0===a?2:a,u=i.retryDelay,d=void 0===u?2e3:u,f=new URL(i.url||"https://maps.googleapis.com/maps/api/js"),l={key:i.apiKey,v:i.version,libraries:i.libraries,channel:i.channel,client:i.client,language:i.language,region:i.region,map_ids:i.mapIds,auth_referrer_policy:i.authReferrerPolicy,callback:"__gmlc"};for(var s in l){var v=l[s];v&&f.searchParams.set(s,"string"==typeof v?v:v.join(","))}window.__gmlc=e.l,function e(){var n=function(n){c--?setTimeout(function(){e(),t.removeChild(o)},d):r(n)},o=document.createElement("script"),t=document.head;o.type="text/javascript","defer"in i&&(o.defer=i.defer),"async"in i&&(o.async=i.async),"nonce"in i&&(o.nonce=i.nonce),o.src=f.toString(),o.addEventListener("load",function(){o.removeEventListener("error",n)},{once:!0}),o.addEventListener("error",n),t.appendChild(o)}()}delete e.i,delete e.u,delete e.l}return e.completion},e}();o.l=void 0,o.u=void 0,o.i=void 0,o[1]=new Set,o[2]=new Set,o[3]=new Set,o.status=0,o.completion=new Promise(function(e,n){function t(e,n){return function(t){o.o(e,n,t),o.t(2),o.t(3),delete o.o,delete o.t,delete window.__gmlc}}o.l=t(2,e),o.u=t(3,n)});export{e as LoaderStatus,o as default}; | ||
var n=function(){},e={NONE:0,LOADING:1,LOADED:2,ERROR:3},o=/*#__PURE__*/function(){function e(){}return e.o=function(n,o,t){e.status=n,o(t);for(var i=e[n].values(),r=e[n].size;r--;)i.next().value(t)},e.t=function(n){e[n].clear(),delete e[n]},e.setOptions=function(o){e.i=o,e.setOptions=n},e.addListener=function(o,t){var i=e[o];return i?(i.add(t),function(){i.delete(t)}):n},e.load=function(){if(!e.status){var o,t,i=e.i,r=e.u;if(i?null!=(o=window.google)&&o.maps&&(t="google.maps already loaded"):t="no options was set",t)r(new Error(t));else{e.o(1,n),e.t(1);var a=new URL(i.url||"https://maps.googleapis.com/maps/api/js"),u={key:i.apiKey,v:i.version,libraries:i.libraries,channel:i.channel,client:i.client,language:i.language,region:i.region,map_ids:i.mapIds,auth_referrer_policy:i.authReferrerPolicy,callback:"__gmlc"},c=i.retryCount,l=void 0===c?2:c,d=i.retryDelay,f=void 0===d?2e3:d;for(var s in u){var v=u[s];v&&a.searchParams.set(s,"string"==typeof v?v:v.join(","))}window.__gmlc=e.l,function n(){var e=document.createElement("script"),o=document.head;e.type="text/javascript",e.onload=function(){e.onerror=null,e.onload=null},e.onerror=function(t){l--?setTimeout(function(){n(),o.removeChild(e)},f):r(t)},void 0!==i.defer&&(e.defer=i.defer),void 0!==i.async&&(e.async=i.async),void 0!==i.nonce&&(e.nonce=i.nonce),e.src=a.toString(),o.appendChild(e)}()}delete e.i,delete e.u,delete e.l}return e.completion},e}();o.l=void 0,o.u=void 0,o.i=void 0,o[1]=new Set,o[2]=new Set,o[3]=new Set,o.status=0,o.completion=new Promise(function(n,e){function t(n,e){return function(t){o.o(n,e,t),o.t(2),o.t(3),delete window.__gmlc}}o.l=t(2,n),o.u=t(3,e)});export{e as LoaderStatus,o as default}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).googleMapsJsApiLoader={})}(this,function(e){var n,o=function(){};e.LoaderStatus=void 0,(n=e.LoaderStatus||(e.LoaderStatus={}))[n.NONE=0]="NONE",n[n.LOADING=1]="LOADING",n[n.LOADED=2]="LOADED",n[n.ERROR=3]="ERROR";var i=/*#__PURE__*/function(){function e(){}return e.o=function(n,o,i){e.status=n,o(i);for(var t=e[n].values(),r=e[n].size;r--;)t.next().value(i)},e.i=function(n){e[n].clear(),delete e[n]},e.setOptions=function(n){e.setOptions=o,e.t=n},e.addListener=function(n,i){var t=e[n];return t?(t.add(i),function(){t.delete(i)}):o},e.load=function(){if(!e.status){var n,i,t=e.t,r=e.u;if(t?null!=(n=window.google)&&n.maps&&(i="Google Maps already loaded"):i="no options was provided",i)r(new Error(i));else{e.o(1,o),e.i(1);var f=t.retryCount,c=void 0===f?2:f,d=t.retryDelay,a=void 0===d?2e3:d,u=new URL(t.url||"https://maps.googleapis.com/maps/api/js"),l={key:t.apiKey,v:t.version,libraries:t.libraries,channel:t.channel,client:t.client,language:t.language,region:t.region,map_ids:t.mapIds,auth_referrer_policy:t.authReferrerPolicy,callback:"__gmlc"};for(var s in l){var p=l[s];p&&u.searchParams.set(s,"string"==typeof p?p:p.join(","))}window.__gmlc=e.l,function e(){var n=function(n){c--?setTimeout(function(){e(),i.removeChild(o)},a):r(n)},o=document.createElement("script"),i=document.head;o.type="text/javascript","defer"in t&&(o.defer=t.defer),"async"in t&&(o.async=t.async),"nonce"in t&&(o.nonce=t.nonce),o.src=u.toString(),o.addEventListener("load",function(){o.removeEventListener("error",n)},{once:!0}),o.addEventListener("error",n),i.appendChild(o)}()}delete e.t,delete e.u,delete e.l}return e.completion},e}();i.l=void 0,i.u=void 0,i.t=void 0,i[1]=new Set,i[2]=new Set,i[3]=new Set,i.status=0,i.completion=new Promise(function(e,n){function o(e,n){return function(o){i.o(e,n,o),i.i(2),i.i(3),delete i.o,delete i.i,delete window.__gmlc}}i.l=o(2,e),i.u=o(3,n)}),e.default=i}); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).googleMapsJsApiLoader={})}(this,function(e){var n=function(){},o=/*#__PURE__*/function(){function e(){}return e.o=function(n,o,t){e.status=n,o(t);for(var i=e[n].values(),r=e[n].size;r--;)i.next().value(t)},e.t=function(n){e[n].clear(),delete e[n]},e.setOptions=function(o){e.i=o,e.setOptions=n},e.addListener=function(o,t){var i=e[o];return i?(i.add(t),function(){i.delete(t)}):n},e.load=function(){if(!e.status){var o,t,i=e.i,r=e.u;if(i?null!=(o=window.google)&&o.maps&&(t="google.maps already loaded"):t="no options was set",t)r(new Error(t));else{e.o(1,n),e.t(1);var f=new URL(i.url||"https://maps.googleapis.com/maps/api/js"),u={key:i.apiKey,v:i.version,libraries:i.libraries,channel:i.channel,client:i.client,language:i.language,region:i.region,map_ids:i.mapIds,auth_referrer_policy:i.authReferrerPolicy,callback:"__gmlc"},c=i.retryCount,d=void 0===c?2:c,l=i.retryDelay,a=void 0===l?2e3:l;for(var s in u){var p=u[s];p&&f.searchParams.set(s,"string"==typeof p?p:p.join(","))}window.__gmlc=e.l,function e(){var n=document.createElement("script"),o=document.head;n.type="text/javascript",n.onload=function(){n.onerror=null,n.onload=null},n.onerror=function(t){d--?setTimeout(function(){e(),o.removeChild(n)},a):r(t)},void 0!==i.defer&&(n.defer=i.defer),void 0!==i.async&&(n.async=i.async),void 0!==i.nonce&&(n.nonce=i.nonce),n.src=f.toString(),o.appendChild(n)}()}delete e.i,delete e.u,delete e.l}return e.completion},e}();o.l=void 0,o.u=void 0,o.i=void 0,o[1]=new Set,o[2]=new Set,o[3]=new Set,o.status=0,o.completion=new Promise(function(e,n){function t(e,n){return function(t){o.o(e,n,t),o.t(2),o.t(3),delete window.__gmlc}}o.l=t(2,e),o.u=t(3,n)}),e.LoaderStatus={NONE:0,LOADING:1,LOADED:2,ERROR:3},e.default=o}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "google-maps-js-api-loader", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Google Maps JavaScript API loader", | ||
@@ -5,0 +5,0 @@ "repository": { |
111
src/index.ts
@@ -6,3 +6,3 @@ import type { LoaderOptions, Library } from "./types"; | ||
export enum LoaderStatus { | ||
const enum _LoaderStatus { | ||
NONE, | ||
@@ -14,8 +14,12 @@ LOADING, | ||
export const LoaderStatus: typeof _LoaderStatus = { | ||
NONE: 0, | ||
LOADING: 1, | ||
LOADED: 2, | ||
ERROR: 3, | ||
}; | ||
type OnError = (err: ErrorEvent | Error) => void; | ||
type RunningStatus = | ||
| LoaderStatus.LOADING | ||
| LoaderStatus.LOADED | ||
| LoaderStatus.ERROR; | ||
type RunningStatus = Exclude<_LoaderStatus, _LoaderStatus.NONE>; | ||
@@ -27,7 +31,7 @@ class Loader { | ||
private static [1]? = new Set<() => void>(); | ||
private static [2]? = new Set<() => void>(); | ||
private static [3]? = new Set<OnError>(); | ||
private static [_LoaderStatus.LOADING]? = new Set<() => void>(); | ||
private static [_LoaderStatus.LOADED]? = new Set<() => void>(); | ||
private static [_LoaderStatus.ERROR]? = new Set<OnError>(); | ||
private static _runListeners?( | ||
private static _runListeners( | ||
status: RunningStatus, | ||
@@ -48,3 +52,3 @@ fn: (...args: any[]) => void, | ||
private static _cleanup?(status: RunningStatus) { | ||
private static _cleanup(status: RunningStatus) { | ||
Loader[status]!.clear(); | ||
@@ -56,5 +60,5 @@ | ||
static setOptions(options: LoaderOptions) { | ||
Loader._options = options; | ||
Loader.setOptions = noop; | ||
Loader._options = options; | ||
} | ||
@@ -67,9 +71,12 @@ | ||
static addListener( | ||
status: LoaderStatus.LOADING | LoaderStatus.LOADED, | ||
status: _LoaderStatus.LOADING | _LoaderStatus.LOADED, | ||
callback: () => void | ||
): () => void; | ||
static addListener(status: LoaderStatus.ERROR, callback: OnError): () => void; | ||
static addListener( | ||
status: _LoaderStatus.ERROR, | ||
callback: OnError | ||
): () => void; | ||
static addListener( | ||
status: Exclude<LoaderStatus, LoaderStatus.NONE>, | ||
status: RunningStatus, | ||
callback: (...args: any[]) => void | ||
@@ -91,3 +98,3 @@ ) { | ||
/** Current status of {@link Loader} */ | ||
static status: LoaderStatus = 0; // LoaderStatus.NONE | ||
static status = _LoaderStatus.NONE; | ||
@@ -111,12 +118,8 @@ /** | ||
return (arg?: any) => { | ||
Loader._runListeners!(status, fn, arg); | ||
Loader._runListeners(status, fn, arg); | ||
Loader._cleanup!(2); | ||
Loader._cleanup(_LoaderStatus.LOADED); | ||
Loader._cleanup!(3); | ||
Loader._cleanup(_LoaderStatus.ERROR); | ||
delete Loader._runListeners; | ||
delete Loader._cleanup; | ||
delete window[CALLBACK_NAME]; | ||
@@ -126,4 +129,4 @@ }; | ||
Loader._resolve = handleSetStatus(2, resolve); | ||
Loader._reject = handleSetStatus(3, reject); | ||
Loader._resolve = handleSetStatus(_LoaderStatus.LOADED, resolve); | ||
Loader._reject = handleSetStatus(_LoaderStatus.ERROR, reject); | ||
}); | ||
@@ -144,5 +147,5 @@ | ||
if (!options) { | ||
errorMessage = "no options was provided"; | ||
errorMessage = "no options was set"; | ||
} else if (window.google?.maps) { | ||
errorMessage = "Google Maps already loaded"; | ||
errorMessage = "google.maps already loaded"; | ||
} | ||
@@ -153,10 +156,6 @@ | ||
} else { | ||
Loader._runListeners!(1, noop); | ||
Loader._runListeners(_LoaderStatus.LOADING, noop); | ||
Loader._cleanup!(1); | ||
Loader._cleanup(_LoaderStatus.LOADING); | ||
let { retryCount = 2 } = options; | ||
const { retryDelay = 2000 } = options; | ||
const url = new URL( | ||
@@ -179,2 +178,6 @@ options.url || "https://maps.googleapis.com/maps/api/js" | ||
let { retryCount = 2 } = options; | ||
const { retryDelay = 2000 } = options; | ||
for (const key in params) { | ||
@@ -192,3 +195,15 @@ const param = params[key]; | ||
const createScript = () => { | ||
const onError = (err: ErrorEvent) => { | ||
const script = document.createElement("script"); | ||
const { head } = document; | ||
script.type = "text/javascript"; | ||
script.onload = () => { | ||
script.onerror = null; | ||
script.onload = null; | ||
}; | ||
script.onerror = (err: ErrorEvent) => { | ||
if (retryCount--) { | ||
@@ -205,18 +220,12 @@ setTimeout(() => { | ||
const script = document.createElement("script"); | ||
const { head } = document; | ||
script.type = "text/javascript"; | ||
if ("defer" in options) { | ||
script.defer = options.defer!; | ||
if (options.defer !== undefined) { | ||
script.defer = options.defer; | ||
} | ||
if ("async" in options) { | ||
script.async = options.async!; | ||
if (options.async !== undefined) { | ||
script.async = options.async; | ||
} | ||
if ("nonce" in options) { | ||
script.nonce = options.nonce!; | ||
if (options.nonce !== undefined) { | ||
script.nonce = options.nonce; | ||
} | ||
@@ -226,12 +235,2 @@ | ||
script.addEventListener( | ||
"load", | ||
() => { | ||
script.removeEventListener("error", onError); | ||
}, | ||
{ once: true } | ||
); | ||
script.addEventListener("error", onError); | ||
head.appendChild(script); | ||
@@ -238,0 +237,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
416
0
71554