Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add realtime API #67

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions backend/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type SendUpdateMessage = {
update: ReceivedStatusUpdate<any>;
};

type SendRealtimeMessage = {
type: "sendRealtime";
data: Uint8Array;
};

type SetUpdateListenerMessage = {
type: "setUpdateListener";
serial: number;
Expand Down Expand Up @@ -125,6 +130,12 @@ export class Instances {
// XXX should validate parsed
if (isSendUpdateMessage(parsed)) {
instance.webXdc.sendUpdate(parsed.update, "");
} else if (isSendRealtimeMessage(parsed)) {
instance.webXdc.sendRealtimeData(parsed.data);
} else if (isSetRealtimeListenerMessage(parsed)) {
instance.webXdc.connectRealtime((data) => {
return broadcast(wss, JSON.stringify({ type: "realtime", data }));
});
} else if (isSetUpdateListenerMessage(parsed)) {
instance.webXdc.connect(
(updates) => {
Expand Down Expand Up @@ -216,6 +227,16 @@ function isSendUpdateMessage(value: any): value is SendUpdateMessage {
return value.type === "sendUpdate";
}

function isSendRealtimeMessage(value: any): value is SendRealtimeMessage {
return value.type === "sendRealtime";
}

function isSetRealtimeListenerMessage(
value: any,
): value is { type: "setRealtimeListener" } {
return value.type === "setRealtimeListener";
}

function isSetUpdateListenerMessage(
value: any,
): value is SetUpdateListenerMessage {
Expand Down
27 changes: 27 additions & 0 deletions backend/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ test("distribute to self", () => {
]);
});

test("Send realtime", () => {
const [getMessages, onMessage] = track();
const processor = createProcessor(onMessage);
const client0 = processor.createClient("3001");
const client1 = processor.createClient("3002");

const client0Heard: string[] = [];
const client1Heard: string[] = [];

const decoder = new TextDecoder();
client0.connectRealtime((data) => {
client0Heard.push(decoder.decode(data));
return true;
});
client1.connectRealtime((data) => {
client1Heard.push(decoder.decode(data));
return true;
});

const encoder = new TextEncoder();

client0.sendRealtimeData(new Uint8Array(encoder.encode("hi")));

expect(client1Heard).toMatchObject(["hi"]);
expect(client0Heard).toMatchObject([]);
});

test("distribute to self and other", () => {
const [getMessages, onMessage] = track();
const processor = createProcessor(onMessage);
Expand Down
58 changes: 57 additions & 1 deletion backend/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
import {
RealtimeListener as WebxdcRealtimeListener,
ReceivedStatusUpdate,
SendingStatusUpdate,
Webxdc,
Expand All @@ -10,6 +11,7 @@ type UpdateListenerMulti = (updates: ReceivedStatusUpdate<any>[]) => boolean;

type ClearListener = () => boolean;
type DeleteListener = () => boolean;
type RTListener = (data: Uint8Array) => boolean;

type Connect = (
updateListener: UpdateListenerMulti,
Expand All @@ -20,7 +22,9 @@ type Connect = (

export type WebXdcMulti = {
connect: Connect;
connectRealtime: (listener: RTListener) => void;
sendUpdate: Webxdc<any>["sendUpdate"];
sendRealtimeData: (data: Uint8Array) => void;
};

export type OnMessage = (message: Message) => void;
Expand All @@ -33,6 +37,7 @@ export interface IProcessor {

class Client implements WebXdcMulti {
updateListener: UpdateListenerMulti | null = null;
realtimeListener: RTListener | null = null;
clearListener: ClearListener | null = null;
updateSerial: number | null = null;
deleteListener: DeleteListener | null = null;
Expand All @@ -46,6 +51,35 @@ class Client implements WebXdcMulti {
this.processor.distribute(this.id, update);
}

sendRealtimeData(data: Uint8Array) {
this.processor.distributeRealtime(this.id, data);
}

connectRealtime(listener: RTListener) {
this.processor.onMessage({
type: "connect-realtime",
instanceId: this.id,
instanceColor: getColorForId(this.id),
timestamp: Date.now(),
});

const realtimeListener = (data: Uint8Array) => {
const hasReceived = listener(data);
if (hasReceived) {
this.processor.onMessage({
type: "realtime-received",
data,
instanceId: this.id,
instanceColor: getColorForId(this.id),
timestamp: Date.now(),
});
}
return hasReceived;
};

this.realtimeListener = realtimeListener;
}

connect(
listener: UpdateListenerMulti,
serial: number,
Expand Down Expand Up @@ -108,6 +142,13 @@ class Client implements WebXdcMulti {
this.updateListener([update]);
}

receiveRealtime(data: Uint8Array) {
if (this.realtimeListener == null) {
return;
}
this.realtimeListener(data);
}

clear() {
if (
this.clearListener == null ||
Expand Down Expand Up @@ -148,6 +189,21 @@ class Processor implements IProcessor {
this.clients.splice(client_index, 1);
}

distributeRealtime(instanceId: string, data: Uint8Array) {
this.onMessage({
type: "realtime-sent",
instanceId: instanceId,
instanceColor: getColorForId(instanceId),
data,
timestamp: Date.now(),
});
for (const client of this.clients) {
if (client.id != instanceId) {
client.receiveRealtime(data);
}
}
}

distribute(instanceId: string, update: SendingStatusUpdate<any>) {
this.currentSerial++;
const receivedUpdate: ReceivedStatusUpdate<any> = {
Expand Down
Loading
Loading