重构tracer设计
This commit is contained in:
102
tracers.ts/src/tracers/array-tracer.ts
Normal file
102
tracers.ts/src/tracers/array-tracer.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { getTracerContext } from '../context';
|
||||
import type { JsonValue } from '../types';
|
||||
|
||||
interface ArrayTracerCreateOptions<T extends JsonValue> {
|
||||
description?: string;
|
||||
array?: T[];
|
||||
}
|
||||
|
||||
export const createArrayTracer = <T extends JsonValue>(
|
||||
options: ArrayTracerCreateOptions<T>,
|
||||
) => {
|
||||
const { description = 'ArrayTracer', array } = options;
|
||||
const tracer = crypto.randomUUID();
|
||||
|
||||
const { command } = getTracerContext();
|
||||
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'create',
|
||||
params: {
|
||||
description: description,
|
||||
array: array,
|
||||
},
|
||||
});
|
||||
|
||||
const preset = (array: T[]) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'preset',
|
||||
params: {
|
||||
array: array,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const scale = (size: number) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'scale',
|
||||
params: {
|
||||
size: size,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const pick = (index: number) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'pick',
|
||||
params: {
|
||||
index: index,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const drop = (index: number) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'drop',
|
||||
params: {
|
||||
index: index,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const patch = (index: number, value: T) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'patch',
|
||||
params: {
|
||||
index: index,
|
||||
value: value,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const unset = (index: number) => {
|
||||
command({
|
||||
type: 'ArrayTracer',
|
||||
tracer: tracer,
|
||||
action: 'unset',
|
||||
params: {
|
||||
index: index,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
// preset,
|
||||
scale,
|
||||
pick,
|
||||
drop,
|
||||
patch,
|
||||
unset,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user