Compare commits
3 Commits
0e61e4f5b9
...
9f08c34225
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f08c34225 | |||
| 134d4e2b19 | |||
| 8894458f4c |
@@ -1,35 +1,102 @@
|
|||||||
import type { TracerCommand } from '../types';
|
import type { TracerCommand } from '../types';
|
||||||
|
|
||||||
const createTracerContext = () => {
|
class ScopeError extends Error {
|
||||||
const commands: TracerCommand[] = [];
|
constructor(message?: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'ScopeError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if (typeof process !== 'undefined' && typeof process.on === 'function') {
|
const SCOPE_STATE = {
|
||||||
// process.on('exit', () => {
|
INITIAL: 'initial',
|
||||||
// if (commands.length > 0) {
|
SCOPING: 'scoping',
|
||||||
// console.log(commands);
|
SCOPED: 'scoped',
|
||||||
// }
|
} as const;
|
||||||
// });
|
|
||||||
// }
|
type ScopeState = (typeof SCOPE_STATE)[keyof typeof SCOPE_STATE];
|
||||||
|
|
||||||
|
type Error = {
|
||||||
|
stack?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ScopeResult = {
|
||||||
|
// 记录指令序列
|
||||||
|
commands: TracerCommand[];
|
||||||
|
// 记录异常信息
|
||||||
|
exception: Error;
|
||||||
|
// 检查scope的执行状态
|
||||||
|
state: ScopeState;
|
||||||
|
};
|
||||||
|
|
||||||
|
const createTracerContext = () => {
|
||||||
|
const scopeResult: ScopeResult = {
|
||||||
|
commands: [],
|
||||||
|
exception: {},
|
||||||
|
state: SCOPE_STATE.INITIAL,
|
||||||
|
};
|
||||||
|
|
||||||
|
const command = (command: TracerCommand) => {
|
||||||
|
scopeResult.commands.push(command);
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: 输出指令序列和错误信息
|
||||||
|
const flush = () => {
|
||||||
|
const { commands, exception } = scopeResult;
|
||||||
|
if (commands.length > 0 || !!exception.stack) {
|
||||||
|
console.log(JSON.stringify({ commands, exception }, null, 2));
|
||||||
|
}
|
||||||
|
scopeResult.commands = [];
|
||||||
|
scopeResult.exception = {};
|
||||||
|
};
|
||||||
|
|
||||||
const getTracerContext = () => {
|
const getTracerContext = () => {
|
||||||
const command = (command: TracerCommand) => {
|
|
||||||
commands.push(command);
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: 输出指令序列
|
|
||||||
const dump = () => {
|
|
||||||
return commands;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
command,
|
command,
|
||||||
dump,
|
flush,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const scope = (routine: () => void | Promise<void>) => {
|
||||||
|
if (scopeResult.state === SCOPE_STATE.SCOPED) {
|
||||||
|
throw new ScopeError('ScopeError: Detect multiple scope.');
|
||||||
|
}
|
||||||
|
if (scopeResult.state === SCOPE_STATE.SCOPING) {
|
||||||
|
throw new ScopeError('ScopeError: Detect nested scope.');
|
||||||
|
}
|
||||||
|
scopeResult.state = SCOPE_STATE.SCOPING;
|
||||||
|
// 如果routine是同步函数,那可能会抛出同步错误,所以需要用try-catch包裹
|
||||||
|
try {
|
||||||
|
const result = routine();
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
result
|
||||||
|
.catch((error) => {
|
||||||
|
if (error instanceof ScopeError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
scopeResult.exception.stack = (error as Error).stack;
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
scopeResult.state = SCOPE_STATE.SCOPED;
|
||||||
|
flush();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
scopeResult.state = SCOPE_STATE.SCOPED;
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ScopeError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
scopeResult.exception.stack = (error as Error).stack;
|
||||||
|
scopeResult.state = SCOPE_STATE.SCOPED;
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getTracerContext,
|
getTracerContext,
|
||||||
|
scope,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const { getTracerContext } = createTracerContext();
|
export const { getTracerContext, scope } = createTracerContext();
|
||||||
|
|||||||
Reference in New Issue
Block a user