Skip to content

Commit

Permalink
Merge pull request #284 from airgap-it/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AndreasGassmann authored Oct 8, 2021
2 parents 8b3405f + 82e8d96 commit 8224632
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airgap/beacon-sdk",
"version": "2.3.3",
"version": "2.3.4",
"description": "The beacon-sdk allows you to easily connect DApps with Wallets through P2P communication or a chrome extension.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
27 changes: 23 additions & 4 deletions src/clients/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import { desktopList, extensionList, iOSList, webList } from '../../ui/alert/wal
import { Optional } from '../../utils/utils'
import { DAppClientOptions } from './DAppClientOptions'
import { App, DesktopApp, ExtensionApp, WebApp } from '../../ui/alert/Pairing'
import { closeToast } from '../../ui/toast/Toast'

const logger = new Logger('DAppClient')

Expand Down Expand Up @@ -428,8 +429,8 @@ export class DAppClient extends Client {
await this.events.emit(BeaconEvent.SHOW_PREPARE, { walletInfo })
}

public async hideUI(): Promise<void> {
await this.events.emit(BeaconEvent.HIDE_UI)
public async hideUI(elements?: ('alert' | 'toast')[]): Promise<void> {
await this.events.emit(BeaconEvent.HIDE_UI, elements)
}

/**
Expand Down Expand Up @@ -858,7 +859,7 @@ export class DAppClient extends Client {
* @param errorMessage The error message to send.
*/
private async sendInternalError(errorMessage: string): Promise<void> {
await this.events.emit(BeaconEvent.INTERNAL_ERROR, errorMessage)
await this.events.emit(BeaconEvent.INTERNAL_ERROR, { text: errorMessage })
throw new Error(errorMessage)
}

Expand Down Expand Up @@ -1158,7 +1159,25 @@ export class DAppClient extends Client {

logger.log('makeRequest', 'sending message', request)
console.timeLog(messageId, 'sending')
await (await this.transport).send(payload, peer)
try {
await (await this.transport).send(payload, peer)
} catch (sendError) {
this.events.emit(BeaconEvent.INTERNAL_ERROR, {
text:
'Unable to send message. If this problem persists, please reset the connection and pair your wallet again.',
buttons: [
{
text: 'Reset Connection',
actionCallback: async (): Promise<void> => {
await closeToast()
this.disconnect()
}
}
]
})
console.timeLog(messageId, 'send error')
throw sendError
}
console.timeLog(messageId, 'sent')

this.events
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const SDK_VERSION: string = '2.3.3'
export const SDK_VERSION: string = '2.3.4'
export const BEACON_VERSION: string = '2'
26 changes: 19 additions & 7 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export interface BeaconEventType {
[BeaconEvent.ACTIVE_ACCOUNT_SET]: AccountInfo
[BeaconEvent.ACTIVE_TRANSPORT_SET]: Transport
[BeaconEvent.SHOW_PREPARE]: { walletInfo?: WalletInfo }
[BeaconEvent.HIDE_UI]: undefined
[BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined
[BeaconEvent.PAIR_INIT]: {
p2pPeerInfo: () => Promise<P2PPairingRequest>
postmessagePeerInfo: () => Promise<PostMessagePairingRequest>
Expand All @@ -157,7 +157,7 @@ export interface BeaconEventType {
}
[BeaconEvent.PAIR_SUCCESS]: ExtendedPostMessagePairingResponse | ExtendedP2PPairingResponse
[BeaconEvent.CHANNEL_CLOSED]: string
[BeaconEvent.INTERNAL_ERROR]: string
[BeaconEvent.INTERNAL_ERROR]: { text: string; buttons?: AlertButton[] }
[BeaconEvent.UNKNOWN]: undefined
}

Expand Down Expand Up @@ -237,9 +237,17 @@ const showPrepare = async (data: { walletInfo?: WalletInfo }): Promise<void> =>
}).catch((toastError) => console.error(toastError))
}

const hideUI = async (): Promise<void> => {
closeToast()
closeAlerts()
const hideUI = async (elements?: ('alert' | 'toast')[]): Promise<void> => {
if (elements) {
if (elements.includes('alert')) {
closeAlerts()
}
if (elements.includes('toast')) {
closeToast()
}
} else {
closeToast()
}
}

/**
Expand Down Expand Up @@ -339,10 +347,14 @@ const showChannelClosedAlert = async (): Promise<void> => {
const showInternalErrorAlert = async (
data: BeaconEventType[BeaconEvent.INTERNAL_ERROR]
): Promise<void> => {
const buttons: AlertButton[] = [...(data.buttons ?? [])]

buttons.push({ text: 'Done', style: 'outline' })

const alertConfig: AlertConfig = {
title: 'Internal Error',
body: `${data}`,
buttons: [{ text: 'Done', style: 'outline' }]
body: data.text,
buttons
}
await openAlert(alertConfig)
}
Expand Down
4 changes: 2 additions & 2 deletions test/clients/dapp-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ describe(`DAppClient`, () => {
expect(eventsStub.firstCall.args[0]).to.equal(BeaconEvent.ACTIVE_TRANSPORT_SET) // Called in the constructor
expect(eventsStub.firstCall.args[1]).to.equal(undefined)
expect(eventsStub.secondCall.args[0]).to.equal(BeaconEvent.INTERNAL_ERROR)
expect(eventsStub.secondCall.args[1]).to.equal('No active account set!')
expect(eventsStub.secondCall.args[1]).to.deep.equal({ text: 'No active account set!' })
expect(eventsStub.thirdCall.args[0]).to.equal(BeaconEvent.ACTIVE_ACCOUNT_SET) // Called in the constructor
expect(eventsStub.thirdCall.args[1]).to.equal(undefined)
expect(eventsStub.callCount).to.equal(3)
Expand Down Expand Up @@ -861,7 +861,7 @@ describe(`DAppClient`, () => {
} catch (e) {
expect(eventsStub.callCount).to.equal(2)
expect(eventsStub.firstCall.args[0]).to.equal(BeaconEvent.INTERNAL_ERROR)
expect(eventsStub.firstCall.args[1]).to.equal('some-message')
expect(eventsStub.firstCall.args[1]).to.deep.equal({ text: 'some-message' })
expect(eventsStub.secondCall.args[0]).to.equal(BeaconEvent.ACTIVE_TRANSPORT_SET)
expect(eventsStub.secondCall.args[1]).to.equal(undefined)
expect(e.message).to.equal('some-message')
Expand Down

0 comments on commit 8224632

Please sign in to comment.