-
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
6 changed files
with
390 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
interface CallStack { | ||
index: number; | ||
} | ||
|
||
export type Context<T> = { | ||
proceed(): Promise<void> | void; | ||
} & T; | ||
|
||
export type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
|
||
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; | ||
awaitPromise?: boolean; | ||
} | ||
|
||
function dispatch<C>( | ||
middlewareList: Middleware<C>[], | ||
depth: number, | ||
stack: CallStack, | ||
ctx: PureContext<C>, | ||
): Promise<void> | void { | ||
if (depth <= stack.index) { | ||
throw new Error('joinPoint.proceed() called multiple times'); | ||
} | ||
|
||
stack.index = depth; | ||
|
||
const { length } = middlewareList; | ||
|
||
let maybePromise: Promise<void> | void; | ||
if (depth <= length) { | ||
if (depth < length) { | ||
const middleware = middlewareList[depth]; | ||
|
||
maybePromise = middleware({ | ||
...ctx, | ||
proceed: () => { | ||
return dispatch(middlewareList, depth + 1, stack, ctx); | ||
}, | ||
} as Context<C>); | ||
|
||
if (middleware.awaitPromise) { | ||
maybePromise = Promise.resolve(maybePromise); | ||
} | ||
} else if (ctx.proceed) { | ||
// 这里可以不用 Promise.resolve | ||
// 但是为了兼容旧的表现,这里还是加上了 | ||
maybePromise = ctx.proceed(); | ||
} | ||
} | ||
|
||
return maybePromise; | ||
} | ||
|
||
export default function compose<C>(middlewareList: Middleware<C>[]): Composed<C> { | ||
return (ctx) => { | ||
const stack = { index: -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,140 @@ | ||
import { Injectable, Aspect, Around, IAroundJoinPoint, Injector } from '../src'; | ||
|
||
describe('aspect', () => { | ||
jest.setTimeout(1000 * 1000); | ||
/** | ||
* 下面的 case 目前输出: | ||
* TestAspect2 async 10 | ||
* 然后执行超时 | ||
*/ | ||
it('异步的hook异常, promise无法结束', 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); | ||
}); | ||
|
||
/** | ||
* 下面的 case 目前输出: | ||
* TestAspect2 async 10 | ||
* TestAspect2 undefined | ||
* TestClass invoke result undefined | ||
* 到这里单测就停了,其实后续会再执行 | ||
* TestAspect undefined | ||
* TestClass add result 3 | ||
*/ | ||
it('异步的hook异常, 等待的promise错误', 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); | ||
}); | ||
}); |
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,82 @@ | ||
import compose, { Middleware } from '../../src/helper/compose'; | ||
|
||
describe('di compose', () => { | ||
it('can worked', async () => { | ||
interface ExampleContext { | ||
getName(): string; | ||
getResult(): any; | ||
} | ||
|
||
const middleware1: Middleware<ExampleContext> = async (ctx) => { | ||
const name = ctx.getName(); | ||
console.log(`middleware1: ${name}`); | ||
await ctx.proceed(); | ||
const result = ctx.getResult(); | ||
console.log(`middleware1 result: ${result}`); | ||
console.log(`middleware1 after: ${name}`); | ||
}; | ||
|
||
const middleware2: Middleware<ExampleContext> = async (ctx) => { | ||
const name = ctx.getName(); | ||
console.log(`middleware2: ${name}`); | ||
await ctx.proceed(); | ||
const result = ctx.getResult(); | ||
console.log(`middleware2 result: ${result}`); | ||
console.log(`middleware2 after: ${name}`); | ||
}; | ||
|
||
const all = compose<ExampleContext>([middleware1, middleware2]); | ||
let ret = undefined as any; | ||
all({ | ||
getName() { | ||
return 'example'; | ||
}, | ||
async proceed() { | ||
console.log('invoked'); | ||
ret = 'final result'; | ||
}, | ||
getResult(): any { | ||
return ret; | ||
}, | ||
}); | ||
}); | ||
|
||
it('can worked with sync', async () => { | ||
interface ExampleContext { | ||
getName(): string; | ||
getResult(): any; | ||
} | ||
const middleware1: Middleware<ExampleContext> = async (ctx) => { | ||
const name = ctx.getName(); | ||
console.log(`middleware1: ${name}`); | ||
ctx.proceed(); | ||
const result = ctx.getResult(); | ||
console.log(`middleware1 result: ${result}`); | ||
console.log(`middleware1 after: ${name}`); | ||
}; | ||
|
||
const middleware2: Middleware<ExampleContext> = async (ctx) => { | ||
const name = ctx.getName(); | ||
console.log(`middleware2: ${name}`); | ||
ctx.proceed(); | ||
const result = ctx.getResult(); | ||
console.log(`middleware2 result: ${result}`); | ||
console.log(`middleware2 after: ${name}`); | ||
}; | ||
|
||
const all = compose<ExampleContext>([middleware1, middleware2]); | ||
let ret = undefined as any; | ||
all({ | ||
getName() { | ||
return 'example'; | ||
}, | ||
proceed() { | ||
console.log('invoked'); | ||
ret = 'final result'; | ||
}, | ||
getResult(): any { | ||
return ret; | ||
}, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.