forked from finos/FDC3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultChannel.ts
More file actions
131 lines (120 loc) · 4.24 KB
/
DefaultChannel.ts
File metadata and controls
131 lines (120 loc) · 4.24 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ContextHandler, DisplayMetadata, Listener, Channel, EventHandler } from '@finos/fdc3-standard';
import { Context } from '@finos/fdc3-context';
import { Messaging } from '../Messaging';
import { DefaultContextListener } from '../listeners/DefaultContextListener';
import {
BroadcastRequest,
BroadcastResponse,
ClearContextRequest,
ClearContextResponse,
GetCurrentContextRequest,
GetCurrentContextResponse,
} from '@finos/fdc3-schema/generated/api/BrowserTypes';
import { RegisterableListener } from '../listeners/RegisterableListener';
import { EventListener } from '../listeners/EventListener';
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;
}
async clearContext(contextType?: string): Promise<void> {
// first, ensure channel state is up-to-date
const request: ClearContextRequest = {
meta: this.messaging.createMeta(),
payload: {
channelId: this.id,
contextType: contextType ?? null,
},
type: 'clearContextRequest',
};
await this.messaging.exchange<ClearContextResponse>(request, 'clearContextResponse', this.messageExchangeTimeout);
}
async addEventListener(type: string | null, handler: EventHandler): Promise<Listener> {
let listener: RegisterableListener;
switch (type) {
case 'contextCleared':
listener = new EventListener(this.messaging, 'contextCleared', handler);
break;
default:
throw new Error('Unsupported event type: ' + type);
}
await listener.register();
return listener;
}
}