设计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

34
tracers.ts/.gitignore vendored Normal file
View File

@@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules
# output
out
dist
*.tgz
# code coverage
coverage
*.lcov
# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# caches
.eslintcache
.cache
*.tsbuildinfo
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

15
tracers.ts/README.md Normal file
View File

@@ -0,0 +1,15 @@
# tracers.ts
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.2.23. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.

29
tracers.ts/bun.lock Normal file
View File

@@ -0,0 +1,29 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "tracers.ts",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5",
},
},
},
"packages": {
"@types/bun": ["@types/bun@1.3.2", "", { "dependencies": { "bun-types": "1.3.2" } }, "sha512-t15P7k5UIgHKkxwnMNkJbWlh/617rkDGEdSsDbu+qNHTaz9SKf7aC8fiIlUdD5RPpH6GEkP0cK7WlvmrEBRtWg=="],
"@types/node": ["@types/node@24.10.1", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="],
"@types/react": ["@types/react@19.2.4", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-tBFxBp9Nfyy5rsmefN+WXc1JeW/j2BpBHFdLZbEVfs9wn3E3NRFxwV0pJg8M1qQAexFpvz73hJXFofV0ZAu92A=="],
"bun-types": ["bun-types@1.3.2", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg=="],
"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
"undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
}
}

12
tracers.ts/package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "tracers.ts",
"module": "index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}

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;

29
tracers.ts/tsconfig.json Normal file
View File

@@ -0,0 +1,29 @@
{
"compilerOptions": {
// Environment setup & latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}