Skip to content

Commit

Permalink
fix: correct aspect chain bug (#1204)
Browse files Browse the repository at this point in the history
Co-authored-by: chenzhaozheng <chenzhaozheng@LAPTOP-GK4GS2RH.localdomain>
  • Loading branch information
chenzhaozheng and chenzhaozheng committed Aug 16, 2021
1 parent 1d20d64 commit 5de5284
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/service/aspectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isAsyncFunction,
isProxy,
listModule,
isClass,
} from '@midwayjs/decorator';
import * as pm from 'picomatch';
import { IAspectService, IMidwayContainer } from '../interface';
Expand Down Expand Up @@ -181,7 +182,11 @@ export class MidwayAspectService implements IAspectService {
}
const resultTemp = aspectIns.afterReturn?.(joinPoint, result);
result = typeof resultTemp === 'undefined' ? result : resultTemp;
return result;
if (result && isClass(result.constructor)) {
return this.wrapperAspectToInstance(ins);
} else {
return result;
}
} catch (err) {
error = err;
if (aspectIns.afterThrow) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ describe('/test/baseFramework.test.ts', () => {
} catch (err) {
expect(err.message).toMatch('ccc');
}
// aspect chain
const result = await userController1.test1().test2().getUser1();
expect(result).toEqual('before test user');
});

it('should inject global value in component', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/fixtures/base-app-aspect/src/aspect/c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export class MyAspect3 implements IMethodAspect {
throw new Error('ccc');
}
}
async before(point: JoinPoint) {
console.log('before around in aspect3');
console.log('before methodName:', point.methodName);
point.args = ['before test user'];
}
}
17 changes: 13 additions & 4 deletions packages/core/test/fixtures/base-app-aspect/src/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ class Parent {
}

async hello1() {
return 'hello world 1'
return 'hello world 1';
}
}

@Provide()
export class Home extends Parent {

bbb = 'aaa';

ccc: string;
Expand All @@ -28,14 +27,24 @@ export class Home extends Parent {
}
}


@Provide()
export class UserController {

@Inject()
ctx;

async getUser() {
throw new Error('bbb');
}

async getUser1(uid = 'user1') {
return uid;
}

test1() {
return this;
}

test2() {
return this;
}
}
4 changes: 2 additions & 2 deletions packages/ws/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class MidwayWSFramework extends BaseFramework<
server = this.configurationOptions.server ?? http.createServer();
}

server.on('upgrade', (request, socket, head) => {
this.app.handleUpgrade(request, socket as any, head, ws => {
server.on('upgrade', (request, socket: any, head) => {
this.app.handleUpgrade(request, socket, head, ws => {
this.app.emit('connection', ws, request);
});
});
Expand Down

0 comments on commit 5de5284

Please sign in to comment.