Skip to content

Commit

Permalink
revert async changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed May 30, 2024
1 parent 8beb2cb commit 6eadee6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ describe('Extension', () => {
url: 'http://localhost:3000'
};
await chrome.storage.local.set({ authUrls: JSON.stringify(authUrls) });
// eslint-disable-next-line @typescript-eslint/await-thenable
state = await new State();
state = new State();
tabs = new Tabs(state);

return new Extension(state);
Expand Down
20 changes: 9 additions & 11 deletions packages/extension-base/src/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class Extension {
};
}

private async accountsForget ({ address }: RequestAccountForget): Promise<boolean> {
private accountsForget ({ address }: RequestAccountForget): boolean {
const authorizedAccountsDiff: AuthorizedAccountsDiff = [];

// cycle through authUrls and prepare the array of diff
Expand All @@ -128,12 +128,12 @@ export default class Extension {
authorizedAccountsDiff.push([url, urlInfo.authorizedAccounts.filter((previousAddress) => previousAddress !== address)]);
});

await this.#state.updateAuthorizedAccounts(authorizedAccountsDiff);
this.#state.updateAuthorizedAccounts(authorizedAccountsDiff);

// cycle through default account selection for auth and remove any occurrence of the account
const newDefaultAuthAccounts = this.#state.defaultAuthAccountSelection.filter((defaultSelectionAddress) => defaultSelectionAddress !== address);

await this.#state.updateDefaultAuthAccounts(newDefaultAuthAccounts);
this.#state.updateDefaultAuthAccounts(newDefaultAuthAccounts);

keyring.forgetAccount(address);

Expand Down Expand Up @@ -212,8 +212,8 @@ export default class Extension {
return true;
}

private async authorizeUpdate ({ authorizedAccounts, url }: RequestUpdateAuthorizedAccounts): Promise<void> {
return await this.#state.updateAuthorizedAccounts([[url, authorizedAccounts]]);
private authorizeUpdate ({ authorizedAccounts, url }: RequestUpdateAuthorizedAccounts): void {
return this.#state.updateAuthorizedAccounts([[url, authorizedAccounts]]);
}

private getAuthList (): ResponseAuthorizeList {
Expand Down Expand Up @@ -518,10 +518,8 @@ export default class Extension {
return true;
}

private async removeAuthorization (url: string): Promise<ResponseAuthorizeList> {
const remAuth = await this.#state.removeAuthorization(url);

return { list: remAuth };
private removeAuthorization (url: string): ResponseAuthorizeList {
return { list: this.#state.removeAuthorization(url) };
}

private deleteAuthRequest (requestId: string): void {
Expand All @@ -547,7 +545,7 @@ export default class Extension {
return this.getAuthList();

case 'pri(authorize.remove)':
return await this.removeAuthorization(request as string);
return this.removeAuthorization(request as string);

case 'pri(authorize.delete.request)':
return this.deleteAuthRequest(request as string);
Expand All @@ -556,7 +554,7 @@ export default class Extension {
return port && this.authorizeSubscribe(id, port);

case 'pri(authorize.update)':
return await this.authorizeUpdate(request as RequestUpdateAuthorizedAccounts);
return this.authorizeUpdate(request as RequestUpdateAuthorizedAccounts);

case 'pri(accounts.create.external)':
return this.accountsCreateExternal(request as RequestAccountCreateExternal);
Expand Down
69 changes: 29 additions & 40 deletions packages/extension-base/src/background/handlers/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface SignRequest extends Resolver<ResponseSigning> {
url: string;
}

const NOTIFICATION_URL = chrome.runtime.getURL('notification.html');
const NOTIFICATION_URL = chrome.extension.getURL('notification.html');

const POPUP_WINDOW_OPTS: chrome.windows.CreateData = {
focused: true,
Expand Down Expand Up @@ -123,7 +123,7 @@ function extractMetadata (store: MetadataStore): void {
}

export default class State {
#authUrls: AuthUrls = {};
readonly #authUrls: AuthUrls = {};

readonly #authRequests: Record<string, AuthRequest> = {};

Expand Down Expand Up @@ -156,28 +156,19 @@ export default class State {
constructor (providers: Providers = {}) {
this.#providers = providers;

// Via the transition from v2 - v3 manifest localStorage is not available in service workers.
// Therefore we invoke an IIFE so that we can set state in the constructor with async/await.
// This requires the `new State` call to now be `await new State`.
return (async (): Promise<State> => {
extractMetadata(this.#metaStore);
extractMetadata(this.#metaStore);

// retrieve previously set authorizations
const storageAuthUrls: Record<string, string> = await chrome.storage.local.get(AUTH_URLS_KEY);
const authString = storageAuthUrls?.[AUTH_URLS_KEY] || '{}';
const previousAuth = JSON.parse(authString) as AuthUrls;
// retrieve previously set authorizations
const authString = localStorage.getItem(AUTH_URLS_KEY) || '{}';
const previousAuth = JSON.parse(authString) as AuthUrls;

this.#authUrls = previousAuth;
this.#authUrls = previousAuth;

// retrieve previously set default auth accounts
const storageDefaultAuthAccounts: Record<string, string> = await chrome.storage.local.get(DEFAULT_AUTH_ACCOUNTS);
const defaultAuthString: string = storageDefaultAuthAccounts?.[DEFAULT_AUTH_ACCOUNTS] || '[]';
const previousDefaultAuth = JSON.parse(defaultAuthString) as string[];
// retrieve previously set default auth accounts
const defaultAuthString = localStorage.getItem(DEFAULT_AUTH_ACCOUNTS) || '[]';
const previousDefaultAuth = JSON.parse(defaultAuthString) as string[];

this.defaultAuthAccountSelection = previousDefaultAuth;

return this;
})() as unknown as State;
this.defaultAuthAccountSelection = previousDefaultAuth;
}

public get knownMetadata (): MetadataDef[] {
Expand Down Expand Up @@ -239,7 +230,7 @@ export default class State {
}

private authComplete = (id: string, resolve: (resValue: AuthResponse) => void, reject: (error: Error) => void): Resolver<AuthResponse> => {
const complete = async (authorizedAccounts: string[] = []) => {
const complete = (authorizedAccounts: string[] = []) => {
const { idStr, request: { origin }, url } = this.#authRequests[id];

this.#authUrls[this.stripUrl(url)] = {
Expand All @@ -250,21 +241,19 @@ export default class State {
url
};

await this.saveCurrentAuthList();
await this.updateDefaultAuthAccounts(authorizedAccounts);
this.saveCurrentAuthList();
this.updateDefaultAuthAccounts(authorizedAccounts);
delete this.#authRequests[id];
this.updateIconAuth(true);
};

return {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
reject: async (error: Error): Promise<void> => {
await complete();
reject: (error: Error): void => {
complete();
reject(error);
},
// eslint-disable-next-line @typescript-eslint/no-misused-promises
resolve: async ({ authorizedAccounts, result }: AuthResponse): Promise<void> => {
await complete(authorizedAccounts);
resolve: ({ authorizedAccounts, result }: AuthResponse): void => {
complete(authorizedAccounts);
resolve({ authorizedAccounts, result });
}
};
Expand Down Expand Up @@ -300,17 +289,17 @@ export default class State {
this.updateIconAuth(true);
}

private async saveCurrentAuthList () {
await chrome.storage.local.set({ [AUTH_URLS_KEY]: JSON.stringify(this.#authUrls) });
private saveCurrentAuthList () {
localStorage.setItem(AUTH_URLS_KEY, JSON.stringify(this.#authUrls));
}

private async saveDefaultAuthAccounts () {
await chrome.storage.local.set({ [DEFAULT_AUTH_ACCOUNTS]: JSON.stringify(this.defaultAuthAccountSelection) });
private saveDefaultAuthAccounts () {
localStorage.setItem(DEFAULT_AUTH_ACCOUNTS, JSON.stringify(this.defaultAuthAccountSelection));
}

public async updateDefaultAuthAccounts (newList: string[]) {
public updateDefaultAuthAccounts (newList: string[]) {
this.defaultAuthAccountSelection = newList;
await this.saveDefaultAuthAccounts();
this.saveDefaultAuthAccounts();
}

private metaComplete = (id: string, resolve: (result: boolean) => void, reject: (error: Error) => void): Resolver<boolean> => {
Expand Down Expand Up @@ -369,20 +358,20 @@ export default class State {
: (signCount ? `${signCount}` : '')
);

withErrorLog(() => chrome.action.setBadgeText({ text }));
withErrorLog(() => chrome.browserAction.setBadgeText({ text }));

if (shouldClose && text === '') {
this.popupClose();
}
}

public async removeAuthorization (url: string): Promise<AuthUrls> {
public removeAuthorization (url: string): AuthUrls {
const entry = this.#authUrls[url];

assert(entry, `The source ${url} is not known`);

delete this.#authUrls[url];
await this.saveCurrentAuthList();
this.saveCurrentAuthList();

return this.#authUrls;
}
Expand All @@ -402,12 +391,12 @@ export default class State {
this.updateIcon(shouldClose);
}

public async updateAuthorizedAccounts (authorizedAccountDiff: AuthorizedAccountsDiff): Promise<void> {
public updateAuthorizedAccounts (authorizedAccountDiff: AuthorizedAccountsDiff): void {
authorizedAccountDiff.forEach(([url, authorizedAccountDiff]) => {
this.#authUrls[url].authorizedAccounts = authorizedAccountDiff;
});

await this.saveCurrentAuthList();
this.saveCurrentAuthList();
}

public async authorizeUrl (url: string, request: RequestAuthorizeTab): Promise<AuthResponse> {
Expand Down
3 changes: 1 addition & 2 deletions packages/extension-base/src/background/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import Tabs from './Tabs.js';

export { withErrorLog } from './helpers.js';

// eslint-disable-next-line @typescript-eslint/await-thenable
const state = await new State();
const state = new State();
const extension = new Extension(state);
const tabs = new Tabs(state);

Expand Down
2 changes: 2 additions & 0 deletions packages/extension-ui/src/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default function Popup (): React.ReactElement {
const [settingsCtx, setSettingsCtx] = useState<SettingsStruct>(startSettings);
const history = useHistory();

console.log('WINDOW; ', window);

const _onAction = useCallback(
(to?: string): void => {
setWelcomeDone(window.localStorage.getItem('welcome_read') === 'ok');
Expand Down

0 comments on commit 6eadee6

Please sign in to comment.