-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathMessagePortMessaging.ts
76 lines (63 loc) · 2.26 KB
/
MessagePortMessaging.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
import { AbstractWebMessaging } from './AbstractWebMessaging'
import { RegisterableListener } from "@kite9/fdc3-agent-proxy"
import { GetAgentParams } from "@kite9/fdc3-standard"
import { v4 as uuidv4 } from "uuid"
import { BrowserTypes } from "@kite9/fdc3-schema";
import { AddContextListenerRequestMeta } from '@kite9/fdc3-schema/generated/api/BrowserTypes';
type WebConnectionProtocol3Handshake = BrowserTypes.WebConnectionProtocol3Handshake
/**
* Details needed to set up the Messaging instance
*/
export type ConnectionDetails = {
connectionAttemptUuid: string
handshake: WebConnectionProtocol3Handshake,
messagePort: MessagePort,
actualUrl: string,
options: GetAgentParams
}
export class MessagePortMessaging extends AbstractWebMessaging {
private readonly cd: ConnectionDetails
private readonly listeners: Map<string, RegisterableListener> = new Map()
constructor(cd: ConnectionDetails, deliveryTimeoutMs?: number) {
super(cd.options, cd.connectionAttemptUuid, cd.actualUrl, deliveryTimeoutMs)
this.cd = cd;
this.cd.messagePort.onmessage = (m) => {
this.listeners.forEach((v, _k) => {
if (v.filter(m.data)) {
v.action(m.data)
}
})
}
}
createUUID(): string {
return uuidv4();
}
post(message: object): Promise<void> {
this.cd.messagePort.postMessage(message);
return Promise.resolve();
}
register(l: RegisterableListener): void {
this.listeners.set(l.id!!, l)
}
unregister(id: string): void {
this.listeners.delete(id)
}
createMeta(): AddContextListenerRequestMeta {
return {
"requestUuid": this.createUUID(),
"timestamp": new Date(),
"source": this.getSource()
}
}
waitFor<X>(filter: (m: any) => boolean, timeoutErrorMessage?: string): Promise<X> {
// console.log("Waiting for", filter, timeoutErrorMessage)
return super.waitFor(filter, timeoutErrorMessage).then((v: any) => {
// console.log("Wait over ", v, timeoutErrorMessage)
return v;
})
}
async disconnect(): Promise<void> {
await super.disconnect()
this.cd.messagePort.close()
}
}