1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Use async/await for extension functions (#15722)

This commit is contained in:
David Walsh 2022-11-21 08:51:11 -06:00 committed by GitHub
parent 3d14e0b3f0
commit b675a12dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 126 deletions

View File

@ -1,7 +1,7 @@
import browser from 'webextension-polyfill';
import { getBlockExplorerLink } from '@metamask/etherscan-link';
import { getEnvironmentType, checkForError } from '../lib/util';
import { getEnvironmentType } from '../lib/util';
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
@ -13,70 +13,32 @@ export default class ExtensionPlatform {
browser.runtime.reload();
}
openTab(options) {
return new Promise((resolve, reject) => {
browser.tabs.create(options).then((newTab) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(newTab);
});
});
async openTab(options) {
const newTab = await browser.tabs.create(options);
return newTab;
}
openWindow(options) {
return new Promise((resolve, reject) => {
browser.windows.create(options).then((newWindow) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(newWindow);
});
});
async openWindow(options) {
const newWindow = await browser.windows.create(options);
return newWindow;
}
focusWindow(windowId) {
return new Promise((resolve, reject) => {
browser.windows.update(windowId, { focused: true }).then(() => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve();
});
});
async focusWindow(windowId) {
await browser.windows.update(windowId, { focused: true });
}
updateWindowPosition(windowId, left, top) {
return new Promise((resolve, reject) => {
browser.windows.update(windowId, { left, top }).then(() => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve();
});
});
async updateWindowPosition(windowId, left, top) {
await browser.windows.update(windowId, { left, top });
}
getLastFocusedWindow() {
return new Promise((resolve, reject) => {
browser.windows.getLastFocused().then((windowObject) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(windowObject);
});
});
async getLastFocusedWindow() {
const windowObject = await browser.windows.getLastFocused();
return windowObject;
}
closeCurrentWindow() {
return browser.windows.getCurrent().then((windowDetails) => {
return browser.windows.remove(windowDetails.id);
});
async closeCurrentWindow() {
const windowDetails = await browser.windows.getCurrent();
browser.windows.remove(windowDetails.id);
}
getVersion() {
@ -169,67 +131,28 @@ export default class ExtensionPlatform {
browser.windows.onRemoved.addListener(listener);
}
getAllWindows() {
return new Promise((resolve, reject) => {
browser.windows.getAll().then((windows) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(windows);
});
});
async getAllWindows() {
const windows = await browser.windows.getAll();
return windows;
}
getActiveTabs() {
return new Promise((resolve, reject) => {
browser.tabs.query({ active: true }).then((tabs) => {
const error = checkForError();
if (error) {
return reject(error);
}
return resolve(tabs);
});
});
async getActiveTabs() {
const tabs = await browser.tabs.query({ active: true });
return tabs;
}
currentTab() {
return new Promise((resolve, reject) => {
browser.tabs.getCurrent().then((tab) => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve(tab);
}
});
});
async currentTab() {
const tab = await browser.tabs.getCurrent();
return tab;
}
switchToTab(tabId) {
return new Promise((resolve, reject) => {
browser.tabs.update(tabId, { highlighted: true }).then((tab) => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve(tab);
}
});
});
async switchToTab(tabId) {
const tab = await browser.tabs.update(tabId, { highlighted: true });
return tab;
}
closeTab(tabId) {
return new Promise((resolve, reject) => {
browser.tabs.remove(tabId).then(() => {
const err = checkForError();
if (err) {
reject(err);
} else {
resolve();
}
});
});
async closeTab(tabId) {
await browser.tabs.remove(tabId);
}
_showConfirmedTransaction(txMeta, rpcPrefs) {

View File

@ -282,27 +282,22 @@ async function start() {
}
async function queryCurrentActiveTab(windowType) {
return new Promise((resolve) => {
// At the time of writing we only have the `activeTab` permission which means
// that this query will only succeed in the popup context (i.e. after a "browserAction")
if (windowType !== ENVIRONMENT_TYPE_POPUP) {
resolve({});
return;
}
// At the time of writing we only have the `activeTab` permission which means
// that this query will only succeed in the popup context (i.e. after a "browserAction")
if (windowType !== ENVIRONMENT_TYPE_POPUP) {
return {};
}
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
const [activeTab] = tabs;
const { id, title, url } = activeTab;
const { origin, protocol } = url ? new URL(url) : {};
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const [activeTab] = tabs;
const { id, title, url } = activeTab;
const { origin, protocol } = url ? new URL(url) : {};
if (!origin || origin === 'null') {
resolve({});
return;
}
if (!origin || origin === 'null') {
return {};
}
resolve({ id, title, origin, protocol, url });
});
});
return { id, title, origin, protocol, url };
}
function initializeUi(activeTab, connectionStream, cb) {