Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offer aspectj-like around advice via new MethodMetadata.complete method #76

Merged
merged 5 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/src/advices/sync_advices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Advice } from '../core/advice';
import { Metadata } from '../core/metadata';
import { Advice } from "../core/advice";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you revert back to single quotes.

import { Metadata } from "../core/metadata";

export class BeforeAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
Expand All @@ -19,9 +19,17 @@ export class AfterAdvice extends Advice {

export class AroundAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
// if the user called metadata.method.complete(), then he's handling return value himself;
// else he called metadata.method.invoke() & we'll handle it unless metadata.method.proceed is false
const result = this.advice.bind(this.context, metadata).apply(null, metadata.method.args);

if (metadata.method.proceed) {
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
} else {
metadata.method.result = result;
}

return metadata.method.result;
}
}
Expand Down
10 changes: 9 additions & 1 deletion lib/src/core/join_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ export abstract class JoinPoint {
context: any,
woveMetadata: any
): Metadata {
const boundFn = fn.bind(context)

var invocation: MethodMetadata = {
name: key,
proceed: true,
context: context,
result: undefined,
exception: undefined,
args: undefined,
invoke: fn.bind(context),
invoke: boundFn,
complete: function complete(...args: any[]): any {
this.method.proceed = false;
return boundFn.apply(null, args);
}
};

var metadata: Metadata = new Metadata();
metadata.method = invocation;
metadata.className = className;
Expand All @@ -53,6 +60,7 @@ export abstract class JoinPoint {
metadata.method.context = previousMetadata.method.context;
} else {
metadata.method.args = Array.prototype.slice.call(args);
metadata.method.complete = metadata.method.complete.bind(metadata)
}
return metadata;
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class MethodMetadata {
public result: any;
public exception: any;
public invoke: (...args: any[]) => any;
public complete: (...args: any[]) => any;
}

export class Metadata {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aspect.js",
"version": "0.7.5",
"version": "0.8.0",
"description": "Library for aspect-oriented programming with JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
33 changes: 33 additions & 0 deletions test/advices/around_advice_using_complete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Wove, resetRegistry, Metadata } from "../../lib/src/core";

import { expect } from "chai";
import { aroundMethod } from "../../lib";

describe("around advices", () => {
afterEach(() => {
resetRegistry();
});

it("should only get invoked once when using MethodMetadata.complete()", () => {
let count = 0;

class Aspect {
@aroundMethod({ classNamePattern: /^Test/, methodNamePattern: /^get$/ })
aroundMethod(meta: Metadata) {
return meta.method.complete(...meta.method.args) + " appended from the aspect";
}
}

@Wove()
class Test {
get(it: string): string {
count++;
return it;
}
}

const test = new Test();
expect(test.get("test")).to.equal("test appended from the aspect");
expect(count).to.equal(1);
});
});