Skip to content

Commit

Permalink
feat(events): pass overrides in constructor and log successful responses
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasGassmann committed May 1, 2020
1 parent 5fcde3b commit 1090ade
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/clients/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export abstract class Client extends BeaconClient {
protected readonly rateLimit: number = 2
protected readonly rateLimitWindowInSeconds: number = 5

protected readonly events: BeaconEventHandler = new BeaconEventHandler()
protected readonly events: BeaconEventHandler

protected _transport: ExposedPromise<Transport> = new ExposedPromise()
protected get transport(): Promise<Transport> {
Expand All @@ -47,11 +47,7 @@ export abstract class Client extends BeaconClient {
constructor(config: ClientOptions) {
super({ name: config.name, storage: config.storage })

if (config.eventHandlers) {
this.events.overrideDefaults(config.eventHandlers).catch((overrideError: Error) => {
logger.error('constructor', overrideError)
})
}
this.events = new BeaconEventHandler(config.eventHandlers)

this.handleResponse = (message: BeaconBaseMessage, connectionInfo: ConnectionContext): void => {
throw new Error(
Expand Down
10 changes: 9 additions & 1 deletion src/clients/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ export class DAppClient extends Client {

await (await this.transport).send(payload)

return exposed.promise as any // TODO: Type
exposed.promise
.then((promiseResult: { message: BeaconMessage; connectionInfo: ConnectionContext }) => {
this.events
.emit(messageEvents[requestInput.type].success, promiseResult)
.catch((emitError) => console.warn(emitError))
})
.catch(() => undefined)

return exposed.promise as any // TODO: fix type
}
}
42 changes: 40 additions & 2 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { getQrData } from './utils/qr'
import { Logger } from './utils/Logger'
import { Transport } from './transports/Transport'
import { BeaconError } from './errors/BeaconError'
import { P2PPairInfo, AccountInfo, BeaconErrorMessage, UnknownBeaconError } from '.'
import { ConnectionContext } from './types/ConnectionContext'
import { P2PPairInfo, AccountInfo, BeaconErrorMessage, UnknownBeaconError, BeaconMessage } from '.'

const logger = new Logger('BeaconEvents')

export enum BeaconEvent {
PERMISSION_REQUEST_SENT = 'PERMISSION_REQUEST_SENT',
PERMISSION_REQUEST_SUCCESS = 'PERMISSION_REQUEST_SUCCESS',
PERMISSION_REQUEST_ERROR = 'PERMISSION_REQUEST_ERROR',
OPERATION_REQUEST_SENT = 'OPERATION_REQUEST_SENT',
OPERATION_REQUEST_SUCCESS = 'OPERATION_REQUEST_SUCCESS',
OPERATION_REQUEST_ERROR = 'OPERATION_REQUEST_ERROR',
SIGN_REQUEST_SENT = 'SIGN_REQUEST_SENT',
SIGN_REQUEST_SUCCESS = 'SIGN_REQUEST_SUCCESS',
SIGN_REQUEST_ERROR = 'SIGN_REQUEST_ERROR',
BROADCAST_REQUEST_SENT = 'BROADCAST_REQUEST_SENT',
BROADCAST_REQUEST_SUCCESS = 'BROADCAST_REQUEST_SUCCESS',
BROADCAST_REQUEST_ERROR = 'BROADCAST_REQUEST_ERROR',

LOCAL_RATE_LIMIT_REACHED = 'LOCAL_RATE_LIMIT_REACHED',
Expand All @@ -34,12 +39,25 @@ export enum BeaconEvent {

export interface BeaconEventType {
[BeaconEvent.PERMISSION_REQUEST_SENT]: undefined
[BeaconEvent.PERMISSION_REQUEST_SUCCESS]: {
message: BeaconMessage
connectionInfo: ConnectionContext
}
[BeaconEvent.PERMISSION_REQUEST_ERROR]: BeaconErrorMessage
[BeaconEvent.OPERATION_REQUEST_SENT]: undefined
[BeaconEvent.OPERATION_REQUEST_SUCCESS]: {
message: BeaconMessage
connectionInfo: ConnectionContext
}
[BeaconEvent.OPERATION_REQUEST_ERROR]: BeaconErrorMessage
[BeaconEvent.SIGN_REQUEST_SENT]: undefined
[BeaconEvent.SIGN_REQUEST_SUCCESS]: { message: BeaconMessage; connectionInfo: ConnectionContext }
[BeaconEvent.SIGN_REQUEST_ERROR]: BeaconErrorMessage
[BeaconEvent.BROADCAST_REQUEST_SENT]: undefined
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: {
message: BeaconMessage
connectionInfo: ConnectionContext
}
[BeaconEvent.BROADCAST_REQUEST_ERROR]: BeaconErrorMessage
[BeaconEvent.PERMISSION_REQUEST_SENT]: undefined
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: undefined
Expand Down Expand Up @@ -114,12 +132,16 @@ export const defaultEventCallbacks: {
[key in BeaconEvent]: BeaconEventHandlerFunction<BeaconEventType[key]>
} = {
[BeaconEvent.PERMISSION_REQUEST_SENT]: showSentToast,
[BeaconEvent.PERMISSION_REQUEST_SUCCESS]: emptyHandler(BeaconEvent.PERMISSION_REQUEST_SUCCESS),
[BeaconEvent.PERMISSION_REQUEST_ERROR]: showErrorAlert,
[BeaconEvent.OPERATION_REQUEST_SENT]: showSentToast,
[BeaconEvent.OPERATION_REQUEST_SUCCESS]: emptyHandler(BeaconEvent.OPERATION_REQUEST_SUCCESS),
[BeaconEvent.OPERATION_REQUEST_ERROR]: showErrorAlert,
[BeaconEvent.SIGN_REQUEST_SENT]: showSentToast,
[BeaconEvent.SIGN_REQUEST_SUCCESS]: emptyHandler(BeaconEvent.SIGN_REQUEST_SUCCESS),
[BeaconEvent.SIGN_REQUEST_ERROR]: showErrorAlert,
[BeaconEvent.BROADCAST_REQUEST_SENT]: showSentToast,
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: emptyHandler(BeaconEvent.BROADCAST_REQUEST_SUCCESS),
[BeaconEvent.BROADCAST_REQUEST_ERROR]: showErrorAlert,
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: showRateLimitReached,
[BeaconEvent.NO_PERMISSIONS]: showNoPermissionAlert,
Expand All @@ -135,12 +157,16 @@ export class BeaconEventHandler {
[key in BeaconEvent]: BeaconEventHandlerFunction<any>[]
} = {
[BeaconEvent.PERMISSION_REQUEST_SENT]: [defaultEventCallbacks.PERMISSION_REQUEST_SENT],
[BeaconEvent.PERMISSION_REQUEST_SUCCESS]: [defaultEventCallbacks.PERMISSION_REQUEST_SUCCESS],
[BeaconEvent.PERMISSION_REQUEST_ERROR]: [defaultEventCallbacks.PERMISSION_REQUEST_ERROR],
[BeaconEvent.OPERATION_REQUEST_SENT]: [defaultEventCallbacks.OPERATION_REQUEST_SENT],
[BeaconEvent.OPERATION_REQUEST_SUCCESS]: [defaultEventCallbacks.OPERATION_REQUEST_SUCCESS],
[BeaconEvent.OPERATION_REQUEST_ERROR]: [defaultEventCallbacks.OPERATION_REQUEST_ERROR],
[BeaconEvent.SIGN_REQUEST_SENT]: [defaultEventCallbacks.SIGN_REQUEST_SENT],
[BeaconEvent.SIGN_REQUEST_SUCCESS]: [defaultEventCallbacks.SIGN_REQUEST_SUCCESS],
[BeaconEvent.SIGN_REQUEST_ERROR]: [defaultEventCallbacks.SIGN_REQUEST_ERROR],
[BeaconEvent.BROADCAST_REQUEST_SENT]: [defaultEventCallbacks.BROADCAST_REQUEST_SENT],
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: [defaultEventCallbacks.BROADCAST_REQUEST_SUCCESS],
[BeaconEvent.BROADCAST_REQUEST_ERROR]: [defaultEventCallbacks.BROADCAST_REQUEST_ERROR],
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: [defaultEventCallbacks.LOCAL_RATE_LIMIT_REACHED],
[BeaconEvent.NO_PERMISSIONS]: [defaultEventCallbacks.NO_PERMISSIONS],
Expand All @@ -151,6 +177,18 @@ export class BeaconEventHandler {
[BeaconEvent.UNKNOWN]: [defaultEventCallbacks.UNKNOWN]
}

constructor(
eventsToOverride?: {
[key in BeaconEvent]?: {
handler: BeaconEventHandlerFunction
}
}
) {
this.overrideDefaults(eventsToOverride || {}).catch((overrideError: Error) => {
logger.error('constructor', overrideError)
})
}

public async on<K extends BeaconEvent>(
event: K,
eventCallback: BeaconEventHandlerFunction<BeaconEventType[K]>
Expand All @@ -173,7 +211,7 @@ export class BeaconEventHandler {
}
}

public async overrideDefaults(
private async overrideDefaults(
eventsToOverride: {
[key in BeaconEvent]?: {
handler: BeaconEventHandlerFunction
Expand Down

0 comments on commit 1090ade

Please sign in to comment.