You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
50 lines
1.1 KiB
import { randomUUID } from "crypto";
|
|
import * as shapeManager from "./shapeManager";
|
|
import type { WasmConnection, Diff } from "./types";
|
|
import type { Shape } from "../js-land/types";
|
|
|
|
const mockShapeObject1 = {
|
|
type: "Person",
|
|
name: "Bob",
|
|
address: {
|
|
street: "First street",
|
|
houseNumber: "15",
|
|
},
|
|
hasChildren: true,
|
|
numberOfHouses: 0,
|
|
};
|
|
const mockShapeObject2 = {
|
|
type: "Cat",
|
|
name: "Niko's cat",
|
|
age: 12,
|
|
numberOfHomes: 3,
|
|
address: {
|
|
street: "Niko's street",
|
|
compartment: 2,
|
|
},
|
|
};
|
|
|
|
export default async function handleShapeRequest(
|
|
shape: Shape,
|
|
callback: (diff: Diff, connectionId: WasmConnection["id"]) => void
|
|
): Promise<{
|
|
connectionId: string;
|
|
shapeObject: object;
|
|
}> {
|
|
const connection: WasmConnection = {
|
|
id: randomUUID(),
|
|
shape,
|
|
// Create a deep copy to prevent accidental by-reference changes.
|
|
state: JSON.parse(
|
|
JSON.stringify(shape === "Shape1" ? mockShapeObject1 : mockShapeObject2)
|
|
),
|
|
callback,
|
|
};
|
|
|
|
shapeManager.connections.set(connection.id, connection);
|
|
|
|
return {
|
|
connectionId: connection.id,
|
|
shapeObject: connection.state,
|
|
};
|
|
}
|
|
|