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.
99 lines
2.3 KiB
99 lines
2.3 KiB
import * as shapeManager from "./shapeManager";
|
|
import type { WasmConnection, Diff, Scope } from "./types";
|
|
import type { CompactShapeType } from "@ldo/ldo";
|
|
import type { LdoCompactBase } from "@ldo/ldo";
|
|
import type { Person } from "src/shapes/ldo/personShape.typings";
|
|
import type { Cat } from "src/shapes/ldo/catShape.typings";
|
|
import type { TestObject } from "src/shapes/ldo/testShape.typings";
|
|
|
|
export const mockTestObject = {
|
|
id: "ex:mock-id-1",
|
|
type: "TestObject",
|
|
stringValue: "string",
|
|
numValue: 42,
|
|
boolValue: true,
|
|
arrayValue: new Set([1, 2, 3]),
|
|
objectValue: {
|
|
nestedString: "nested",
|
|
nestedNum: 7,
|
|
nestedArray: new Set([10, 12]),
|
|
},
|
|
anotherObject: {
|
|
"id:1": {
|
|
id: "id:1",
|
|
prop1: "prop1 value",
|
|
prop2: 100,
|
|
},
|
|
"id:2": {
|
|
id: "id:1",
|
|
prop1: "prop2 value",
|
|
prop2: 200,
|
|
},
|
|
},
|
|
} satisfies TestObject;
|
|
|
|
const mockShapeObject1 = {
|
|
id: "ex:person-1",
|
|
type: "Person",
|
|
name: "Bob",
|
|
address: {
|
|
street: "First street",
|
|
houseNumber: "15",
|
|
},
|
|
hasChildren: true,
|
|
numberOfHouses: 0,
|
|
} satisfies Person;
|
|
const mockShapeObject2 = {
|
|
id: "ex:cat-1",
|
|
type: "Cat",
|
|
name: "Niko's cat",
|
|
age: 12,
|
|
numberOfHomes: 3,
|
|
address: {
|
|
street: "Niko's street",
|
|
houseNumber: "15",
|
|
floor: 0,
|
|
},
|
|
} satisfies Cat;
|
|
|
|
let connectionIdCounter = 1;
|
|
|
|
export default async function requestShape<T extends LdoCompactBase>(
|
|
shape: CompactShapeType<T>,
|
|
scope: Scope | undefined,
|
|
callback: (diff: Diff, connectionId: WasmConnection["id"]) => void
|
|
): Promise<{
|
|
connectionId: string;
|
|
shapeObject: T;
|
|
}> {
|
|
const connectionId = `connection-${connectionIdCounter++}-${
|
|
shape.schema.shapes?.[0].id
|
|
}`;
|
|
|
|
let shapeObject: T;
|
|
if (shape.schema.shapes?.[0].id.includes("TestObject")) {
|
|
shapeObject = mockTestObject as T;
|
|
} else if (shape.schema.shapes?.[0].id.includes("Person")) {
|
|
shapeObject = mockShapeObject1 as T;
|
|
} else if (shape.schema.shapes?.[0].id.includes("Cat")) {
|
|
shapeObject = mockShapeObject2 as T;
|
|
} else {
|
|
console.warn(
|
|
"BACKEND: requestShape for unknown shape, returning empty object.",
|
|
shape.schema.shapes?.[0].id
|
|
);
|
|
shapeObject = {};
|
|
}
|
|
|
|
shapeManager.connections.set(connectionId, {
|
|
id: connectionId,
|
|
shape,
|
|
state: shapeObject,
|
|
callback,
|
|
});
|
|
|
|
return {
|
|
connectionId,
|
|
shapeObject,
|
|
};
|
|
}
|
|
|