lint, move various methods to async

This commit is contained in:
Tropical 2022-09-03 13:25:15 -05:00
parent b7affd6199
commit 25dff53749
8 changed files with 50 additions and 27 deletions

View File

@ -131,11 +131,13 @@ class EmbeddedStoreCard extends React.Component {
}
async install(id, filename) {
await WebAPI.getAddonContents(id).then(contents => {
return this.manager.installAddon(contents, filename);
}).catch(err => {
Toasts.error(Strings.Store.downloadError.format({type: this.state.addon.type}), err);
});
try {
const contents = await WebAPI.getAddonContents(id);
this.props.installAddon(contents, filename);
}
catch (error) {
Toasts.error(Strings.Store.downloadError.format({type: this.props.type}));
}
}
render() {

View File

@ -25,7 +25,7 @@ export default new class WebAPI {
getAddons(type) {
return new Promise((resolve, reject) => {
if (API_CACHE[type].length) resolve(API_CACHE[type]);
https.get(Web.ENDPOINTS.store(type), (res) => {
const request = https.get(Web.ENDPOINTS.store(type), (res) => {
const chunks = [];
res.on("data", chunk => chunks.push(chunk));
@ -44,6 +44,11 @@ export default new class WebAPI {
reject(error);
});
});
request.on("error", (error) => {
Logger.stacktrace("WebAPI", Strings.Store.connectionError, error);
reject(error);
});
});
}
@ -57,7 +62,7 @@ export default new class WebAPI {
const cacheMatch = API_CACHE.addon.find(a => a[typeof addon === "number" ? "id" : "name"] === addon);
if (cacheMatch) resolve(cacheMatch);
https.get(Web.ENDPOINTS.addon(addon), (res) => {
const request = https.get(Web.ENDPOINTS.addon(addon), (res) => {
const chunks = [];
res.on("data", chunk => chunks.push(chunk));
@ -76,6 +81,11 @@ export default new class WebAPI {
reject(error);
});
});
request.on("error", (error) => {
Logger.stacktrace("WebAPI", Strings.Store.connectionError, error);
reject(error);
});
});
}
@ -86,7 +96,7 @@ export default new class WebAPI {
*/
getAddonContents(id) {
return new Promise((resolve, reject) => {
https.get(Web.ENDPOINTS.download(id), (res) => {
const request = https.get(Web.ENDPOINTS.download(id), (res) => {
const chunks = [];
res.on("data", chunk => chunks.push(chunk));
@ -100,6 +110,11 @@ export default new class WebAPI {
reject(error);
});
});
request.on("error", (error) => {
Logger.stacktrace("WebAPI", Strings.Store.connectionError, error);
reject(error);
});
});
}
};

View File

@ -14,7 +14,7 @@ export default class Modals {
static get ModalStack() {return WebpackModules.getByProps("push", "update", "pop", "popWithKey");}
static get ModalComponents() {return WebpackModules.getByProps("ModalRoot");}
static get ModalClasses() {return WebpackModules.getByProps("modal", "content");}
static get MaskedLink() {return WebpackModules.getModule(m => m?.default?.type?.toString()?.includes('default.MASKED_LINK'))?.default;}
static get MaskedLink() {return WebpackModules.getModule(m => m?.default?.type?.toString()?.includes("default.MASKED_LINK"))?.default;}
static get ImageModal() {return WebpackModules.getByDisplayName("ImageModal");}
static get AlertModal() {return WebpackModules.getByPrototypes("handleCancel", "handleSubmit", "handleMinorConfirm");}
static get FlexElements() {return WebpackModules.getByProps("Child", "Align");}
@ -115,7 +115,7 @@ export default class Modals {
placeholder: src,
original: src,
onClickUntrusted: link => link.openHref(),
renderLinkComponent: props => React.createElement(this.MaskedLink, props)
renderLinkComponent: () => React.createElement(this.MaskedLink, props)
}, props)));
}, {modalKey: key});
}

View File

@ -1,7 +1,6 @@
import Logger from "common/logger";
import {React, Strings, WebpackModules, DiscordModules} from "modules";
import SimpleMarkdown from "../../../structs/markdown";
import ReloadIcon from "../../icons/reload";
import EditIcon from "../../icons/edit";
import DeleteIcon from "../../icons/delete";
import CogIcon from "../../icons/cog";

View File

@ -8,7 +8,6 @@ import SearchBar from "../components/searchbar";
import TabBar from "../../tabbar";
import Divider from "../../divider";
import SettingsTitle from "../title";
import Reload from "../../icons/reload";
import StorePage from "./store";
import InstalledPage from "./installed";

View File

@ -1,5 +1,5 @@
import Logger from "common/logger";
import {React, Settings, Strings, Events} from "modules";
import {React, Strings, Events} from "modules";
import {Web} from "data";
import AddonCard from "./addoncard";
@ -95,6 +95,6 @@ const originalRender = InstalledPage.prototype.render;
Object.defineProperty(InstalledPage.prototype, "render", {
enumerable: false,
configurable: false,
set: function() {Logger.warn("AddonList", "Addon policy for plugins #5 https://github.com/BetterDiscord/BetterDiscord/wiki/Addon-Policies#plugins");},
set: function() {Logger.warn("AddonList", "Addon guideline for plugins #3 https://docs.betterdiscord.app/plugins/introduction/guidelines/#scope");},
get: () => originalRender
});

View File

@ -28,25 +28,33 @@ export default class StorePage extends React.Component {
this.connect();
}
connect() {
WebAPI.getAddons(`${this.props.type}s`).then(data => {
async connect() {
try {
const data = await WebAPI.getAddons(`${this.props.type}s`);
this.setState({
isLoaded: true,
addons: data
});
}).catch(() => Modals.showConfirmationModal(Strings.Store.connectionError, Strings.Store.connectionErrorMessage, {
cancelText: Strings.Modals.close,
confirmText: Strings.Modals.retry,
onConfirm: () => this.connect()
}));
}
catch (error) {
console.log("error");
Modals.showConfirmationModal(Strings.Store.connectionError, Strings.Store.connectionErrorMessage, {
cancelText: Strings.Modals.close,
confirmText: Strings.Modals.retry,
onConfirm: () => this.connect()
});
}
}
async install(id, filename) {
await WebAPI.getAddonContents(id).then(contents => {
return this.props.installAddon(contents, filename);
}).catch(err => {
Toasts.error(Strings.Store.downloadError.format({type: this.props.type}), err);
});
try {
const contents = await WebAPI.getAddonContents(id);
this.props.installAddon(contents, filename);
}
catch (error) {
Toasts.error(Strings.Store.downloadError.format({type: this.props.type}), error);
}
}
matchAddon(addon, query) {

View File

@ -25,7 +25,7 @@ export default class Spinner extends React.Component {
const {className, type = Type.WANDERING_CUBES, ...props} = this.props;
return <div className={Utilities.joinClassNames("bd-spinner", `bd-spinner-${type}`, className)} {...props}>
<span class="bd-spinner-inner">
<span className="bd-spinner-inner">
{this.renderItems(type)}
</span>
</div>;