storj/web/satellite/src/utils/loadScript.ts
Egon Elbre ad0b19fb02 web/satellite: fix lint issues
Fix svg.d.ts definition.

Disable no-explicit-any in src/api, because wrangling all the GraphQL
result types properly is not that nice. We can either fix this later
manually, generate GraphQL types or remove the GraphQL endpoints.

Add annotations to src/store/. Currently it still uses any in places and
also defines more types than absolutely necessary. This is an
unfortunate side-effect of the vuex api. There does seem to be an
alternative package that handles them, but to minimize the number of
changes, we'll currently use these types. Due to those decisions it's
also not easily possible to have types instead of any in multiple
places.

StripeCardInput currently uses any, however, if we find the proper
declarations, we can replace them later.

Change-Id: I2ec8bf7fdd8023129d1f8739ce2b6d97de2a58d0
2021-08-25 06:01:19 +00:00

56 lines
1.7 KiB
TypeScript

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
/**
* LoadScript is an utility for loading scripts.
*/
export class LoadScript {
public readonly head : HTMLHeadElement = document.head;
public readonly script: HTMLScriptElement = document.createElement('script');
/**
* Create script element with some predefined attributes, appends it to a DOM and start loading script.
* @param url - script url.
* @param onSuccess - this callback will be fired when load finished.
* @param onError - this callback will be fired when error occurred.
*/
public constructor(url: string, onSuccess: LoadScriptOnSuccessCallback, onError: LoadScriptOnErrorCallback) {
this.head = document.head;
this.script = document.createElement('script');
this.script.type = 'text/javascript';
this.script.charset = 'utf8';
this.script.async = true;
this.script.src = url;
this.script.onload = () => {
this.script.onerror = null;
onSuccess();
};
this.script.onerror = () => {
this.script.onerror = null;
onError(new Error('Failed to load ' + this.script.src));
};
this.head.appendChild(this.script);
}
/**
* Removes script element from DOM.
*/
public remove(): void {
this.head.removeChild(this.script);
}
}
/**
* LoadScriptOnSuccessCallback describes signature of onSuccess callback.
*/
export type LoadScriptOnSuccessCallback = () => void;
/**
* LoadScriptOnErrorCallback describes signature of onError callback.
* @param err - error occurred during script loading.
*/
export type LoadScriptOnErrorCallback = (err: Error) => void;