设计tracer

This commit is contained in:
2025-11-13 20:41:25 +08:00
parent 0ade49a589
commit 7033208b06
16 changed files with 41 additions and 43 deletions

View File

@@ -0,0 +1,26 @@
import type { TracerCommand } from "../types/command";
export class Commander {
private commands: TracerCommand<unknown>[];
private constructor() {
this.commands = [];
}
private static instance: Commander | null = null;
public static getInstance(): Commander {
if (!this.instance) {
this.instance = new Commander();
}
return this.instance;
}
public command<T = never>(command: TracerCommand<T>) {
this.commands.push(command);
}
public output() {
return this.commands;
}
}

10
tracers.ts/src/index.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Commander } from "./commander";
import { ArrayTracer } from "./tracer";
const arrayTracer = ArrayTracer.define<number>({ description: 'Array Tracer' });
arrayTracer.preset([1, 2, 3]);
const commander = Commander.getInstance();
const commands = commander.output();
console.log(commands);

View File

@@ -0,0 +1,25 @@
import type { TracerType } from "../types/tracer";
export class Registry {
private tracers: Map<TracerType, string[]>;
private constructor() {
this.tracers = new Map<TracerType, string[]>();
}
private static instance: Registry | null = null;
public static getInstance() {
if (!this.instance) {
this.instance = new Registry();
}
return this.instance;
}
public register(tracerType: TracerType, string: string) {
if (!this.tracers.has(tracerType)) {
this.tracers.set(tracerType, []);
}
this.tracers.get(tracerType)?.push(string);
}
}

View File

@@ -0,0 +1,94 @@
import { Commander } from "../commander";
import { Registry } from "../registry";
export interface ArrayTracerOption<T> {
description: string;
}
export class ArrayTracer<T> {
private tracerId: string;
private constructor() {
this.tracerId = crypto.randomUUID();
}
public static define<T>(option: ArrayTracerOption<T>): ArrayTracer<T> {
const arrayTracer = new ArrayTracer();
const registry = Registry.getInstance();
registry.register("ArrayTracer", arrayTracer.tracerId);
const commander = Commander.getInstance();
commander.command({
action: "define",
tracer: null,
params: {
type: "ArrayTracer",
id: arrayTracer.tracerId,
desc: option.description,
},
});
return arrayTracer;
}
public preset(array: T[]) {
const commander = Commander.getInstance();
commander.command({
action: "preset",
tracer: this.tracerId,
params: array,
});
}
public extend(size: number) {
const commander = Commander.getInstance();
commander.command({
action: "extend",
tracer: this.tracerId,
params: { size },
});
}
public shrink(size: number) {
const commander = Commander.getInstance();
commander.command({
action: "shrink",
tracer: this.tracerId,
params: { size },
});
}
public pick(index: number) {
const commander = Commander.getInstance();
commander.command({
action: "pick",
tracer: this.tracerId,
params: { index },
});
}
public drop(index: number) {
const commander = Commander.getInstance();
commander.command({
action: "drop",
tracer: this.tracerId,
params: { index },
});
}
public patch(index: number, value: T) {
const commander = Commander.getInstance();
commander.command({
action: "patch",
tracer: this.tracerId,
params: { index, value },
});
}
public reset(index: number) {
const commander = Commander.getInstance();
commander.command({
action: "reset",
tracer: this.tracerId,
params: { index },
});
}
}

View File

@@ -0,0 +1 @@
export * from "./array-tracer";

View File

@@ -0,0 +1,5 @@
import type { ArrayTracerCommand } from "../command";
export type ArrayTracerAction = ArrayTracerCommand["action"];
export type TracerAction = ArrayTracerAction;

View File

@@ -0,0 +1,57 @@
export type ArrayTracerDefineCommand = {
action: "define";
tracer: null;
params: { type: "ArrayTracer"; id: string; desc?: string };
};
export type ArrayTracerPresetCommand<T = unknown> = {
action: "preset";
tracer: string;
params: T[];
};
export type ArrayTracerExtendCommand = {
action: "extend";
tracer: string;
params: { size: number };
};
export type ArrayTracerShrinkCommand = {
action: "shrink";
tracer: string;
params: { size: number };
};
export type ArrayTracerPickCommand = {
action: "pick";
tracer: string;
params: { index: number };
};
export type ArrayTracerDropCommand = {
action: "drop";
tracer: string;
params: { index: number };
};
export type ArrayTracerPatchCommand<T = unknown> = {
action: "patch";
tracer: string;
params: { index: number; value: T };
};
export type ArrayTracerResetCommand = {
action: "reset";
tracer: string;
params: { index: number };
};
export type ArrayTracerCommand<T = never> =
| ArrayTracerDefineCommand
| ArrayTracerPresetCommand<T>
| ArrayTracerExtendCommand
| ArrayTracerShrinkCommand
| ArrayTracerPickCommand
| ArrayTracerDropCommand
| ArrayTracerPatchCommand<T>
| ArrayTracerResetCommand;

View File

@@ -0,0 +1,5 @@
import type { ArrayTracerCommand } from "./array-tracer-command";
export * from "./array-tracer-command";
export type TracerCommand<T = never> = ArrayTracerCommand<T>;

View File

@@ -0,0 +1,5 @@
import type { ArrayTracerDefineCommand } from "../command";
type ArrayTracer = ArrayTracerDefineCommand["params"]["type"];
export type TracerType = ArrayTracer;