设计tracer

This commit is contained in:
2025-11-10 01:55:32 +08:00
parent 5064567457
commit e502cb7840
14 changed files with 191 additions and 184 deletions

View File

@@ -1,7 +0,0 @@
import { ArrayTracer } from "./src/tracers.ts";
const arrayTracer = ArrayTracer.define<number>({
description: "Array Tracer",
});
arrayTracer.preset([1, 2, 3]);

12
src/index.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Commander } from "./tracers.ts/commander";
import { ArrayTracer } from "./tracers.ts/tracers";
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

@@ -1,49 +0,0 @@
export const ARRAY_TRACER_ACTIONS = [
'define',
'preset',
'expand',
'reduce',
'pick',
'drop',
'patch',
'reset',
] as const;
export type ArrayTracerAction = (typeof ARRAY_TRACER_ACTIONS)[number];
export const STACK_TRACER_ACTIONS = [
'define',
'pick',
'drop',
'patch',
'reset',
] as const;
export type StackTracerAction = (typeof STACK_TRACER_ACTIONS)[number];
export const QUEUE_TRACER_ACTIONS = [
'define',
'pick',
'drop',
'patch',
'reset',
] as const;
export type QueueTracerAction = (typeof QUEUE_TRACER_ACTIONS)[number];
export const MATRIX_TRACER_ACTIONS = [
'define',
'preset',
'pick',
'drop',
'patch',
'reset',
] as const;
export type MatrixTracerAction = (typeof MATRIX_TRACER_ACTIONS)[number];
export type TracerAction =
| ArrayTracerAction
| StackTracerAction
| QueueTracerAction
| MatrixTracerAction;

View File

@@ -1,7 +0,0 @@
import type { TracerAction } from "./action-type";
export type Command = {
tracer: string;
action: TracerAction;
params: any;
}

View File

@@ -0,0 +1,26 @@
import type { TracerCommand } from "../types/command";
export class Commander {
private commands: TracerCommand[];
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(command: TracerCommand) {
this.commands.push(command);
}
public output() {
return this.commands;
}
}

View File

@@ -1,109 +0,0 @@
const TracerType = {
ArrayTracer: 'ArrayTracer',
StackTracer: 'StackTracer',
QueueTracer: 'QueueTracer',
MatrixTracer: 'MatrixTracer',
SortTracer: 'SortTracer',
LinkTracer: 'LinkTracer',
TreeTracer: 'TreeTracer',
GraphTracer: 'GraphTracer',
} as const;
type TracerTypeKey = keyof typeof TracerType;
type TracerTypeVal = (typeof TracerType)[TracerTypeKey];
const ArrayTracerAction = {
Define: 'define',
Preset: 'preset',
Expand: 'expand',
Reduce: 'reduce',
Pick: 'pick',
Drop: 'drop',
Patch: 'patch',
Reset: 'reset',
} as const;
type ArrayTracerActionKey = keyof typeof ArrayTracerAction;
type ArrayTracerActionVal = (typeof ArrayTracerAction)[ArrayTracerActionKey];
const Action = {
...ArrayTracerAction,
} as const;
type ActionKey = keyof typeof Action;
type ActionVal = (typeof Action)[ActionKey];
class TracerRegistry {
private tracers: Record<TracerTypeVal, string[]>;
private constructor() {
this.tracers = {
ArrayTracer: [],
StackTracer: [],
QueueTracer: [],
MatrixTracer: [],
SortTracer: [],
LinkTracer: [],
TreeTracer: [],
GraphTracer: [],
};
}
public register(tracerType: TracerTypeVal, tracerId: string) {
this.tracers[tracerType].push(tracerId);
}
}
interface Command {
tracer: string;
action: ActionVal;
params: any;
}
class Commander {
private commands: Command[];
private constructor() {
this.commands = [];
}
public static command() {}
public static serialize() {}
}
export interface ArrayTracerOption<T> {
description: string;
serializeFn?: (value: T) => string;
}
export class ArrayTracer<T> {
private tracerId: string;
private serializeFn?: (value: T) => string;
private constructor(option: ArrayTracerOption<T>) {
this.tracerId = crypto.randomUUID();
this.serializeFn = option.serializeFn;
}
public static define<T>(option: ArrayTracerOption<T>): ArrayTracer<T> {
return new ArrayTracer(option);
}
public preset(array: T[]) {}
public expand(size: number) {}
public reduce(size: number) {}
public pick(index: number) {}
public drop(index: number) {}
public patch(index: number, value: T) {}
public reset(index: number) {}
}

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

@@ -1,12 +0,0 @@
export const TRACER_TYPES = [
'ArrayTracer',
'StackTracer',
'QueueTracer',
'MatrixTracer',
'SortTracer',
'LinkTracer',
'TreeTracer',
'GraphTracer',
] as const;
export type TracerType = typeof TRACER_TYPES[number];

View File

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

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 = {
tracer: null;
action: "define";
params: { type: "ArrayTracer"; id: string; desc?: string };
};
export type ArrayTracerPresetCommand<T = unknown> = {
tracer: string;
action: "preset";
params: T[];
};
export type ArrayTracerExtendCommand = {
tracer: string;
action: "extend";
params: { size: number };
};
export type ArrayTracerShrinkCommand = {
tracer: string;
action: "shrink";
params: { size: number };
};
export type ArrayTracerPickCommand = {
tracer: string;
action: "pick";
params: { index: number };
};
export type ArrayTracerDropCommand = {
tracer: string;
action: "drop";
params: { index: number };
};
export type ArrayTracerPatchCommand<T = unknown> = {
tracer: string;
action: "patch";
params: { index: number; value: T };
};
export type ArrayTracerResetCommand = {
tracer: string;
action: "reset";
params: null;
};
export type ArrayTracerCommand<T = unknown> =
| 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 = ArrayTracerCommand

View File

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