Skip to content

Commit df28f5a

Browse files
committed
add realtime API support
1 parent 3c8a002 commit df28f5a

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

backend/message.ts

+52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
RealtimeListener,
23
ReceivedStatusUpdate,
34
SendingStatusUpdate,
45
Webxdc,
@@ -23,20 +24,28 @@ type Connect = (
2324
export type WebXdcMulti = {
2425
connect: Connect;
2526
sendUpdate: Webxdc<any>["sendUpdate"];
27+
joinRealtimeChannel: Webxdc<any>["joinRealtimeChannel"]
2628
};
2729

2830
export type UpdateDescr = [ReceivedStatusUpdate<any>, string];
2931

3032
export type OnMessage = (message: Message) => void;
3133

34+
export type OnRealtime = (message: Message) => void;
35+
3236
export interface IProcessor {
3337
createClient(id: string): WebXdcMulti;
3438
clear(): void;
3539
removeClient(id: string): void;
3640
}
3741

42+
class Realtime {
43+
constructor(public listener: (data: Uint8Array) => void = () => {}) {};
44+
}
45+
3846
class Client implements WebXdcMulti {
3947
updateListener: UpdateListenerMulti | null = null;
48+
realtime: Realtime | null = null;
4049
clearListener: ClearListener | null = null;
4150
updateSerial: number | null = null;
4251
deleteListener: DeleteListener | null = null;
@@ -50,6 +59,10 @@ class Client implements WebXdcMulti {
5059
this.processor.distribute(this.id, update, descr);
5160
}
5261

62+
sendRealtimeData(data: Uint8Array): void {
63+
this.processor.distributeRealtime(this.id, data);
64+
}
65+
5366
connect(
5467
listener: UpdateListenerMulti,
5568
serial: number,
@@ -112,7 +125,29 @@ class Client implements WebXdcMulti {
112125
}
113126
this.updateListener([[update, descr]]);
114127
}
128+
129+
receiveRealtime(data: Uint8Array) {
130+
if (this.updateListener == null || this.updateSerial == null) {
131+
return;
132+
}
133+
if (this.realtime && this.realtime.listener)
134+
this.realtime?.listener(data)
135+
}
115136

137+
joinRealtimeChannel(): RealtimeListener {
138+
return {
139+
setListener: (listener) => {
140+
this.realtime = new Realtime(listener)
141+
},
142+
leave: () => {
143+
this.realtime = null
144+
},
145+
send: (data) => {
146+
this.sendRealtimeData(data)
147+
}
148+
}
149+
}
150+
116151
clear() {
117152
if (
118153
this.clearListener == null ||
@@ -153,6 +188,23 @@ class Processor implements IProcessor {
153188
this.clients.splice(client_index, 1);
154189
}
155190

191+
distributeRealtime(
192+
instanceId: string,
193+
data: Uint8Array,
194+
) {
195+
this.onMessage({
196+
type: "realtime-sent",
197+
instanceId: instanceId,
198+
instanceColor: getColorForId(instanceId),
199+
data,
200+
timestamp: Date.now(),
201+
});
202+
for (const client of this.clients) {
203+
client.receiveRealtime(data);
204+
}
205+
}
206+
207+
156208
distribute(
157209
instanceId: string,
158210
update: SendingStatusUpdate<any>,

types/message.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export type UpdateMessage =
1717

1818
export type Message =
1919
| UpdateMessage
20+
| ({ type: "realtime-sent", data: Uint8Array} & InstanceMessage)
2021
| ({ type: "clear" } & InstanceMessage)
2122
| ({ type: "connect" } & InstanceMessage);

0 commit comments

Comments
 (0)