README
typed-task-log
typed-task-log
is a task-oriented event emitter to facilitate logging with context.
Install
npm install typed-task-logger
or
yarn add typed-task-logger
Usage
See usage examples from the tests.
Full example
type TaskMeta = {
readonly parentTaskId: null | string;
readonly label: string;
readonly kind: "add" | "mult";
};
let nextId = 0;
const taskLog = new TaskLog<TaskMeta>({
createUniqueId: () => `${nextId++}`,
performanceNow: () => performance.now(),
});
const history: TaskEvent<TaskMeta>[] = [];
taskLog.addListener((event) => history.push(event));
const value = await taskLog.task(
{ parentTaskId: null, label: "task 1", kind: "add" },
async (parentTaskId) => {
await new Promise((resolve) => setTimeout(resolve, 100));
return (
(await taskLog.task(
{ parentTaskId, label: "childAdd", kind: "add" },
async () => 1 + 4,
)) +
(await taskLog.task(
{ parentTaskId, label: "childMult", kind: "mult" },
async () => 5 * 3,
))
);
},
);
taskLog.removeAllListeners();
expect(value).toEqual(20);
expect(history).toEqual([
{
taskId: "0",
kind: "begin",
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
meta: { parentTaskId: null, label: "task 1", kind: "add" },
},
{
taskId: "1",
kind: "begin",
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
meta: { parentTaskId: "0", label: "childAdd", kind: "add" },
},
{
taskId: "1",
meta: { parentTaskId: "0", label: "childAdd", kind: "add" },
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
duration: expect.any(Number),
kind: "end",
result: { kind: "value", value: 5 },
},
{
taskId: "2",
kind: "begin",
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
meta: { parentTaskId: "0", label: "childMult", kind: "mult" },
},
{
taskId: "2",
meta: { parentTaskId: "0", label: "childMult", kind: "mult" },
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
duration: expect.any(Number),
kind: "end",
result: { kind: "value", value: 15 },
},
{
taskId: "0",
meta: { parentTaskId: null, label: "task 1", kind: "add" },
timestamp: expect.any(Number),
performanceNow: expect.any(Number),
duration: expect.any(Number),
kind: "end",
result: { kind: "value", value: 20 },
},
]);