-
Notifications
You must be signed in to change notification settings - Fork 132
/
DefaultPrivateChannel.ts
89 lines (76 loc) · 3.47 KB
/
DefaultPrivateChannel.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { ApiEvent, ContextHandler, EventHandler, Listener, PrivateChannel, PrivateChannelEventTypes } from "@kite9/fdc3-standard";
import { BrowserTypes } from "@kite9/fdc3-schema";
import { DefaultChannel } from "./DefaultChannel";
import { Messaging } from "../Messaging";
import { PrivateChannelEventListenerType, PrivateChannelEventListenerVoid } from "../listeners/PrivateChannelEventListener";
import { DefaultContextListener } from "../listeners/DefaultContextListener";
type PrivateChannelDisconnectRequest = BrowserTypes.PrivateChannelDisconnectRequest
type PrivateChannelDisconnectResponse = BrowserTypes.PrivateChannelDisconnectResponse
export class DefaultPrivateChannel extends DefaultChannel implements PrivateChannel {
constructor(messaging: Messaging, id: string) {
super(messaging, id, "private")
}
async addEventListener(type: PrivateChannelEventTypes | null, handler: EventHandler): Promise<Listener> {
function wrapEventHandlerString(): (m: string) => void {
return (m: string) => {
handler({
type,
details: m
} as ApiEvent)
}
}
function wrapEventHandlerVoid(): () => void {
return () => {
handler({
type
} as ApiEvent)
}
}
if (type) {
switch (type) {
case "addContextListener":
const a = new PrivateChannelEventListenerType(this.messaging, this.id, "onAddContextListener", wrapEventHandlerString());
await a.register()
return a;
case "unsubscribe":
const u = new PrivateChannelEventListenerType(this.messaging, this.id, "onUnsubscribe", wrapEventHandlerString());
await u.register()
return u;
case "disconnect":
const d = new PrivateChannelEventListenerVoid(this.messaging, this.id, wrapEventHandlerVoid());
await d.register()
return d;
}
}
throw new Error("Unsupported event type: " + type)
}
onAddContextListener(handler: (contextType?: string | undefined) => void): Listener {
const l = new PrivateChannelEventListenerType(this.messaging, this.id, "onAddContextListener", handler);
l.register()
return l;
}
onUnsubscribe(handler: (contextType?: string | undefined) => void): Listener {
const l = new PrivateChannelEventListenerType(this.messaging, this.id, "onUnsubscribe", handler);
l.register()
return l;
}
onDisconnect(handler: () => void): Listener {
const l = new PrivateChannelEventListenerVoid(this.messaging, this.id, handler);
l.register()
return l;
}
async disconnect(): Promise<void> {
await this.messaging.exchange<PrivateChannelDisconnectResponse>({
meta: this.messaging.createMeta(),
payload: {
channelId: this.id,
},
type: "privateChannelDisconnectRequest"
} as PrivateChannelDisconnectRequest, 'privateChannelDisconnectResponse')
}
async addContextListenerInner(contextType: string | null, theHandler: ContextHandler): Promise<Listener> {
const listener = new DefaultContextListener(this.messaging, this.id, contextType, theHandler);
await listener.register()
return listener
}
}