Skip to content

Commit

Permalink
offer aspectj-like around advice via new MethodMetadata.complete meth…
Browse files Browse the repository at this point in the history
…od (#76)

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

* bump minor (to 0.8.0)

* add test

* fix typo

* update package-lock.json
  • Loading branch information
matthewadams authored and mgechev committed Nov 15, 2018
1 parent a58040b commit 1c03a12
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
18 changes: 13 additions & 5 deletions lib/src/advices/sync_advices.ts
@@ -1,5 +1,5 @@
import { Advice } from '../core/advice';
import { Metadata } from '../core/metadata';
import { Advice } from "../core/advice";
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
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
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-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -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
@@ -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);
});
});

0 comments on commit 1c03a12

Please sign in to comment.