Measuring functions by index can be hard to trace back to the function code, that's why trace provides a name property to easily identify the function by name.
import{ Teasim }from"teasim";const app =newTeasim().trace(async({ beforeHandle })=>{for(const child of children){const{ name }=await child;console.log(name);// setup// anonymous}}).get("/",()=>"Hi",{ beforeHandle:[functionsetup(){},()=>{}],}).listen(3000);
TIP
If you are using an arrow function or unnamed function, name will become "anonymous"
Inside the trace callback, you can access Context of the request, and can mutate the value of the request itself, for example using set.headers to update headers.
This is useful when you need support an API like Server-Timing.
import{ Teasim }from"teasim";const app =newTeasim().trace(async({ handle, set })=>{const{ time, end }=await handle; set.headers["Server-Timing"]=`handle;dur=${(await end)- time}`;}).get("/",()=>"Hi").listen(3000);
TIP
Using set inside trace can affect performance, as Teasim defers the execution to the next micro-tick.
Sometimes, beforeHandle or handler can throw an error, skipping the execution of some lifecycles.
By default if this happens, each life-cycle will be resolved automatically, and you can track if the API is executed or not by using skip property
import{ Teasim }from"teasim";const app =newTeasim().trace(async({ handle, set })=>{const{ time, end, skip }=await handle;console.log(skip);}).get("/",()=>"Hi",{beforeHandle(){thrownewError("I'm a teapot");},}).listen(3000);