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(object): drpobject become generic #565

Open
wants to merge 1 commit 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
13 changes: 5 additions & 8 deletions examples/canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { DRP_DISCOVERY_TOPIC } from "@ts-drp/types";
import { Canvas } from "./objects/canvas";

const node = new DRPNode();
let drpObject: DRPObject;
let canvasDRP: Canvas;
let drpObject: DRPObject<Canvas>;
let peers: string[] = [];
let discoveryPeers: string[] = [];
let objectPeers: string[] = [];
Expand All @@ -22,8 +21,8 @@ const render = (): void => {
object_element.innerHTML = `[${objectPeers.join(", ")}]`;
(<HTMLSpanElement>document.getElementById("canvasId")).innerText = drpObject?.id;

if (!canvasDRP) return;
const canvas = canvasDRP.canvas;
if (!drpObject.drp) return;
const canvas = drpObject.drp.canvas;
for (let x = 0; x < canvas.length; x++) {
for (let y = 0; y < canvas[x].length; y++) {
const pixel = document.getElementById(`${x}-${y}`);
Expand All @@ -38,8 +37,8 @@ const random_int = (max: number): number => Math.floor(Math.random() * max);
function paint_pixel(pixel: HTMLDivElement): void {
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)];
canvasDRP.paint([x, y], painting);
const [r, g, b] = canvasDRP.query_pixel(x, y).color();
drpObject.drp?.paint([x, y], painting);
const [r, g, b] = drpObject.drp?.query_pixel(x, y).color() ?? [0, 0, 0];
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}

Expand Down Expand Up @@ -85,7 +84,6 @@ async function init(): Promise<void> {
const create_button = <HTMLButtonElement>document.getElementById("create");
const create = async (): Promise<void> => {
drpObject = await node.createObject({ drp: new Canvas(5, 10) });
canvasDRP = drpObject.drp as Canvas;

createConnectHandlers();
render();
Expand All @@ -101,7 +99,6 @@ async function init(): Promise<void> {
id: drpId,
drp: new Canvas(5, 10),
});
canvasDRP = drpObject.drp as Canvas;

createConnectHandlers();
render();
Expand Down
6 changes: 5 additions & 1 deletion examples/canvas/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
},
"exclude": [
"node_modules",
"dist"
]
}
13 changes: 5 additions & 8 deletions examples/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { DRP_DISCOVERY_TOPIC } from "@ts-drp/types";
import { Chat } from "./objects/chat";

const node = new DRPNode();
let drpObject: DRPObject;
let chatDRP: Chat;
let drpObject: DRPObject<Chat>;
let peers: string[] = [];
let discoveryPeers: string[] = [];
let objectPeers: string[] = [];
Expand All @@ -25,8 +24,8 @@ const render = (): void => {
const element_objectPeers = <HTMLDivElement>document.getElementById("objectPeers");
element_objectPeers.innerHTML = `[${objectPeers.join(", ")}]`;

if (!chatDRP) return;
const chat = chatDRP.query_messages();
if (!drpObject.drp) return;
const chat = drpObject.drp.query_messages();
const element_chat = <HTMLDivElement>document.getElementById("chat");
element_chat.innerHTML = "";

Expand All @@ -47,13 +46,13 @@ const render = (): void => {

function sendMessage(message: string): void {
const timestamp: string = Date.now().toString();
if (!chatDRP) {
if (!drpObject.drp) {
console.error("Chat DRP not initialized");
alert("Please create or join a chat room first");
return;
}

chatDRP.addMessage(timestamp, message, node.networkNode.peerId);
drpObject.drp.addMessage(timestamp, message, node.networkNode.peerId);
render();
}

Expand Down Expand Up @@ -86,7 +85,6 @@ async function main(): Promise<void> {

const create = async (): Promise<void> => {
drpObject = await node.createObject({ drp: new Chat() });
chatDRP = drpObject.drp as Chat;
createConnectHandlers();
render();
};
Expand All @@ -101,7 +99,6 @@ async function main(): Promise<void> {
}

drpObject = await node.createObject({ id: objectId, drp: new Chat() });
chatDRP = drpObject.drp as Chat;
createConnectHandlers();
render();
};
Expand Down
6 changes: 5 additions & 1 deletion examples/chat/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
},
"exclude": [
"node_modules",
"dist"
]
}
10 changes: 4 additions & 6 deletions examples/grid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ export function getNetworkConfigFromEnv(): DRPNodeConfig {
}

function addUser(): void {
if (!gridState.gridDRP) {
if (!gridState.drpObject?.drp) {
console.error("Grid DRP not initialized");
alert("Please create or join a grid first");
return;
}

gridState.gridDRP.addUser(
gridState.drpObject.drp.addUser(
gridState.node.networkNode.peerId,
getColorForPeerId(gridState.node.networkNode.peerId)
);
render();
}

function moveUser(direction: string): void {
if (!gridState.gridDRP) {
if (!gridState.drpObject?.drp) {
console.error("Grid DRP not initialized");
alert("Please create or join a grid first");
return;
}

gridState.gridDRP?.moveUser(gridState.node.networkNode.peerId, direction);
gridState.drpObject.drp.moveUser(gridState.node.networkNode.peerId, direction);
render();
}

Expand Down Expand Up @@ -96,7 +96,6 @@ function run(metrics?: IMetrics): void {
drp: new Grid(),
metrics,
});
gridState.gridDRP = gridState.drpObject.drp as Grid;
createConnectHandlers();
addUser();
render();
Expand All @@ -120,7 +119,6 @@ function run(metrics?: IMetrics): void {
drp: new Grid(),
metrics,
});
gridState.gridDRP = gridState.drpObject.drp as Grid;
createConnectHandlers();
addUser();
render();
Expand Down
4 changes: 2 additions & 2 deletions examples/grid/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const render = (): void => {
}

if (!gridState.drpObject) return;
const users = gridState.gridDRP?.query_users();
const users = gridState.drpObject.drp?.query_users();
const element_grid = <HTMLDivElement>document.getElementById("grid");
element_grid.innerHTML = "";

Expand Down Expand Up @@ -169,7 +169,7 @@ export const render = (): void => {
if (!users) return;
for (const userColorString of users) {
const [id, color] = userColorString.split(":");
const position = gridState.gridDRP?.query_userPosition(userColorString);
const position = gridState.drpObject.drp?.query_userPosition(userColorString);

if (position) {
const div = document.createElement("div");
Expand Down
4 changes: 1 addition & 3 deletions examples/grid/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { type Grid } from "./objects/grid";

interface GridState {
node: DRPNode;
drpObject: IDRPObject | undefined;
gridDRP: Grid | undefined;
drpObject: IDRPObject<Grid> | undefined;
peers: string[];
discoveryPeers: string[];
objectPeers: string[];
Expand All @@ -15,7 +14,6 @@ interface GridState {
export const gridState: GridState = {
node: new DRPNode(),
drpObject: undefined,
gridDRP: undefined,
peers: [],
discoveryPeers: [],
objectPeers: [],
Expand Down
6 changes: 5 additions & 1 deletion examples/grid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
},
"exclude": [
"node_modules",
"dist"
]
}
Loading