-
Notifications
You must be signed in to change notification settings - Fork 9
/
channelKeeper.ts
43 lines (36 loc) · 1.11 KB
/
channelKeeper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { FlowType } from '../interactionManager/types'
import { Interaction } from '../interactionManager/interaction'
import { Channel } from './channel'
import { Agent } from '../agent'
import { TransportAPI } from '../types'
export class ChannelKeeper {
ctx: Agent
private _channels: {
[NONCE: string]: Channel
} = {}
constructor(ctx: Agent) {
this.ctx = ctx
}
public async get(id: string) {
const ch = this._channels[id]
if (!ch) throw new Error('no such channel: ' + JSON.stringify(id))
return ch
}
public async create(initInterxn: Interaction, transportAPI?: TransportAPI) {
if (initInterxn.flow.type !== FlowType.EstablishChannel) {
throw new Error(
'not an EstablishChannel interaction: ' + initInterxn.flow.type,
)
}
const ch = new Channel(this, initInterxn, transportAPI)
this._channels[ch.id] = ch
return ch
}
async findByJWT(jwt: string) {
const interxn = await this.ctx.findInteraction(jwt)
const chId = interxn && interxn.flow.type === FlowType.EstablishChannel
? interxn.id
: ''
return this.get(chId)
}
}