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

Realtime Data API #62

Closed
wants to merge 19 commits into from
Closed
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
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
*~
*~
.direnv
2 changes: 1 addition & 1 deletion backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function createFrontend(
app.post<any, { status: string }>("/fake-update", (req, res) => {
const instanceId = Array.from(instances.instances.keys())[0];
const instance = instances.instances.get(instanceId);
instance?.webXdc.sendUpdate({ payload: req.body }, "fake update");
instance?.webXdc.sendUpdate({ payload: req.body }, "");
res.json({
status: "ok",
});
Expand Down
21 changes: 20 additions & 1 deletion backend/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type SendUpdateMessage = {
descr: string;
};

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

type SetUpdateListenerMessage = {
type: "setUpdateListener";
serial: number;
Expand Down Expand Up @@ -125,7 +130,13 @@ export class Instances {
const parsed = JSON.parse(msg);
// XXX should validate parsed
if (isSendUpdateMessage(parsed)) {
instance.webXdc.sendUpdate(parsed.update, parsed.descr);
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: "sendRealtime", data }));
});
} else if (isSetUpdateListenerMessage(parsed)) {
instance.webXdc.connect(
(updates) => {
Expand Down Expand Up @@ -217,6 +228,14 @@ 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
Loading
Loading