-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDefaultPrivateChannel.ts
98 lines (91 loc) · 3.56 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
90
91
92
93
94
95
96
97
98
import {
ApiEvent,
ContextHandler,
EventHandler,
Listener,
PrivateChannel,
PrivateChannelEventTypes,
} from '@finos/fdc3-standard';
import { DefaultChannel } from './DefaultChannel';
import { Messaging } from '../Messaging';
import {
PrivateChannelNullEventListener,
PrivateChannelAddContextEventListener,
PrivateChannelDisconnectEventListener,
PrivateChannelUnsubscribeEventListener,
} from '../listeners/PrivateChannelEventListener';
import { DefaultContextListener } from '../listeners/DefaultContextListener';
import { RegisterableListener } from '../listeners/RegisterableListener';
import {
PrivateChannelDisconnectRequest,
PrivateChannelDisconnectResponse,
} from '@finos/fdc3-schema/dist/generated/api/BrowserTypes';
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> {
let a: RegisterableListener;
switch (type) {
case 'addContextListener':
a = new PrivateChannelAddContextEventListener(this.messaging, this.id, handler);
break;
case 'unsubscribe':
a = new PrivateChannelUnsubscribeEventListener(this.messaging, this.id, handler);
break;
case 'disconnect':
a = new PrivateChannelDisconnectEventListener(this.messaging, this.id, handler);
break;
case null:
a = new PrivateChannelNullEventListener(this.messaging, this.id, handler);
break;
default:
throw new Error('Unsupported event type: ' + type);
}
await a.register();
return a;
}
//implementations of the deprecated listener functions
onAddContextListener(handler: (contextType?: string) => void): Listener {
//Adapt handler type for differences between addEventListener and onAddContextListener handler types
const adaptorHandler: EventHandler = (event: ApiEvent) => {
handler(event.details.contextType ?? undefined);
};
const l = new PrivateChannelAddContextEventListener(this.messaging, this.id, adaptorHandler);
l.register();
return l;
}
onUnsubscribe(handler: (contextType?: string) => void): Listener {
//Adapt handler type for differences between addEventListener and onUnsubscribeListener handler types
const adaptorHandler: EventHandler = (event: ApiEvent) => {
handler(event.details.contextType ?? undefined);
};
const l = new PrivateChannelUnsubscribeEventListener(this.messaging, this.id, adaptorHandler);
l.register();
return l;
}
onDisconnect(handler: () => void): Listener {
//Adapt handler type for differences between addEventListener and onDisconnectListener handler types
const adaptorHandler: EventHandler = () => {
handler();
};
const l = new PrivateChannelDisconnectEventListener(this.messaging, this.id, adaptorHandler);
l.register();
return l;
}
async disconnect(): Promise<void> {
const msg: PrivateChannelDisconnectRequest = {
meta: this.messaging.createMeta(),
payload: {
channelId: this.id,
},
type: 'privateChannelDisconnectRequest',
};
await this.messaging.exchange<PrivateChannelDisconnectResponse>(msg, '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;
}
}