Skip to content

Commit

Permalink
fix: localStorage internal hang
Browse files Browse the repository at this point in the history
  • Loading branch information
isordo committed Oct 3, 2023
1 parent 2c24e0f commit b6aa67a
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ export class DAppClient extends Client {
* The currently active account. For all requests that are associated to a specific request (operation request, signing request),
* the active account is used to determine the network and destination wallet
*/
private _activeAccount: ExposedPromise<AccountInfo | undefined> = new ExposedPromise()
private _activeAccount: Promise<AccountInfo | undefined> = Promise.resolve(undefined)

/**
* The currently active peer. This is used to address a peer in case the active account is not set. (Eg. for permission requests)
*/
private _activePeer: ExposedPromise<PeerInfoType | undefined> = new ExposedPromise()
private _activePeer: Promise<PeerInfoType | undefined> = Promise.resolve(undefined)

private _initPromise: Promise<TransportType> | undefined

Expand Down Expand Up @@ -205,19 +205,7 @@ export class DAppClient extends Client {

this.appMetadataManager = new AppMetadataManager(this.storage)

this.activeAccountLoaded = this.storage
.get(StorageKey.ACTIVE_ACCOUNT)
.then(async (activeAccountIdentifier) => {
if (activeAccountIdentifier) {
await this.setActiveAccount(await this.accountManager.getAccount(activeAccountIdentifier))
} else {
await this.setActiveAccount(undefined)
}
})
.catch(async (storageError) => {
await this.setActiveAccount(undefined)
console.error(storageError)
})
this.activeAccountLoaded = this.initStorage()

this.handleResponse = async (
message: BeaconMessage | BeaconMessageWrapper<BeaconBaseMessage>,
Expand Down Expand Up @@ -361,6 +349,23 @@ export class DAppClient extends Client {
}
}

private promiseState<T>(promise: Promise<T>) {
const t = {};
return Promise.race([promise, t])
.then(v => (v === t)? "pending" : "fulfilled", () => "rejected");
}

private async initStorage() {
const id = await this.storage.get(StorageKey.ACTIVE_ACCOUNT)

if (!id) {
await this.setActiveAccount(undefined)
return
}

await this.setActiveAccount(await this.accountManager.getAccount(id))
}

public async initInternalTransports(): Promise<void> {
const keyPair = await this.keyPair

Expand Down Expand Up @@ -453,11 +458,7 @@ export class DAppClient extends Client {
return this._initPromise
}

try {
await this.activeAccountLoaded
} catch {
//
}
this.activeAccountLoaded.catch((error) => console.error(error))

this._initPromise = new Promise(async (resolve) => {
if (transport) {
Expand Down Expand Up @@ -594,11 +595,11 @@ export class DAppClient extends Client {
* Returns the active account
*/
public async getActiveAccount(): Promise<AccountInfo | undefined> {
return this._activeAccount.promise
return await this._activeAccount
}

private async isInvalidState(account: AccountInfo) {
const activeAccount = await this._activeAccount.promise
const activeAccount = await this._activeAccount
return !activeAccount
? false
: activeAccount?.address !== account.address && !this.isGetActiveAccountHandled
Expand All @@ -620,7 +621,11 @@ export class DAppClient extends Client {
}

// when I'm resetting the activeAccount
if (!account && this._activeAccount.isResolved() && (await this.getActiveAccount())) {
if (
!account &&
(await this.promiseState(this._activeAccount)) !== 'pending' &&
(await this.getActiveAccount())
) {
const transport = await this.transport
const activeAccount = await this.getActiveAccount()

Expand All @@ -633,12 +638,7 @@ export class DAppClient extends Client {
}
}

if (this._activeAccount.isSettled()) {
// If the promise has already been resolved we need to create a new one.
this._activeAccount = ExposedPromise.resolve<AccountInfo | undefined>(account)
} else {
this._activeAccount.resolve(account)
}
this._activeAccount = Promise.resolve(account)

if (account) {
const origin = account.origin.type
Expand Down Expand Up @@ -1292,12 +1292,7 @@ export class DAppClient extends Client {
}

protected async setActivePeer(peer?: PeerInfoType): Promise<void> {
if (this._activePeer.isSettled()) {
// If the promise has already been resolved we need to create a new one.
this._activePeer = ExposedPromise.resolve(peer)
} else {
this._activePeer.resolve(peer)
}
this._activePeer = Promise.resolve(peer)

if (!peer) {
return
Expand Down Expand Up @@ -1564,7 +1559,7 @@ export class DAppClient extends Client {
peer = peers.find((peerEl) => (peerEl as any).extensionId === account.origin.id)
}
} else {
peer = await this._activePeer.promise
peer = await this._activePeer
logger.log('getPeer', 'Active peer', peer)
}

Expand Down

0 comments on commit b6aa67a

Please sign in to comment.