Skip to content

Commit

Permalink
feat: add biome (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak authored Aug 26, 2024
1 parent 90a4f3d commit 5b6ebc0
Show file tree
Hide file tree
Showing 68 changed files with 3,581 additions and 3,373 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Code quality
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
quality:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Biome
uses: biomejs/setup-biome@v2
with:
version: latest
- name: Run Biome
run: biome ci .
15 changes: 15 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["**/dist/*", "**/docs/*", "**/*_pb.ts", "**/bundle/*"]
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"organizeImports": {
"enabled": true
}
}
60 changes: 30 additions & 30 deletions examples/canvas/package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"name": "ts-topology-examples-canvas",
"version": "0.0.23-5",
"license": "MIT",
"scripts": {
"build": "vite build",
"clean": "rm -rf dist/ node_modules/",
"dev": "vite serve",
"start": "ts-node ./src/index.ts"
},
"dependencies": {
"@topology-foundation/crdt": "0.0.23-5",
"@topology-foundation/network": "0.0.23-5",
"@topology-foundation/node": "0.0.23-5",
"@topology-foundation/object": "0.0.23-5",
"crypto-browserify": "^3.12.0",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"ts-node": "^10.9.2",
"vm-browserify": "^1.1.2"
},
"devDependencies": {
"@types/node": "^22.4.1",
"ts-loader": "^9.3.1",
"typescript": "^5.5.4",
"vite": "^5.4.1",
"vite-plugin-node-polyfills": "^0.22.0",
"webpack": "^5.74.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
"name": "ts-topology-examples-canvas",
"version": "0.0.23-5",
"license": "MIT",
"scripts": {
"build": "vite build",
"clean": "rm -rf dist/ node_modules/",
"dev": "vite serve",
"start": "ts-node ./src/index.ts"
},
"dependencies": {
"@topology-foundation/crdt": "0.0.23-5",
"@topology-foundation/network": "0.0.23-5",
"@topology-foundation/node": "0.0.23-5",
"@topology-foundation/object": "0.0.23-5",
"crypto-browserify": "^3.12.0",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"ts-node": "^10.9.2",
"vm-browserify": "^1.1.2"
},
"devDependencies": {
"@types/node": "^22.4.1",
"ts-loader": "^9.3.1",
"typescript": "^5.5.4",
"vite": "^5.4.1",
"vite-plugin-node-polyfills": "^0.22.0",
"webpack": "^5.74.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
40 changes: 19 additions & 21 deletions examples/canvas/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { TopologyObject_Operation } from "@topology-foundation/object";
import { Canvas } from "./objects/canvas";
import type { TopologyObject_Operation } from "@topology-foundation/object";
import type { Canvas } from "./objects/canvas";

export function handleObjectOps(
canvas: Canvas,
ops: TopologyObject_Operation[],
canvas: Canvas,
ops: TopologyObject_Operation[],
) {
// In this case we only have paint
// `paint(${node.getPeerId()}, [${[x, y]}], [${painting}])`
try {
for (const op of ops) {
const offset = op.args[1]
.split(",")
.map((s: string) => parseInt(s, 10)) as [number, number];
const rgb = op.args[2].split(",").map((s: string) => parseInt(s, 10)) as [
number,
number,
number,
];
canvas.paint(op.args[0], offset, rgb);
}
} catch (e) {
console.error(e);
}
// In this case we only have paint
// `paint(${node.getPeerId()}, [${[x, y]}], [${painting}])`
try {
for (const op of ops) {
const offset = op.args[1]
.split(",")
.map((s: string) => Number.parseInt(s, 10)) as [number, number];
const rgb = op.args[2]
.split(",")
.map((s: string) => Number.parseInt(s, 10)) as [number, number, number];
canvas.paint(op.args[0], offset, rgb);
}
} catch (e) {
console.error(e);
}
}
232 changes: 116 additions & 116 deletions examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GCounter } from "@topology-foundation/crdt";
import { TopologyNode } from "@topology-foundation/node";
import type { TopologyObject } from "@topology-foundation/object";
import { handleObjectOps } from "./handlers";
import { Canvas } from "./objects/canvas";
import { Pixel } from "./objects/pixel";
import { GCounter } from "@topology-foundation/crdt";
import { handleObjectOps } from "./handlers";
import { TopologyObject } from "@topology-foundation/object";

const node = new TopologyNode();
let canvasCRO: Canvas;
Expand All @@ -13,127 +13,127 @@ let discoveryPeers: string[] = [];
let objectPeers: string[] = [];

const render = () => {
const peers_element = <HTMLDivElement>document.getElementById("peers");
peers_element.innerHTML = "[" + peers.join(", ") + "]";

const discovery_element = <HTMLDivElement>(
document.getElementById("discovery_peers")
);
discovery_element.innerHTML = "[" + discoveryPeers.join(", ") + "]";

const object_element = <HTMLDivElement>(
document.getElementById("object_peers")
);
object_element.innerHTML = "[" + objectPeers.join(", ") + "]";

if (!canvasCRO) return;
const canvas = canvasCRO.canvas;
const canvas_element = <HTMLDivElement>document.getElementById("canvas");
canvas_element.innerHTML = "";
canvas_element.style.display = "inline-grid";

canvas_element.style.gridTemplateColumns = Array(canvas.length)
.fill("1fr")
.join(" ");

for (let x = 0; x < canvas.length; x++) {
for (let y = 0; y < canvas[x].length; y++) {
let pixel = document.createElement("div");
pixel.id = `${x}-${y}`;
pixel.style.width = "25px";
pixel.style.height = "25px";
pixel.style.backgroundColor = `rgb(${canvas[x][y].color()[0]}, ${canvas[x][y].color()[1]}, ${canvas[x][y].color()[2]})`;
pixel.style.cursor = "pointer";
pixel.addEventListener("click", () => paint_pixel(pixel));
canvas_element.appendChild(pixel);
}
}
const peers_element = <HTMLDivElement>document.getElementById("peers");
peers_element.innerHTML = `[${peers.join(", ")}]`;

const discovery_element = <HTMLDivElement>(
document.getElementById("discovery_peers")
);
discovery_element.innerHTML = `[${discoveryPeers.join(", ")}]`;

const object_element = <HTMLDivElement>(
document.getElementById("object_peers")
);
object_element.innerHTML = `[${objectPeers.join(", ")}]`;

if (!canvasCRO) return;
const canvas = canvasCRO.canvas;
const canvas_element = <HTMLDivElement>document.getElementById("canvas");
canvas_element.innerHTML = "";
canvas_element.style.display = "inline-grid";

canvas_element.style.gridTemplateColumns = Array(canvas.length)
.fill("1fr")
.join(" ");

for (let x = 0; x < canvas.length; x++) {
for (let y = 0; y < canvas[x].length; y++) {
const pixel = document.createElement("div");
pixel.id = `${x}-${y}`;
pixel.style.width = "25px";
pixel.style.height = "25px";
pixel.style.backgroundColor = `rgb(${canvas[x][y].color()[0]}, ${canvas[x][y].color()[1]}, ${canvas[x][y].color()[2]})`;
pixel.style.cursor = "pointer";
pixel.addEventListener("click", () => paint_pixel(pixel));
canvas_element.appendChild(pixel);
}
}
};

const random_int = (max: number) => Math.floor(Math.random() * max);

async function paint_pixel(pixel: HTMLDivElement) {
const [x, y] = pixel.id.split("-").map((v) => parseInt(v, 10));
const painting: [number, number, number] = [
random_int(256),
random_int(256),
random_int(256),
];
canvasCRO.paint(node.networkNode.peerId, [x, y], painting);
const [r, g, b] = canvasCRO.pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;

node.updateObject(topologyObject.id, [
{
fn: "paint",
args: [
node.networkNode.peerId,
`${x},${y}`,
`${painting[0]},${painting[1]},${painting[2]}`,
],
},
]);
const [x, y] = pixel.id.split("-").map((v) => Number.parseInt(v, 10));
const painting: [number, number, number] = [
random_int(256),
random_int(256),
random_int(256),
];
canvasCRO.paint(node.networkNode.peerId, [x, y], painting);
const [r, g, b] = canvasCRO.pixel(x, y).color();
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;

node.updateObject(topologyObject.id, [
{
fn: "paint",
args: [
node.networkNode.peerId,
`${x},${y}`,
`${painting[0]},${painting[1]},${painting[2]}`,
],
},
]);
}

async function init() {
await node.start();

node.addCustomGroupMessageHandler("", (e) => {
peers = node.networkNode.getAllPeers();
discoveryPeers = node.networkNode.getGroupPeers("topology::discovery");
render();
});

let create_button = <HTMLButtonElement>document.getElementById("create");
create_button.addEventListener("click", async () => {
canvasCRO = new Canvas(5, 10);
topologyObject = await node.createObject();

// message handler for the CRO
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(canvasCRO, obj.operations);
});

(<HTMLSpanElement>document.getElementById("canvasId")).innerText =
topologyObject.id;
render();
});

let connect_button = <HTMLButtonElement>document.getElementById("connect");
connect_button.addEventListener("click", async () => {
let croId = (<HTMLInputElement>document.getElementById("canvasIdInput"))
.value;
try {
canvasCRO = new Canvas(5, 10);
topologyObject = await node.createObject();

// message handler for the CRO
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
(<HTMLSpanElement>document.getElementById("canvasId")).innerText =
topologyObject.id;
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(canvasCRO, obj.operations);
render();
});

render();
} catch (e) {
console.error("Error while connecting with CRO", croId, e);
}
});
await node.start();

node.addCustomGroupMessageHandler("", (e) => {
peers = node.networkNode.getAllPeers();
discoveryPeers = node.networkNode.getGroupPeers("topology::discovery");
render();
});

const create_button = <HTMLButtonElement>document.getElementById("create");
create_button.addEventListener("click", async () => {
canvasCRO = new Canvas(5, 10);
topologyObject = await node.createObject();

// message handler for the CRO
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(canvasCRO, obj.operations);
});

(<HTMLSpanElement>document.getElementById("canvasId")).innerText =
topologyObject.id;
render();
});

const connect_button = <HTMLButtonElement>document.getElementById("connect");
connect_button.addEventListener("click", async () => {
const croId = (<HTMLInputElement>document.getElementById("canvasIdInput"))
.value;
try {
canvasCRO = new Canvas(5, 10);
topologyObject = await node.createObject();

// message handler for the CRO
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
// on create/connect
if (topologyObject)
objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
(<HTMLSpanElement>document.getElementById("canvasId")).innerText =
topologyObject.id;
render();
});

node.objectStore.subscribe(topologyObject.id, (_, obj) => {
handleObjectOps(canvasCRO, obj.operations);
render();
});

render();
} catch (e) {
console.error("Error while connecting with CRO", croId, e);
}
});
}

init();
Loading

0 comments on commit 5b6ebc0

Please sign in to comment.