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

View File

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