-
Notifications
You must be signed in to change notification settings - Fork 132
/
AbstractWebMessaging.ts
65 lines (52 loc) · 2.48 KB
/
AbstractWebMessaging.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
import { DesktopAgentDetails, WebDesktopAgentType, GetAgentParams, } from "@kite9/fdc3-standard";
import { RegisterableListener, AbstractMessaging } from "@kite9/fdc3-agent-proxy";
import { BrowserTypes } from "@kite9/fdc3-schema";
import { AddContextListenerRequestMeta } from "@kite9/fdc3-schema/generated/api/BrowserTypes";
type WebConnectionProtocol5ValidateAppIdentitySuccessResponse = BrowserTypes.WebConnectionProtocol5ValidateAppIdentitySuccessResponse
export const DESKTOP_AGENT_SESSION_STORAGE_DETAILS_KEY = "fdc3-desktop-agent-details"
/**
* Version of Messaging which is able to store details in the SessionState (i.e. works on the web)
*/
export abstract class AbstractWebMessaging extends AbstractMessaging {
constructor(options: GetAgentParams, connectionAttemptUuid: string, actualUrl: string, timeout?: number) {
super(options, connectionAttemptUuid, actualUrl, timeout)
}
abstract createUUID(): string
abstract post(message: object): Promise<void>
abstract register(l: RegisterableListener): void
abstract unregister(id: string): void
abstract createMeta(): AddContextListenerRequestMeta
/**
* Note that we also key by the window name as well, in case multiple iframes are using the same session storage.
*/
private sessionKey(): string {
const windowName = globalThis.window.name
const keyName = windowName ? DESKTOP_AGENT_SESSION_STORAGE_DETAILS_KEY + "-" + windowName : DESKTOP_AGENT_SESSION_STORAGE_DETAILS_KEY
return keyName
}
/**
* Used to allow session-reconnection
*/
storeInstanceUuid(vr: WebConnectionProtocol5ValidateAppIdentitySuccessResponse) {
const details: DesktopAgentDetails = {
agentType: WebDesktopAgentType.ProxyParent,
instanceUuid: vr.payload.instanceUuid,
appId: vr.payload.appId,
instanceId: vr.payload.instanceId,
}
globalThis.sessionStorage.setItem(this.sessionKey(), JSON.stringify(details))
}
/**
* Stores the instanceUuid in session storage in case session needs reconnecting.
*/
retrieveInstanceUuid(): string | undefined {
const detailsStr = globalThis.sessionStorage.getItem(this.sessionKey())
if (detailsStr) {
const details = JSON.parse(detailsStr) as DesktopAgentDetails
if (details.agentType == WebDesktopAgentType.ProxyParent) {
return details.instanceUuid
}
}
return undefined;
}
}