-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
469 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
interface CallStack { | ||
depth: number; | ||
} | ||
|
||
export type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
|
||
export type Context<T> = { | ||
proceed(): Promise<void> | void; | ||
} & T; | ||
|
||
export type PureContext<T> = Optional<Context<T>, 'proceed'>; | ||
|
||
export interface Composed<C> { | ||
(ctx: PureContext<C>): Promise<void> | void; | ||
} | ||
|
||
export interface Middleware<C> { | ||
(ctx: Context<C>): Promise<void> | void; | ||
} | ||
|
||
function dispatch<C>( | ||
middlewareList: Middleware<C>[], | ||
idx: number, | ||
stack: CallStack, | ||
ctx: PureContext<C>, | ||
): Promise<void> | void { | ||
if (idx <= stack.depth) { | ||
throw new Error('ctx.proceed() called multiple times'); | ||
} | ||
|
||
stack.depth = idx; | ||
|
||
let maybePromise: Promise<void> | void; | ||
|
||
if (idx < middlewareList.length) { | ||
const middleware = middlewareList[idx]; | ||
|
||
maybePromise = middleware({ | ||
...ctx, | ||
proceed: () => dispatch(middlewareList, idx + 1, stack, ctx), | ||
} as Context<C>); | ||
} else if (ctx.proceed) { | ||
maybePromise = ctx.proceed(); | ||
} | ||
|
||
return maybePromise; | ||
} | ||
|
||
export default function compose<C>(middlewareList: Middleware<C>[]): Composed<C> { | ||
return (ctx) => { | ||
const stack: CallStack = { depth: -1 }; | ||
return dispatch<C>(middlewareList, 0, stack, ctx); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Injectable, Aspect, Around, IAroundJoinPoint, Injector } from '../src'; | ||
|
||
describe('aspect', () => { | ||
jest.setTimeout(50 * 1000); | ||
|
||
it('test around hook: union model1', async () => { | ||
function delay(value: number, time: number): Promise<number> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(value); | ||
}, time); | ||
}); | ||
} | ||
|
||
@Injectable() | ||
class TestClass { | ||
async add(a: number, b: number): Promise<number> { | ||
const data = await delay(a + b, 1000); | ||
console.log('TestClass add result', data); | ||
return data; | ||
} | ||
} | ||
|
||
@Aspect() | ||
@Injectable() | ||
class TestAspect { | ||
@Around<TestClass, [number, number], number>(TestClass, 'add', { await: true }) | ||
async interceptAdd(joinPoint: IAroundJoinPoint<TestClass, [number, number], number>) { | ||
expect(joinPoint.getMethodName()).toBe('add'); | ||
expect(joinPoint.getOriginalArgs()).toBeInstanceOf(Array); | ||
expect(joinPoint.getThis()).toBeInstanceOf(TestClass); | ||
await joinPoint.proceed(); | ||
const result = await joinPoint.getResult(); | ||
console.log('TestAspect', result); | ||
} | ||
} | ||
|
||
@Aspect() | ||
@Injectable() | ||
class TestAspect2 { | ||
@Around<TestClass, [number, number], number>(TestClass, 'add', { await: true }) | ||
async interceptAdd(joinPoint: IAroundJoinPoint<TestClass, [number, number], number>) { | ||
const other = await delay(10, 1000); | ||
console.log('TestAspect2 async', other); | ||
expect(joinPoint.getMethodName()).toBe('add'); | ||
expect(joinPoint.getOriginalArgs()).toBeInstanceOf(Array); | ||
expect(joinPoint.getThis()).toBeInstanceOf(TestClass); | ||
await joinPoint.proceed(); | ||
const result = await joinPoint.getResult(); | ||
console.log('TestAspect2', result); | ||
} | ||
} | ||
|
||
const injector = new Injector(); | ||
injector.addProviders(TestClass); | ||
injector.addProviders(TestAspect); | ||
injector.addProviders(TestAspect2); | ||
|
||
const testClass = injector.get(TestClass); | ||
|
||
const result = await testClass.add(1, 2); | ||
console.log('TestClass invoke result', result); | ||
|
||
expect(result).toBe(3); | ||
}); | ||
|
||
it('test union model: union model2', async () => { | ||
function delay(value: number, time: number): Promise<number> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(value); | ||
}, time); | ||
}); | ||
} | ||
|
||
@Injectable() | ||
class TestClass { | ||
async add(a: number, b: number): Promise<number> { | ||
const data = await delay(a + b, 1000); | ||
console.log('TestClass add result', data); | ||
return data; | ||
} | ||
} | ||
|
||
@Aspect() | ||
@Injectable() | ||
class TestAspect { | ||
@Around<TestClass, [number, number], number>(TestClass, 'add', { await: true }) | ||
async interceptAdd(joinPoint: IAroundJoinPoint<TestClass, [number, number], number>) { | ||
expect(joinPoint.getMethodName()).toBe('add'); | ||
expect(joinPoint.getOriginalArgs()).toBeInstanceOf(Array); | ||
expect(joinPoint.getThis()).toBeInstanceOf(TestClass); | ||
joinPoint.proceed(); | ||
const result = await joinPoint.getResult(); | ||
console.log('TestAspect', result); | ||
} | ||
} | ||
|
||
@Aspect() | ||
@Injectable() | ||
class TestAspect2 { | ||
@Around<TestClass, [number, number], number>(TestClass, 'add', { await: true }) | ||
async interceptAdd(joinPoint: IAroundJoinPoint<TestClass, [number, number], number>) { | ||
const other = await delay(10, 1000); | ||
console.log('TestAspect2 async', other); | ||
expect(joinPoint.getMethodName()).toBe('add'); | ||
expect(joinPoint.getOriginalArgs()).toBeInstanceOf(Array); | ||
expect(joinPoint.getThis()).toBeInstanceOf(TestClass); | ||
joinPoint.proceed(); | ||
const result = await joinPoint.getResult(); | ||
console.log('TestAspect2', result); | ||
} | ||
} | ||
|
||
const injector = new Injector(); | ||
injector.addProviders(TestClass); | ||
injector.addProviders(TestAspect); | ||
injector.addProviders(TestAspect2); | ||
|
||
const testClass = injector.get(TestClass); | ||
|
||
const result = await testClass.add(1, 2); | ||
console.log('TestClass invoke result', result); | ||
|
||
expect(result).toBe(3); | ||
}); | ||
}); |
Oops, something went wrong.