Skip to content

Commit

Permalink
refactor(core): MidwayAspectService.interceptPrototypeMethod() with p…
Browse files Browse the repository at this point in the history
…rocessAsync()
  • Loading branch information
waitingsong committed Nov 9, 2024
1 parent 30e3381 commit 67813b1
Showing 1 changed file with 62 additions and 38 deletions.
100 changes: 62 additions & 38 deletions packages/core/src/service/aspectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,47 +88,15 @@ export class MidwayAspectService {

if (Types.isAsyncFunction(Clz.prototype[methodName])) {
Clz.prototype[methodName] = async function (...args) {
let error, result;
const newProceed = (...args) => {
return originMethod.apply(this, args);
};
const joinPoint = {
const opts: ProcessAsyncOptions = {
args,
aspectObjectInput: aspectObject,
methodName,
originMethod,
target: this,
args: args,
proceed: newProceed,
proceedIsAsyncFunction: true,
} as JoinPoint;

if (typeof aspectObject === 'function') {
aspectObject = aspectObject();
}

try {
await aspectObject.before?.(joinPoint);
if (aspectObject.around) {
result = await aspectObject.around(joinPoint);
} else {
result = await originMethod.call(this, ...joinPoint.args);
}
joinPoint.proceed = undefined;
const resultTemp = await aspectObject.afterReturn?.(
joinPoint,
result
);
result = typeof resultTemp === 'undefined' ? result : resultTemp;
return result;
} catch (err) {
joinPoint.proceed = undefined;
error = err;
if (aspectObject.afterThrow) {
await aspectObject.afterThrow(joinPoint, error);
} else {
throw err;
}
} finally {
await aspectObject.after?.(joinPoint, result, error);
}

Check failure on line 97 in packages/core/src/service/aspectService.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
const res = await processAsync(opts);
return res

Check failure on line 99 in packages/core/src/service/aspectService.ts

View workflow job for this annotation

GitHub Actions / lintAndTestLegacy (lts/*, ubuntu-latest)

Insert `;`
};
} else {
Clz.prototype[methodName] = function (...args) {
Expand Down Expand Up @@ -174,3 +142,59 @@ export class MidwayAspectService {
}
}
}

interface ProcessAsyncOptions {
args: unknown[];
aspectObjectInput: IMethodAspect | (() => IMethodAspect);
methodName: string;
originMethod: Function;
target: any;
}
async function processAsync(options: ProcessAsyncOptions): Promise<unknown> {
const { args, aspectObjectInput, originMethod, methodName, target } = options;

let error, result;
const newProceed = (...args2) => {
return originMethod.apply(this, args2);
};
const joinPoint = {
methodName,
target,
args,
proceed: newProceed,
proceedIsAsyncFunction: true,
} as JoinPoint;

let aspectObject: IMethodAspect;
if (typeof aspectObjectInput === 'function') {
aspectObject = aspectObjectInput();
} else {
aspectObject = aspectObjectInput;
}

try {
await aspectObject.before?.(joinPoint);
if (aspectObject.around) {
result = await aspectObject.around(joinPoint);
} else {
result = await originMethod.call(this, ...joinPoint.args);
}
joinPoint.proceed = undefined;
const resultTemp = await aspectObject.afterReturn?.(
joinPoint,
result
);
result = typeof resultTemp === 'undefined' ? result : resultTemp;
return result;
} catch (err) {
joinPoint.proceed = undefined;
error = err;
if (aspectObject.afterThrow) {
await aspectObject.afterThrow(joinPoint, error);
} else {
throw err;
}
} finally {
await aspectObject.after?.(joinPoint, result, error);
}
}

0 comments on commit 67813b1

Please sign in to comment.