From e502cb78409ba927a020bb3a0c6729c1f1a96bd7 Mon Sep 17 00:00:00 2001 From: skycurtain Date: Mon, 10 Nov 2025 01:55:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E8=AE=A1tracer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.ts | 7 -- src/index.ts | 12 ++ src/tracers.ts/action-type.ts | 49 -------- src/tracers.ts/command.ts | 7 -- src/tracers.ts/commander/index.ts | 26 +++++ src/tracers.ts/index.ts | 109 ------------------ src/tracers.ts/registry/index.ts | 25 ++++ src/tracers.ts/tracer-type.ts | 12 -- src/tracers.ts/tracers/array-tracer.ts | 55 +++++++++ src/tracers.ts/tracers/index.ts | 1 + src/tracers.ts/types/action/index.ts | 5 + .../types/command/array-tracer-command.ts | 57 +++++++++ src/tracers.ts/types/command/index.ts | 5 + src/tracers.ts/types/tracer/index.ts | 5 + 14 files changed, 191 insertions(+), 184 deletions(-) delete mode 100644 index.ts create mode 100644 src/index.ts delete mode 100644 src/tracers.ts/action-type.ts delete mode 100644 src/tracers.ts/command.ts create mode 100644 src/tracers.ts/commander/index.ts delete mode 100644 src/tracers.ts/index.ts create mode 100644 src/tracers.ts/registry/index.ts delete mode 100644 src/tracers.ts/tracer-type.ts create mode 100644 src/tracers.ts/tracers/array-tracer.ts create mode 100644 src/tracers.ts/tracers/index.ts create mode 100644 src/tracers.ts/types/action/index.ts create mode 100644 src/tracers.ts/types/command/array-tracer-command.ts create mode 100644 src/tracers.ts/types/command/index.ts create mode 100644 src/tracers.ts/types/tracer/index.ts diff --git a/index.ts b/index.ts deleted file mode 100644 index fd293be..0000000 --- a/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ArrayTracer } from "./src/tracers.ts"; - -const arrayTracer = ArrayTracer.define({ - description: "Array Tracer", -}); - -arrayTracer.preset([1, 2, 3]); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..d2616b2 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,12 @@ +import { Commander } from "./tracers.ts/commander"; +import { ArrayTracer } from "./tracers.ts/tracers"; + +const arrayTracer = ArrayTracer.define({ + description: "Array Tracer", +}); + +arrayTracer.preset([1, 2, 3]); + +const commander = Commander.getInstance(); +const commands = commander.output(); +console.log(commands); diff --git a/src/tracers.ts/action-type.ts b/src/tracers.ts/action-type.ts deleted file mode 100644 index a5860d6..0000000 --- a/src/tracers.ts/action-type.ts +++ /dev/null @@ -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; diff --git a/src/tracers.ts/command.ts b/src/tracers.ts/command.ts deleted file mode 100644 index 5cb4a50..0000000 --- a/src/tracers.ts/command.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { TracerAction } from "./action-type"; - -export type Command = { - tracer: string; - action: TracerAction; - params: any; -} \ No newline at end of file diff --git a/src/tracers.ts/commander/index.ts b/src/tracers.ts/commander/index.ts new file mode 100644 index 0000000..de1b538 --- /dev/null +++ b/src/tracers.ts/commander/index.ts @@ -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; + } +} diff --git a/src/tracers.ts/index.ts b/src/tracers.ts/index.ts deleted file mode 100644 index a540340..0000000 --- a/src/tracers.ts/index.ts +++ /dev/null @@ -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; - - 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 { - description: string; - serializeFn?: (value: T) => string; -} - -export class ArrayTracer { - private tracerId: string; - private serializeFn?: (value: T) => string; - - private constructor(option: ArrayTracerOption) { - this.tracerId = crypto.randomUUID(); - this.serializeFn = option.serializeFn; - } - - public static define(option: ArrayTracerOption): ArrayTracer { - 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) {} -} diff --git a/src/tracers.ts/registry/index.ts b/src/tracers.ts/registry/index.ts new file mode 100644 index 0000000..6988f4c --- /dev/null +++ b/src/tracers.ts/registry/index.ts @@ -0,0 +1,25 @@ +import type { TracerType } from "../types/tracer"; + +export class Registry { + private tracers: Map; + + private constructor() { + this.tracers = new Map(); + } + + 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); + } +} diff --git a/src/tracers.ts/tracer-type.ts b/src/tracers.ts/tracer-type.ts deleted file mode 100644 index b0b9063..0000000 --- a/src/tracers.ts/tracer-type.ts +++ /dev/null @@ -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]; diff --git a/src/tracers.ts/tracers/array-tracer.ts b/src/tracers.ts/tracers/array-tracer.ts new file mode 100644 index 0000000..30c6dc0 --- /dev/null +++ b/src/tracers.ts/tracers/array-tracer.ts @@ -0,0 +1,55 @@ +import { Commander } from "../commander"; +import { Registry } from "../registry"; + +export interface ArrayTracerOption { + description: string; + serializeFn?: (value: T) => string; +} + +export class ArrayTracer { + private tracerId: string; + private serializeFn?: (value: T) => string; + + private constructor(option: ArrayTracerOption) { + this.tracerId = crypto.randomUUID(); + this.serializeFn = option.serializeFn; + } + + public static define(option: ArrayTracerOption): ArrayTracer { + 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) {} +} diff --git a/src/tracers.ts/tracers/index.ts b/src/tracers.ts/tracers/index.ts new file mode 100644 index 0000000..c9bf137 --- /dev/null +++ b/src/tracers.ts/tracers/index.ts @@ -0,0 +1 @@ +export * from "./array-tracer"; \ No newline at end of file diff --git a/src/tracers.ts/types/action/index.ts b/src/tracers.ts/types/action/index.ts new file mode 100644 index 0000000..e37aa7d --- /dev/null +++ b/src/tracers.ts/types/action/index.ts @@ -0,0 +1,5 @@ +import type { ArrayTracerCommand } from "../command"; + +export type ArrayTracerAction = ArrayTracerCommand["action"]; + +export type TracerAction = ArrayTracerAction; diff --git a/src/tracers.ts/types/command/array-tracer-command.ts b/src/tracers.ts/types/command/array-tracer-command.ts new file mode 100644 index 0000000..2b066f1 --- /dev/null +++ b/src/tracers.ts/types/command/array-tracer-command.ts @@ -0,0 +1,57 @@ +export type ArrayTracerDefineCommand = { + tracer: null; + action: "define"; + params: { type: "ArrayTracer"; id: string; desc?: string }; +}; + +export type ArrayTracerPresetCommand = { + 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 = { + tracer: string; + action: "patch"; + params: { index: number; value: T }; +}; + +export type ArrayTracerResetCommand = { + tracer: string; + action: "reset"; + params: null; +}; + +export type ArrayTracerCommand = + | ArrayTracerDefineCommand + | ArrayTracerPresetCommand + | ArrayTracerExtendCommand + | ArrayTracerShrinkCommand + | ArrayTracerPickCommand + | ArrayTracerDropCommand + | ArrayTracerPatchCommand + | ArrayTracerResetCommand; diff --git a/src/tracers.ts/types/command/index.ts b/src/tracers.ts/types/command/index.ts new file mode 100644 index 0000000..d852989 --- /dev/null +++ b/src/tracers.ts/types/command/index.ts @@ -0,0 +1,5 @@ +import type { ArrayTracerCommand } from "./array-tracer-command"; + +export * from "./array-tracer-command"; + +export type TracerCommand = ArrayTracerCommand diff --git a/src/tracers.ts/types/tracer/index.ts b/src/tracers.ts/types/tracer/index.ts new file mode 100644 index 0000000..5787464 --- /dev/null +++ b/src/tracers.ts/types/tracer/index.ts @@ -0,0 +1,5 @@ +import type { ArrayTracerDefineCommand } from "../command"; + +type ArrayTracer = ArrayTracerDefineCommand["params"]["type"]; + +export type TracerType = ArrayTracer;