Skip to content

Commit

Permalink
fix: loosen types on ble inbound and outbound transport and session (#…
Browse files Browse the repository at this point in the history
…226)

Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Sep 18, 2023
1 parent 1dc0f23 commit 17c6203
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
18 changes: 9 additions & 9 deletions packages/transport-ble/src/transports/BleInboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Central } from '@animo-id/react-native-ble-didcomm'
import type { Ble } from '@animo-id/react-native-ble-didcomm'
import type { Agent, InboundTransport, Logger } from '@aries-framework/core'
import type { EmitterSubscription } from 'react-native'

Expand All @@ -8,22 +8,22 @@ import { BleTransportSession } from './BleTransportSession'

export class BleInboundTransport implements InboundTransport {
public supportedSchemes: string[] = ['ble']
private central: Central
private messenger: Ble
private messageListener?: EmitterSubscription
private session!: BleTransportSession
private disconnectionListener?: EmitterSubscription
private logger?: Logger

public constructor(central: Central) {
this.central = central
public constructor(messenger: Ble) {
this.messenger = messenger
}

public async start(agent: Agent): Promise<void> {
this.logger = agent.config.logger
this.logger.debug('Starting BLE inbound transport')

const sessionId = utils.uuid()
this.session = new BleTransportSession(sessionId, this.central, agent)
this.session = new BleTransportSession(sessionId, this.messenger, agent.context)

const messageListener = async (data: { message: string }) => {
const message = data.message
Expand All @@ -37,11 +37,11 @@ export class BleInboundTransport implements InboundTransport {
session: this.session,
})
} catch (error) {
agent.config.logger.error(`Error processing message: ${error}`)
this.logger?.error(`Error processing message: ${error}`)
}
}

this.messageListener = this.central.registerMessageListener(messageListener)
this.messageListener = this.messenger.registerMessageListener(messageListener)

const disconnectionListener = async (data: { identifier: string }) => {
this.logger?.debug('BLE disconnection detected', { data })
Expand All @@ -50,14 +50,14 @@ export class BleInboundTransport implements InboundTransport {
transportService.removeSession(this.session)
}

this.disconnectionListener = this.central.registerOnDisconnectedListener(disconnectionListener)
this.disconnectionListener = this.messenger.registerOnDisconnectedListener(disconnectionListener)
}

public async stop(): Promise<void> {
this.logger?.debug('Stopping BLE inbound transport')

this.messageListener?.remove()
this.disconnectionListener?.remove()
await this.central.shutdown()
await this.messenger.shutdown()
}
}
14 changes: 7 additions & 7 deletions packages/transport-ble/src/transports/BleOutboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type { Peripheral } from '@animo-id/react-native-ble-didcomm'
import type { Ble } from '@animo-id/react-native-ble-didcomm'
import type { Agent, Logger, OutboundPackage, OutboundTransport } from '@aries-framework/core'

import { AriesFrameworkError } from '@aries-framework/core'

export class BleOutboundTransport implements OutboundTransport {
public supportedSchemes: string[] = ['ble']
private peripheral: Peripheral
private messenger: Ble
private logger?: Logger

public constructor(peripheral: Peripheral) {
this.peripheral = peripheral
public constructor(messenger: Ble) {
this.messenger = messenger
}

public async start(agent: Agent): Promise<void> {
this.logger = agent.config.logger

agent.config.logger.debug('Starting BLE outbound transport')
this.logger.debug('Starting BLE outbound transport')
}

public async sendMessage(outboundPackage: OutboundPackage): Promise<void> {
Expand All @@ -31,11 +31,11 @@ export class BleOutboundTransport implements OutboundTransport {
const serializedMessage = JSON.stringify(payload)

this.logger?.debug('Sending BLE outbound message')
await this.peripheral.sendMessage(serializedMessage)
await this.messenger.sendMessage(serializedMessage)
}

public async stop(): Promise<void> {
this.logger?.debug('Stopping BLE outbound transport')
await this.peripheral.shutdown()
await this.messenger.shutdown()
}
}
21 changes: 11 additions & 10 deletions packages/transport-ble/src/transports/BleTransportSession.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import type { Central } from '@animo-id/react-native-ble-didcomm'
import type { Agent, AgentContext, EncryptedMessage, TransportSession } from '@aries-framework/core'
import type { Ble } from '@animo-id/react-native-ble-didcomm'
import type { AgentContext, EncryptedMessage, TransportSession } from '@aries-framework/core'

import { utils } from '@aries-framework/core'

export class BleTransportSession implements TransportSession {
public readonly type = 'ble'
public id: string
private agent: Agent
private central: Central

public constructor(id: string, central: Central, agent: Agent) {
private agentContext: AgentContext
private messenger: Ble

public constructor(id: string, messenger: Ble, agentContext: AgentContext) {
this.id = id ?? utils.uuid()
this.agent = agent
this.central = central
this.messenger = messenger
this.agentContext = agentContext
}

public async send(agentContext: AgentContext, encryptedMessage: EncryptedMessage): Promise<void> {
const serializedMessage = JSON.stringify(encryptedMessage)

this.agent.config.logger.debug('Sending BLE inbound message via session')
await this.central.sendMessage(serializedMessage)
agentContext.config.logger.debug('Sending BLE inbound message via session')
await this.messenger.sendMessage(serializedMessage)
}

public async close(): Promise<void> {
this.agent.config.logger.debug('Stopping BLE inbound session')
this.agentContext.config.logger.debug('Stopping BLE inbound session')
}
}

0 comments on commit 17c6203

Please sign in to comment.