/
BroadcastChannelBridge.ts
197 lines (176 loc) · 5.65 KB
/
BroadcastChannelBridge.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { BroadcasterBridge } from "./Bridge";
import { BroadcasterContentTypeMismatchError, BroadcasterError } from "../utils/Errors";
import { BroadcasterMessage, BroadcasterStateMessage } from "../types";
import { transformIntoBroadcasterError } from "../utils/transformIntoBroadcasterError";
enum MessageTypes {
MESSAGE,
STATE,
}
type SerializedMessage<M, T> = {
payload: M;
type: T;
};
type SerializedPublicMessage<M> = SerializedMessage<M, MessageTypes.MESSAGE>
type SerializedPrivateMessage<M> = SerializedMessage<M, MessageTypes.STATE>;
/**
* **BroadcastChannel Bridge: Serverless browsing context bridge**
*
* Communicates with other Broadcaster instances via
* [BroadcastChannel API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API).
*
* BroadcastChannel API allows to communicate with different browsing contexts inside a browser
* (tabs, window, workers, etc..) without need of a server.
*
* ____
*
* Minimal requirements:
*
* | Chromium | Edge | Safari | Firefox |
* |:--------:|:----:|:------:|:-------:|
* | 54 | 79 | 15.4 | 38 |
*/
export class BroadcastChannelBridge<
Payload,
Metadata,
> extends BroadcasterBridge<Payload, Metadata> {
/**
* Instance of BroadcastChannel API.
*/
private messageChannel!: BroadcastChannel;
/**
* Channel used as identifier for communication
*/
private channelName!: string;
/**
* Connects to a BroadcasterChannel stream
*
* @param channelName communication identifier
*/
public connect(channelName: string): void {
this.channelName = channelName;
this.messageChannel = new BroadcastChannel(this.channelName);
// subscribe to BroadcastChannel API
this.messageChannel.addEventListener(
"message",
this.extractMessageAndPush,
);
this.messageChannel.addEventListener(
"messageerror",
(event) => {
this.pushErrorMessage(
new BroadcasterError(
"BROADCAST_CHANNEL_DESERIALIZE_ERROR",
`Broadcast channel cannot deserialize incoming message: \n
Data Type: ${typeof event.data}
`
),
);
}
);
}
protected disconnect(): void {
// Unsubscribe from BroadcasterChannel API
this.messageChannel.removeEventListener("message", this.extractMessageAndPush);
this.messageChannel.close();
}
/**
* Sort incoming messages and push it to corresponding stream
*
* @param event BroadcasterChannel Event
*/
private extractMessageAndPush = (
event: MessageEvent<
SerializedPublicMessage<BroadcasterMessage<Payload>> | SerializedPrivateMessage<BroadcasterStateMessage<Metadata>>
>
): void => {
const data = event.data;
if (this.isPublicMessage(data)) {
this.pushMessage(data.payload);
}
else if (this.isStateChangeMessage(data)) {
this.pushState(data.payload);
}
else {
this.pushErrorMessage(new BroadcasterContentTypeMismatchError(data));
}
};
/**
* Wraps action to try catch, and in case of an error,
* it will transform it to BroadcastError and push it to subscribers
*
* @param action callback action, which will be called inside try scope
*/
private guardPostMessageErrors = (
action: () => void,
): void => {
try {
action();
}
catch (error) {
this.pushErrorMessage(transformIntoBroadcasterError("BROADCAST_CHANNEL_POST_MESSAGE_ERROR", error));
}
};
/**
* Incoming message is a valid BroadcastChannelBridge message
*
* @param data
* @returns
*/
private isMessage(data: unknown): data is SerializedMessage<unknown, unknown> {
// data is an object
if (
typeof data === "object" &&
!Array.isArray(data) &&
data !== null
) {
// assert message is a valid SerializedMessage
const message = data as SerializedMessage<unknown, unknown>;
return (
message.type !== undefined
);
}
return false;
}
/**
* A BroadcasterChannel message is a public message
*
* @param data
* @returns
*/
private isPublicMessage(data: unknown): data is SerializedPublicMessage<Payload> {
return this.isMessage(data) && data.type === MessageTypes.MESSAGE;
}
/**
* A BroadcasterChannel message is a state change message
*
* @param data
* @returns
*/
private isStateChangeMessage(data: unknown): data is SerializedPrivateMessage<Metadata> {
return this.isMessage(data) && data.type === MessageTypes.STATE;
}
/**
* Sends public message to all BroadcasterChannelBridge instances
*
* @param payload
*/
public postMessage(payload: Payload): void {
const message:SerializedPublicMessage<Payload> = {
payload,
type: MessageTypes.MESSAGE
};
this.guardPostMessageErrors(() => this.messageChannel.postMessage(message));
}
/**
* Sends new state of this instance to all BroadcasterChannelBridge instances
*
* @param payload
*/
public setState(payload: Metadata): void {
const message:SerializedPrivateMessage<Metadata> = {
payload,
type: MessageTypes.STATE
};
this.guardPostMessageErrors(() => this.messageChannel.postMessage(message));
}
}