Skip to content

Commit

Permalink
fix: await opening a new tab before closing the popup (#636)
Browse files Browse the repository at this point in the history
* fix: await opening a new tab before closing the popup

* chore: enforce await everywhere
  • Loading branch information
MikkCZ authored Apr 11, 2023
1 parent fc35b0c commit 6dc8372
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions configs/.eslintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const config: ESLint.ConfigData = {
count: 1,
},
],
'require-await': 'error',
'@typescript-eslint/no-unused-vars': [
'error',
{
Expand Down
4 changes: 2 additions & 2 deletions src/background/RemotePontoon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function listenToMessagesFromClients() {
listenToMessagesExclusively(
BackgroundClientMessageType.UPDATE_TEAMS_LIST,
async () => {
return updateTeamsList();
return await updateTeamsList();
},
);
listenToMessagesExclusively(
Expand All @@ -82,7 +82,7 @@ export function listenToMessagesFromClients() {
listenToMessagesExclusively(
BackgroundClientMessageType.GET_TEAM_FROM_PONTOON,
async () => {
return getUsersTeamFromPontoon();
return await getUsersTeamFromPontoon();
},
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/background/backgroundClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function getTeam(): Promise<{ code: string }> {
}

async function getPontoonBaseUrl(): Promise<string> {
return getOneOption('pontoon_base_url');
return await getOneOption('pontoon_base_url');
}

export async function getNotificationsUrl(): Promise<string> {
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function reportTranslatedTextToBugzilla(text: string) {
export async function notificationBellIconScriptLoaded(): Promise<{
type: BackgroundClientMessageType;
}> {
return browser.runtime.sendMessage({
return await browser.runtime.sendMessage({
type: BackgroundClientMessageType.NOTIFICATIONS_BELL_SCRIPT_LOADED,
});
}
2 changes: 1 addition & 1 deletion src/background/httpClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const pontoonHttpClient = new PontoonHttpClient();

export const httpClient = {
fetch: async (url: string): Promise<Response> => {
return fetch(url, { credentials: 'omit' });
return await fetch(url, { credentials: 'omit' });
},
};

Expand Down
40 changes: 22 additions & 18 deletions src/commons/webExtensionsApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ export function listenToStorageChange<K extends keyof StorageContent>(
}

export async function saveToStorage(toSave: Partial<StorageContent>) {
return browser.storage.local.set(toSave);
return await browser.storage.local.set(toSave);
}

export async function deleteFromStorage<K extends keyof StorageContent>(
...storageKeys: K[]
) {
return browser.storage.local.remove(storageKeys);
return await browser.storage.local.remove(storageKeys);
}

export async function createNotification(
Expand All @@ -132,7 +132,7 @@ export async function createNotification(
}

export async function closeNotification(notificationId: string) {
return browser.notifications.clear(notificationId);
return await browser.notifications.clear(notificationId);
}

export function createContextMenu(
Expand All @@ -142,7 +142,7 @@ export function createContextMenu(
}

export async function removeContextMenu(menuItemId: number | string) {
return browser.contextMenus.remove(menuItemId);
return await browser.contextMenus.remove(menuItemId);
}

export function browserFamily(): BrowserFamily {
Expand All @@ -154,26 +154,26 @@ export function browserFamily(): BrowserFamily {
}

export async function openNewTab(url: string) {
return browser.tabs.create({ url });
return await browser.tabs.create({ url });
}

export async function getAllTabs() {
return browser.tabs.query({});
return await browser.tabs.query({});
}

export async function getTabsMatching(...patterns: string[]) {
return browser.tabs.query({ url: patterns });
return await browser.tabs.query({ url: patterns });
}

export async function getTabsWithBaseUrl(baseUrl: string) {
return getTabsMatching(`${baseUrl}/*`);
return await getTabsMatching(`${baseUrl}/*`);
}

export async function getActiveTab(): Promise<Tabs.Tab> {
return (await browser.tabs.query({ currentWindow: true, active: true }))[0];
}

export async function listenToTabsCompletedLoading(
export function listenToTabsCompletedLoading(
listener: (tab: Tabs.Tab & { id: number }) => void,
) {
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
Expand All @@ -188,23 +188,25 @@ export function getResourceUrl(path: string) {
}

export async function openOptions() {
return browser.runtime.openOptionsPage();
return await browser.runtime.openOptionsPage();
}

export async function openIntro() {
return openNewTab(browser.runtime.getURL('frontend/intro.html'));
return await openNewTab(browser.runtime.getURL('frontend/intro.html'));
}

export async function openPrivacyPolicy() {
return openNewTab(browser.runtime.getURL('frontend/privacy-policy.html'));
return await openNewTab(
browser.runtime.getURL('frontend/privacy-policy.html'),
);
}

export async function openSnakeGame() {
return openNewTab(browser.runtime.getURL('frontend/snake-game.html'));
return await openNewTab(browser.runtime.getURL('frontend/snake-game.html'));
}

export async function openToolbarButtonPopup() {
return browser.browserAction.openPopup();
return await browser.browserAction.openPopup();
}

export function supportsAddressBar(): boolean {
Expand Down Expand Up @@ -241,26 +243,28 @@ export function supportsContainers(): boolean {

export async function getAllContainers() {
if (supportsContainers()) {
return browser.contextualIdentities.query({});
return await browser.contextualIdentities.query({});
} else {
return [];
}
}

export async function requestPermissionForPontoon(pontoonBaseUrl: string) {
return browser.permissions.request({ origins: [`${pontoonBaseUrl}/*`] });
return await browser.permissions.request({
origins: [`${pontoonBaseUrl}/*`],
});
}

export async function registerScriptForBaseUrl(baseUrl: string, file: string) {
return browser.contentScripts.register({
return await browser.contentScripts.register({
js: [{ file }],
matches: [`${baseUrl}/*`],
runAt: 'document_end', // Corresponds to interactive. The DOM has finished loading, but resources such as scripts and images may still be loading.
});
}

export async function executeScript(tabId: number, file: string) {
return browser.tabs.executeScript(tabId, { file });
return await browser.tabs.executeScript(tabId, { file });
}

export function callWithInterval(
Expand Down
2 changes: 1 addition & 1 deletion src/commons/webExtensionsApi/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('webExtensionsApi', () => {
.expect(42, { file: 'foo/bar.js' })
.andResolve([]);

executeScript(42, 'foo/bar.js');
await executeScript(42, 'foo/bar.js');
});

it('callWithInterval', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function injectScript(src: string) {
document.head.prepend(script);
}

async function postMessage(): Promise<void> {
function postMessage() {
window.postMessage(
{
_type: 'PontoonAddonInfo',
Expand Down
12 changes: 6 additions & 6 deletions src/frontend/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('address bar', () => {
const root = prepareRoot('address-bar-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand All @@ -63,7 +63,7 @@ describe('intro', () => {
const root = prepareRoot('intro-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand All @@ -76,7 +76,7 @@ describe('options', () => {
const root = prepareRoot('options-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand All @@ -89,7 +89,7 @@ describe('privacy policy', () => {
const root = prepareRoot('privacy-policy-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand All @@ -102,7 +102,7 @@ describe('snake game', () => {
const root = prepareRoot('snake-game-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand Down Expand Up @@ -130,7 +130,7 @@ describe('toolbar button', () => {
const root = prepareRoot('toolbar-button-root');

await act(async () => {
await index();
index();
await flushPromises();
});

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function renderToolbarButtonApp(rootElement: HTMLElement) {
renderRoot(rootElement, <ToolbarButtonApp />);
}

export async function render(): Promise<void> {
export function render() {
const addressBarRoot = document.getElementById('address-bar-root');
const introRoot = document.getElementById('intro-root');
const optionsRoot = document.getElementById('options-root');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function wrapLinks(pontoonBaseUrl: string): HTMLReactParserOptions['replace'] {

async function openTeamProject(projectUrl: string): Promise<void> {
const teamProjectUrl = await getTeamProjectUrl(projectUrl);
openNewPontoonTab(teamProjectUrl);
await openNewPontoonTab(teamProjectUrl);
window.close();
}

Expand Down

0 comments on commit 6dc8372

Please sign in to comment.