Skip to content

Commit c30e018

Browse files
committed
restructure files and folders
1 parent 37448d4 commit c30e018

13 files changed

+251
-201
lines changed

src/TryCatchFinallyHooks.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { FunctionContext, createTryCatchFinally, FunctionInterceptors } from "./tryCatchFinally";
1+
import { FunctionContext, createTryCatchFinally } from "./tryCatchFinally";
22

3-
export interface ITryCatchFinallyHook<TParentContext extends FunctionContext=FunctionContext, TrtReturnContext={}>{
4-
onTry(ctx: TParentContext): void | {
3+
export interface ITryCatchFinallyHook<TParentContext=FunctionContext, TrtReturnContext={}>{
4+
onTry(ctx: TParentContext & FunctionContext): void | {
55
context?: TrtReturnContext
6-
onCatch?(ctx:TParentContext&TrtReturnContext): void
7-
onFinally?(ctx:TParentContext&TrtReturnContext): void
6+
onCatch?(ctx:FunctionContext & TParentContext&TrtReturnContext): void
7+
onFinally?(ctx:FunctionContext & TParentContext & TrtReturnContext): void
88
/**
99
* If true, the hook will be executed after all other hooks
1010
*/
@@ -20,9 +20,14 @@ export class TryCatchFinallyHooksBuilder<FullHookContext extends FunctionContext
2020
{
2121
private hooks:ITryCatchFinallyHook[] = []
2222

23+
add<TryReturnContext={}>(onTry:ITryCatchFinallyHook<FullHookContext,TryReturnContext>['onTry']): TryCatchFinallyHooksBuilder<FullHookContext & TryReturnContext>
2324
add<THookContext extends {}, TryReturnContext extends {}={}>(hook: ITryCatchFinallyHook<FullHookContext & THookContext,TryReturnContext>)
2425
: TryCatchFinallyHooksBuilder<THookContext &FullHookContext & TryReturnContext>
26+
add(hook:any):any
2527
{
28+
if(hook instanceof Function)
29+
return this.add(TryCatchFinallyHooksBuilder.createHook(hook) as any)
30+
2631
this.hooks.push(hook);
2732
return this as any;
2833
}
@@ -33,12 +38,6 @@ export class TryCatchFinallyHooksBuilder<FullHookContext extends FunctionContext
3338
onTry
3439
} as any
3540
}
36-
37-
createAndAdd<TryReturnContext={}>(onTry:ITryCatchFinallyHook<FullHookContext,TryReturnContext>['onTry']): TryCatchFinallyHooksBuilder<FullHookContext & TryReturnContext>
38-
{
39-
this.add(TryCatchFinallyHooksBuilder.createHook(onTry) as any)
40-
return this as any
41-
}
4241

4342
asFunctionWrapper(args?: DecoratorArgsOf<FullHookContext>): <TFunc extends (...args:any[])=>any>(func:TFunc)=>TFunc
4443
{

src/callStack.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { TryCatchFinallyHooksBuilder } from "./TryCatchFinallyHooks"
22
import { callStack } from './callStack'
3-
import { AsyncResource, AsyncLocalStorage } from 'node:async_hooks'
43

54
function createTrack(log:any){
65
return new TryCatchFinallyHooksBuilder()
@@ -53,24 +52,16 @@ test("callstack async",async ()=>{
5352
const log = jest.fn()
5453
const track = createTrack(log)
5554

56-
const asyncStr = new AsyncLocalStorage<any>()
57-
asyncStr.enterWith(["root"])
5855

5956
const myChildFunc = jest.fn(track.asFunctionWrapper({name:"MyAsyncChildFunc"})(async function myChildFunc(a:number,b:number){
60-
const path = [...asyncStr.getStore()||[]]
61-
asyncStr.enterWith([...path,"child"])
6257
await delay(Math.random()*1000)
63-
asyncStr.enterWith(path)
64-
6558
return a+b
6659
}))
6760

6861

6962
const myParentFunc = jest.fn(track.asFunctionWrapper({name: 'MyAsyncParentFunc'})(async function myParentFunc(){
70-
const path = [...asyncStr.getStore()||[]]
71-
asyncStr.enterWith([...path,"parent"])
7263
return await Promise.allSettled(new Array(amountOfParallels).fill(0).map(async function promiseAllRunner(_,i){
73-
await myChildFunc(i,i*2)
64+
await myChildFunc(i,i*2)
7465
}))
7566
}))
7667

src/callStack.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ITryCatchFinallyHook, TryCatchFinallyHooksBuilder } from "./TryCatchFinallyHooks";
2-
import { FunctionContext } from "./tryCatchFinally";
32

43
/**
54
* This is a callstack tracker, which provides Stack of Called Function Context - works both for async and sync call stacks
@@ -32,8 +31,8 @@ export type CallStackContext = {
3231
};
3332

3433

35-
export const callStack = TryCatchFinallyHooksBuilder.createHook<CallStackContext>(
36-
(ctx)=>{
34+
export const callStack: ITryCatchFinallyHook<CallStackContext>= {
35+
onTry(ctx){
3736
if(!ctx.name)
3837
ctx.name = ctx.args.name || ctx.func.name || '<anonymous>'
3938

@@ -58,7 +57,7 @@ export const callStack = TryCatchFinallyHooksBuilder.createHook<CallStackContext
5857
lastInQueue: true
5958
};
6059
}
61-
)
60+
}
6261

6362
/**
6463
* Returns current tracked callstack. Filters Error.captureStackTrace by tracked functions.

src/defer.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ITryCatchFinallyHook } from "./TryCatchFinallyHooks";
2+
import { FunctionContext } from "./tryCatchFinally";
3+
4+
export type DeferContext = {
5+
defer: (fnToDefer: (ctx: DeferContext) => void) => void
6+
}
7+
8+
export const Defer: ITryCatchFinallyHook<DeferContext> = {
9+
onTry(ctx){
10+
const funcsToDefer = [] as ((ctx:DeferContext)=>void)[]
11+
ctx.defer = (fnToDefer) => {
12+
funcsToDefer.push(fnToDefer)
13+
}
14+
return {
15+
onFinally() {
16+
for (const defer of funcsToDefer) {
17+
defer(ctx);
18+
}
19+
}
20+
}
21+
}
22+
}

src/example/logOnFinally.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/example/tracker.test.ts

Lines changed: 0 additions & 160 deletions
This file was deleted.

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export * from './tryCatchFinally'
1+
export * from './tryCatchFinally'
2+
export * from './TryCatchFinallyHooks'
3+
export * from './defer'
4+
export * from './measureDuration'
5+
export * from './callStack'

src/measureDuration.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { TryCatchFinallyHooksBuilder } from "./TryCatchFinallyHooks";
2+
import { measureDuration } from "./measureDuration";
3+
4+
test("measure duration", async() => {
5+
const actualDuration = 500;
6+
7+
const track = new TryCatchFinallyHooksBuilder()
8+
.add(measureDuration)
9+
//test
10+
.add(ctx=>{
11+
return {
12+
onFinally() {
13+
expect(ctx.duration).toBeGreaterThanOrEqual(actualDuration)
14+
}
15+
}
16+
})
17+
;
18+
const myTrackedFunction = track.asFunctionWrapper({ name: 'MyAction' })(
19+
async function myFunction(a: number, b: number) {
20+
await delay(actualDuration);
21+
return a + b;
22+
}
23+
);
24+
25+
const res = await myTrackedFunction(1, 2);
26+
expect(res).toBe(3);
27+
})
28+
29+
async function delay(ms: number) {
30+
return new Promise<void>(resolve => setTimeout(resolve, ms));
31+
}

src/example/tracker.ts renamed to src/specs/combine-hooks.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
import { callStack } from "../callStack";
2-
import { logOnFinally } from "./logOnFinally";
32
import { measureDuration } from "../measureDuration";
4-
import { TryCatchFinallyHooksBuilder, ContextOf } from "../TryCatchFinallyHooks";
3+
import { TryCatchFinallyHooksBuilder, ITryCatchFinallyHook } from "../TryCatchFinallyHooks";
4+
5+
6+
export const logOnFinally: ITryCatchFinallyHook<{ args: { name?: string; }; name:string }> = {
7+
onTry(ctx){
8+
if(!ctx.name) ctx.name = ctx.args.name || ctx.func.name || '<anonymous>'
9+
return {
10+
onFinally() {
11+
console.log(`Function ${ctx.args.name} finished!`);
12+
},
13+
};
14+
}};
15+
516

617
const track = new TryCatchFinallyHooksBuilder()
718
.add(callStack)
819
.add(measureDuration)
920
.add(logOnFinally)
10-
.createAndAdd(ctx => {
21+
.add(ctx => {
1122
return {
1223
onFinally() {
1324
const datadog: any = {};
1425
datadog.histogram(`action-${ctx.args.name}-duration`, ctx.duration, { tags: ['action:' + ctx.name, 'callstack:' + ctx.getCallStack().map(c => c.name).join('/')] });
1526
},
1627
};
1728
})
18-
.createAndAdd((ctx) => {
29+
.add((ctx) => {
1930
type DeferContext = typeof ctx
2031
const funcsToDefer = [] as ((_ctx:DeferContext) => void)[]
2132
return {
@@ -46,3 +57,5 @@ const myTrackedFunction = track.asFunctionWrapper({ name: 'MyAction' })(
4657
return res;
4758
}
4859
);
60+
61+
test.todo("combined tests")

0 commit comments

Comments
 (0)