-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathDefaultChannel.ts
101 lines (92 loc) · 3.21 KB
/
DefaultChannel.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
99
100
101
import { ContextHandler, DisplayMetadata, Listener, Channel } from '@finos/fdc3-standard';
import { Context } from '@finos/fdc3-context';
import { Messaging } from '../Messaging';
import { DefaultContextListener } from '../listeners/DefaultContextListener';
import {
BroadcastRequest,
BroadcastResponse,
GetCurrentContextRequest,
GetCurrentContextResponse,
} from '@finos/fdc3-schema/generated/api/BrowserTypes';
export class DefaultChannel implements Channel {
protected readonly messaging: Messaging;
protected readonly messageExchangeTimeout;
readonly id: string;
readonly type: 'user' | 'app' | 'private';
readonly displayMetadata?: DisplayMetadata | undefined;
constructor(
messaging: Messaging,
messageExchangeTimeout: number,
id: string,
type: 'user' | 'app' | 'private',
displayMetadata?: DisplayMetadata
) {
this.messaging = messaging;
this.messageExchangeTimeout = messageExchangeTimeout;
this.id = id;
this.type = type;
this.displayMetadata = displayMetadata;
}
async broadcast(context: Context): Promise<void> {
const request: BroadcastRequest = {
meta: this.messaging.createMeta(),
payload: {
channelId: this.id,
context,
},
type: 'broadcastRequest',
};
await this.messaging.exchange<BroadcastResponse>(request, 'broadcastResponse', this.messageExchangeTimeout);
}
async getCurrentContext(contextType?: string | undefined): Promise<Context | null> {
// first, ensure channel state is up-to-date
const request: GetCurrentContextRequest = {
meta: this.messaging.createMeta(),
payload: {
channelId: this.id,
contextType: contextType ?? null,
},
type: 'getCurrentContextRequest',
};
const response = await this.messaging.exchange<GetCurrentContextResponse>(
request,
'getCurrentContextResponse',
this.messageExchangeTimeout
);
return response.payload.context ?? null;
}
async addContextListener(
contextTypeOrHandler: string | null | ContextHandler,
handler?: ContextHandler
): Promise<Listener> {
let theContextType: string | null;
let theHandler: ContextHandler;
if (contextTypeOrHandler == null && typeof handler === 'function') {
theContextType = null;
theHandler = handler;
} else if (typeof contextTypeOrHandler === 'string' && typeof handler === 'function') {
theContextType = contextTypeOrHandler;
theHandler = handler;
} else if (typeof contextTypeOrHandler === 'function') {
// deprecated one-arg version
theContextType = null;
theHandler = contextTypeOrHandler as ContextHandler;
} else {
//invalid call
// TODO: Replace with Standardized error when #1490 is resolved
throw new Error('Invalid arguments passed to addContextListener!');
}
return await this.addContextListenerInner(theContextType, theHandler);
}
async addContextListenerInner(contextType: string | null, theHandler: ContextHandler): Promise<Listener> {
const listener = new DefaultContextListener(
this.messaging,
this.messageExchangeTimeout,
this.id,
contextType,
theHandler
);
await listener.register();
return listener;
}
}