88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
import {
|
|
assertCircuitGroupSubtreeSnapshot,
|
|
type CircuitGroupSubtreeSnapshot,
|
|
} from "./circuit-group-subtree-snapshot.model.js";
|
|
import type { SerializedProjectCommand } from "./project-command.model.js";
|
|
|
|
export const circuitGroupDeleteSubtreeCommandType =
|
|
"circuit-group.delete-subtree" as const;
|
|
export const circuitGroupRestoreSubtreeCommandType =
|
|
"circuit-group.restore-subtree" as const;
|
|
export const circuitGroupSubtreeCommandSchemaVersion = 1 as const;
|
|
|
|
interface Payload {
|
|
snapshot: CircuitGroupSubtreeSnapshot;
|
|
}
|
|
|
|
export interface CircuitGroupDeleteSubtreeProjectCommand
|
|
extends SerializedProjectCommand<Payload> {
|
|
schemaVersion: typeof circuitGroupSubtreeCommandSchemaVersion;
|
|
type: typeof circuitGroupDeleteSubtreeCommandType;
|
|
}
|
|
|
|
export interface CircuitGroupRestoreSubtreeProjectCommand
|
|
extends SerializedProjectCommand<Payload> {
|
|
schemaVersion: typeof circuitGroupSubtreeCommandSchemaVersion;
|
|
type: typeof circuitGroupRestoreSubtreeCommandType;
|
|
}
|
|
|
|
export type CircuitGroupSubtreeProjectCommand =
|
|
| CircuitGroupDeleteSubtreeProjectCommand
|
|
| CircuitGroupRestoreSubtreeProjectCommand;
|
|
|
|
export function createCircuitGroupDeleteSubtreeProjectCommand(
|
|
snapshot: CircuitGroupSubtreeSnapshot
|
|
): CircuitGroupDeleteSubtreeProjectCommand {
|
|
const command: CircuitGroupDeleteSubtreeProjectCommand = {
|
|
schemaVersion: circuitGroupSubtreeCommandSchemaVersion,
|
|
type: circuitGroupDeleteSubtreeCommandType,
|
|
payload: { snapshot },
|
|
};
|
|
assertCircuitGroupDeleteSubtreeProjectCommand(command);
|
|
return command;
|
|
}
|
|
|
|
export function createCircuitGroupRestoreSubtreeProjectCommand(
|
|
snapshot: CircuitGroupSubtreeSnapshot
|
|
): CircuitGroupRestoreSubtreeProjectCommand {
|
|
const command: CircuitGroupRestoreSubtreeProjectCommand = {
|
|
schemaVersion: circuitGroupSubtreeCommandSchemaVersion,
|
|
type: circuitGroupRestoreSubtreeCommandType,
|
|
payload: { snapshot },
|
|
};
|
|
assertCircuitGroupRestoreSubtreeProjectCommand(command);
|
|
return command;
|
|
}
|
|
|
|
export function assertCircuitGroupDeleteSubtreeProjectCommand(
|
|
command: SerializedProjectCommand<unknown>
|
|
): asserts command is CircuitGroupDeleteSubtreeProjectCommand {
|
|
assertCommand(command, circuitGroupDeleteSubtreeCommandType);
|
|
}
|
|
|
|
export function assertCircuitGroupRestoreSubtreeProjectCommand(
|
|
command: SerializedProjectCommand<unknown>
|
|
): asserts command is CircuitGroupRestoreSubtreeProjectCommand {
|
|
assertCommand(command, circuitGroupRestoreSubtreeCommandType);
|
|
}
|
|
|
|
function assertCommand(
|
|
command: SerializedProjectCommand<unknown>,
|
|
type:
|
|
| typeof circuitGroupDeleteSubtreeCommandType
|
|
| typeof circuitGroupRestoreSubtreeCommandType
|
|
) {
|
|
if (
|
|
command.schemaVersion !== circuitGroupSubtreeCommandSchemaVersion ||
|
|
command.type !== type ||
|
|
!isPlainObject(command.payload) ||
|
|
Object.keys(command.payload).length !== 1
|
|
) {
|
|
throw new Error("Unsupported circuit-group subtree command.");
|
|
}
|
|
assertCircuitGroupSubtreeSnapshot(command.payload.snapshot);
|
|
}
|
|
|
|
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
}
|