设计tracer
This commit is contained in:
26
tracers.ts/src/commander/index.ts
Normal file
26
tracers.ts/src/commander/index.ts
Normal 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
10
tracers.ts/src/index.ts
Normal 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);
|
||||
25
tracers.ts/src/registry/index.ts
Normal file
25
tracers.ts/src/registry/index.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
94
tracers.ts/src/tracer/array-tracer.ts
Normal file
94
tracers.ts/src/tracer/array-tracer.ts
Normal 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 },
|
||||
});
|
||||
}
|
||||
}
|
||||
1
tracers.ts/src/tracer/index.ts
Normal file
1
tracers.ts/src/tracer/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./array-tracer";
|
||||
5
tracers.ts/src/types/action/index.ts
Normal file
5
tracers.ts/src/types/action/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { ArrayTracerCommand } from "../command";
|
||||
|
||||
export type ArrayTracerAction = ArrayTracerCommand["action"];
|
||||
|
||||
export type TracerAction = ArrayTracerAction;
|
||||
57
tracers.ts/src/types/command/array-tracer-command.ts
Normal file
57
tracers.ts/src/types/command/array-tracer-command.ts
Normal 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;
|
||||
5
tracers.ts/src/types/command/index.ts
Normal file
5
tracers.ts/src/types/command/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { ArrayTracerCommand } from "./array-tracer-command";
|
||||
|
||||
export * from "./array-tracer-command";
|
||||
|
||||
export type TracerCommand<T = never> = ArrayTracerCommand<T>;
|
||||
5
tracers.ts/src/types/tracer/index.ts
Normal file
5
tracers.ts/src/types/tracer/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { ArrayTracerDefineCommand } from "../command";
|
||||
|
||||
type ArrayTracer = ArrayTracerDefineCommand["params"]["type"];
|
||||
|
||||
export type TracerType = ArrayTracer;
|
||||
Reference in New Issue
Block a user