Skip to content

Commit

Permalink
Merge pull request #61 from webxdc/adb/use-webxdc-types-package
Browse files Browse the repository at this point in the history
use webxdc types package
  • Loading branch information
adbenitez authored Nov 16, 2024
2 parents b8976d0 + fd72951 commit 6b422ea
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 115 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ release date when you use `npm version` (see `README.md`).

## [Unreleased]

### Changed

- use `@webxdc/types` instead of own types declaration

## [0.18.0][] - 2024-11-16

### Changed
Expand Down
5 changes: 0 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
port: for instance, if you open an instance again the server doesn't know to
clear it first, because it thinks it already been cleared previously.

## Webxdc development

- Make typescript types for webxdc available as a standalone package so that
developers can use this in their own projects.

## Other

- Update webxdc specification based on what I learned about it.
Expand Down
4 changes: 2 additions & 2 deletions backend/instance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import expressWs from "express-ws";
import { WebSocket, Server } from "ws";

import { JsonValue, ReceivedUpdate } from "../types/webxdc";
import { ReceivedStatusUpdate } from "@webxdc/types";
import { createProcessor, IProcessor, WebXdcMulti, OnMessage } from "./message";
import { Location } from "./location";
import { createPeer, InjectExpress } from "./app";
Expand All @@ -17,7 +17,7 @@ export type Options = {

type SendUpdateMessage = {
type: "sendUpdate";
update: ReceivedUpdate<JsonValue>;
update: ReceivedStatusUpdate<any>;
descr: string;
};

Expand Down
27 changes: 15 additions & 12 deletions backend/message.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type {
Update,
JsonValue,
ReceivedUpdate,
SendUpdate,
} from "../types/webxdc";
ReceivedStatusUpdate,
SendingStatusUpdate,
Webxdc,
} from "@webxdc/types";
import type { Message } from "../types/message";
import { getColorForId } from "./color";

type UpdateListenerMulti = (
updates: [ReceivedUpdate<JsonValue>, string][],
updates: [ReceivedStatusUpdate<any>, string][],
) => boolean;

type ClearListener = () => boolean;
Expand All @@ -23,10 +22,10 @@ type Connect = (

export type WebXdcMulti = {
connect: Connect;
sendUpdate: SendUpdate<JsonValue>;
sendUpdate: Webxdc<any>["sendUpdate"];
};

export type UpdateDescr = [ReceivedUpdate<JsonValue>, string];
export type UpdateDescr = [ReceivedStatusUpdate<any>, string];

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

Expand All @@ -47,7 +46,7 @@ class Client implements WebXdcMulti {
public id: string,
) {}

sendUpdate(update: Update<JsonValue>, descr: string): void {
sendUpdate(update: SendingStatusUpdate<any>, descr: string): void {
this.processor.distribute(this.id, update, descr);
}

Expand Down Expand Up @@ -103,7 +102,7 @@ class Client implements WebXdcMulti {
this.clear();
}

receiveUpdate(update: ReceivedUpdate<JsonValue>, descr: string) {
receiveUpdate(update: ReceivedStatusUpdate<any>, descr: string) {
if (this.updateListener == null || this.updateSerial == null) {
return;
}
Expand Down Expand Up @@ -154,9 +153,13 @@ class Processor implements IProcessor {
this.clients.splice(client_index, 1);
}

distribute(instanceId: string, update: Update<JsonValue>, descr: string) {
distribute(
instanceId: string,
update: SendingStatusUpdate<any>,
descr: string,
) {
this.currentSerial++;
const receivedUpdate: ReceivedUpdate<JsonValue> = {
const receivedUpdate: ReceivedStatusUpdate<any> = {
...update,
serial: this.currentSerial,
max_serial: this.updates.length + 1,
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/node": "^18.0.0",
"@types/node-fetch": "^2.6.2",
"@types/wait-on": "^5.3.1",
"@webxdc/types": "^2.0.0",
"babel-loader": "^8.2.5",
"babel-preset-solid": "~1.5.0",
"concurrently": "^7.2.2",
Expand Down
12 changes: 6 additions & 6 deletions sim/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WebXdc, JsonValue, ReceivedUpdate } from "../types/webxdc";
import { WebXdc, ReceivedStatusUpdate } from "@webxdc/types";

type UpdatesMessage = {
type: "updates";
updates: ReceivedUpdate<JsonValue>[];
updates: ReceivedStatusUpdate<any>[];
};

type ClearMessage = {
Expand Down Expand Up @@ -30,7 +30,7 @@ export type TransportMessageCallback = (message: Message) => void;
export type TransportConnectCallback = () => void;

export type Transport = {
send(data: JsonValue): void;
send(data: any): void;
onMessage(callback: TransportMessageCallback): void;
onConnect(callback: TransportConnectCallback): void;
clear(): void;
Expand All @@ -45,10 +45,10 @@ type Log = (...args: any[]) => void;
export function createWebXdc(
transport: Transport,
log: Log = () => {},
): WebXdc {
): WebXdc<any> {
let resolveUpdateListenerPromise: (() => void) | null = null;

const webXdc: WebXdc = {
const webXdc: WebXdc<any> = {
sendUpdate: (update, descr) => {
transport.send({ type: "sendUpdate", update, descr });
log("send", { update, descr });
Expand Down Expand Up @@ -177,7 +177,7 @@ export function createWebXdc(
...(filters.mimeTypes || []),
].join(",");
element.multiple = filters.multiple || false;
const promise = new Promise((resolve, _reject) => {
const promise: Promise<File[]> = new Promise((resolve, _reject) => {
element.onchange = (_ev) => {
console.log("element.files", element.files);
const files = Array.from(element.files || []);
Expand Down
6 changes: 3 additions & 3 deletions sim/webxdc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonValue, WebXdc } from "../types/webxdc";
import { WebXdc } from "@webxdc/types";
import {
Transport,
TransportMessageCallback,
Expand All @@ -25,7 +25,7 @@ export class DevServerTransport implements Transport {
});
}

send(data: JsonValue): void {
send(data: any): void {
this.socket.send(JSON.stringify(data));
}

Expand Down Expand Up @@ -99,7 +99,7 @@ export class DevServerTransport implements Transport {
}
}

function getWebXdc(): WebXdc {
function getWebXdc(): WebXdc<any> {
return (window as any).webxdc;
}

Expand Down
4 changes: 2 additions & 2 deletions types/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReceivedUpdate, JsonValue } from "./webxdc";
import { ReceivedStatusUpdate } from "@webxdc/types";

export type InstanceMessage = {
instanceId: string;
Expand All @@ -7,7 +7,7 @@ export type InstanceMessage = {
};

export type BaseUpdateMessage = {
update: ReceivedUpdate<JsonValue>;
update: ReceivedStatusUpdate<any>;
descr: string;
} & InstanceMessage;

Expand Down
85 changes: 0 additions & 85 deletions types/webxdc.ts

This file was deleted.

0 comments on commit 6b422ea

Please # to comment.