Skip to content

Commit

Permalink
refactor: rename wove to advised
Browse files Browse the repository at this point in the history
Fix #67
  • Loading branch information
mgechev committed Nov 18, 2018
1 parent b3a51e9 commit ffa52fa
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 81 deletions.
11 changes: 5 additions & 6 deletions README.md
Expand Up @@ -2,7 +2,6 @@

[![Build Status](https://travis-ci.org/mgechev/aspect.js.svg?branch=master)](https://travis-ci.org/mgechev/aspect.js)


Library for aspect-oriented programming with JavaScript, which takes advantage of ECMAScript 2016 decorators syntax.

> NOTE: if you are using `aspect.js` in a plain JavaScript project that uses [`@babel/plugin-proposal-decorators`](https://www.npmjs.com/package/@babel/plugin-proposal-decorators), you _must_ set its `legacy` property to `true` until [#72](https://github.com/mgechev/aspect.js/issues/72) is fixed.
Expand All @@ -29,7 +28,7 @@ class LoggerAspect {
methodNamePattern: /^(get|set)/
})
invokeBeforeMethod(meta: Metadata) {
// meta.woveMetadata == { bar: 42 }
// meta.advisedMetadata == { bar: 42 }
console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
}
}
Expand All @@ -40,7 +39,7 @@ class Article {
content: string;
}

@Wove({ bar: 42 })
@Advised({ bar: 42 })
class ArticleCollection {
articles: Article[] = [];
getArticle(id: number) {
Expand Down Expand Up @@ -71,7 +70,7 @@ class LoggerAspect {
methods: [ArticleCollection.prototype.getArticle, ArticleCollection.prototype.setArticle]
})
invokeBeforeMethod(meta: Metadata) {
// meta.woveMetadata == { bar: 42 }
// meta.advisedMetadata == { bar: 42 }
console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
}
}
Expand All @@ -82,7 +81,7 @@ class ArticleCollection {
}
```

In this case you can omit the `@Wove` decorator.
In this case you can omit the `@Advised` decorator.

This way, by explicitly listing the classes and methods which should be woven, you can prevent the unwanted effect of mangling.

Expand Down Expand Up @@ -155,7 +154,7 @@ export interface PropertySelector {
export class Metadata {
public method: MethodMetadata;
public className: string;
public woveMetadata: any;
public advisedMetadata: any;
}
```

Expand Down
4 changes: 2 additions & 2 deletions demo/class.ts
@@ -1,7 +1,7 @@
import './aspect';
import { Wove } from '../lib/index';
import { Advised } from '../lib/index';

@Wove()
@Advised()
class Demo {
get(foo: any, bar: any): string {
return 'Demo';
Expand Down
6 changes: 3 additions & 3 deletions demo/index.ts
@@ -1,5 +1,5 @@
import {
Wove,
Advised,
Metadata,
MethodMetadata,
beforeMethod,
Expand Down Expand Up @@ -50,7 +50,7 @@ class CacheAspect {
// }
// }

@Wove()
@Advised()
class Http {
get(url: string) {
console.log(`Called Http.get with url = ${url}`);
Expand All @@ -60,7 +60,7 @@ class Http {
}
}

@Wove()
@Advised()
class UserMapper {
constructor(private http: Http) {}
get(id: number) {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Expand Up @@ -3,7 +3,7 @@ import { BeforeAdvice, AroundAdvice, AfterAdvice, OnThrowAdvice, AsyncOnThrowAdv
import { makeMethodCallAdviceDecorator, makeStaticMethodAdviceDecorator } from './src/join_points';
import { makeFieldGetAdviceDecorator, makeFieldSetAdviceDecorator } from './src/join_points';

export { Wove, Metadata, MethodMetadata, AspectRegistry as _AspectRegistry, Targets as _Targets } from './src/core';
export { Advised, Metadata, MethodMetadata, AspectRegistry as _AspectRegistry, Targets as _Targets } from './src/core';
export { MemberPrecondition } from './src/join_points';

export const beforeMethod = makeMethodCallAdviceDecorator(BeforeAdvice);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/advices/async_advices.ts
Expand Up @@ -2,7 +2,7 @@ import { AsyncAdvice } from '../core/advice';
import { Metadata } from '../core/metadata';

export class AsyncOnThrowAdvice extends AsyncAdvice {
async wove(target: Function, metadata: Metadata) {
async apply(target: Function, metadata: Metadata) {
try {
await this.invoke(target, metadata);
} catch (e) {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/advices/sync_advices.ts
Expand Up @@ -2,23 +2,23 @@ import { Advice } from '../core/advice';
import { Metadata } from '../core/metadata';

export class BeforeAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
apply(target: Function, metadata: Metadata) {
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
this.invoke(target, metadata);
return metadata.method.result;
}
}

export class AfterAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
apply(target: Function, metadata: Metadata) {
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
return metadata.method.result;
}
}

export class AroundAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
apply(target: Function, metadata: Metadata) {
// 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);
Expand All @@ -35,7 +35,7 @@ export class AroundAdvice extends Advice {
}

export class OnThrowAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
apply(target: Function, metadata: Metadata) {
try {
this.invoke(target, metadata);
} catch (e) {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core.ts
Expand Up @@ -3,5 +3,4 @@ export * from './core/aspect';
export * from './core/join_point';
export * from './core/metadata';
export * from './core/pointcut';
export * from './core/wove';

export * from './core/advised';
4 changes: 2 additions & 2 deletions lib/src/core/advice.ts
Expand Up @@ -3,7 +3,7 @@ import { Metadata } from './metadata';
export abstract class Advice {
constructor(public context: Object, public advice: Function) {}

public abstract wove(target: Function, metadata: Metadata): void;
public abstract apply(target: Function, metadata: Metadata): void;

public invoke(target: any, metadata: Metadata) {
if (target.__woven__) {
Expand All @@ -21,7 +21,7 @@ export abstract class Advice {
export abstract class AsyncAdvice {
constructor(public context: Object, public advice: Function) {}

public abstract async wove<T>(target: Function, metadata: Metadata): Promise<T>;
public abstract async apply<T>(target: Function, metadata: Metadata): Promise<T>;

public async invoke(target: any, metadata: Metadata) {
if (target.__woven__) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/wove.ts → lib/src/core/advised.ts
Expand Up @@ -6,15 +6,15 @@ export function weave<TFunction extends Function>(target: TFunction, config?: an
}

for (const aspect of Array.from(AspectRegistry.values())) {
aspect.wove(target, config);
aspect.apply(target, config);
}

Targets.add({ target, config });
(target as any).__woven__ = true;
return target;
}

export function Wove(config?: any): ClassDecorator {
export function Advised(config?: any): ClassDecorator {
return function<TFunction extends Function>(target: TFunction) {
return weave(target, config);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/aspect.ts
Expand Up @@ -16,9 +16,9 @@ export class Aspect {
this.pointcuts = [];
}

public wove(target: Function, woveMetadata: any) {
public apply(target: Function, metadata: any) {
this.pointcuts.forEach(p => {
p.apply(target, woveMetadata);
p.apply(target, metadata);
});
}
}
18 changes: 9 additions & 9 deletions lib/src/core/join_point.ts
Expand Up @@ -12,15 +12,15 @@ export abstract class JoinPoint {

protected abstract getTarget(fn: Function): Object;

protected abstract woveTarget(target: Object, match: string, advice: Advice, woveMetadata: any): void;
protected abstract wrapTarget(target: Object, match: string, advice: Advice, advisedMetadata: any): void;

public wove(
{ fn, matches, woveMetadata }: { fn: Function; matches: string[]; woveMetadata: any },
public apply(
{ fn, matches, advisedMetadata }: { fn: Function; matches: string[]; advisedMetadata: any },
advice: Advice
): void {
const target = this.getTarget(fn);
matches.forEach((match: string) => {
this.woveTarget(target, match, advice, woveMetadata);
this.wrapTarget(target, match, advice, advisedMetadata);
});
}

Expand All @@ -30,9 +30,9 @@ export abstract class JoinPoint {
fn: Function,
args: IArguments,
context: any,
woveMetadata: any
advisedMetadata: any
): Metadata {
const boundFn = fn.bind(context)
const boundFn = fn.bind(context);

var invocation: MethodMetadata = {
name: key,
Expand All @@ -51,7 +51,7 @@ export abstract class JoinPoint {
var metadata: Metadata = new Metadata();
metadata.method = invocation;
metadata.className = className;
metadata.woveMetadata = woveMetadata;
metadata.advisedMetadata = advisedMetadata;
if (args[0] && args[0].__advice_metadata__) {
let previousMetadata = <Metadata>args[0];
metadata.method.result = previousMetadata.method.result;
Expand All @@ -60,7 +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)
metadata.method.complete = metadata.method.complete.bind(metadata);
}
return metadata;
}
Expand All @@ -75,6 +75,6 @@ export abstract class JoinPoint {
*/
export abstract class JointPoint extends JoinPoint {
constructor(public precondition: Precondition) {
super(precondition)
super(precondition);
}
}
2 changes: 1 addition & 1 deletion lib/src/core/metadata.ts
Expand Up @@ -11,7 +11,7 @@ export class MethodMetadata {

export class Metadata {
public method: MethodMetadata;
public woveMetadata: any;
public advisedMetadata: any;
public className: string;
private __advice_metadata__ = true;
}
4 changes: 2 additions & 2 deletions lib/src/core/pointcut.ts
Expand Up @@ -6,11 +6,11 @@ export class Pointcut {
public advice: Advice;
private _applications = new Set<Function>();

public apply(fn: Function, woveMetadata: any) {
public apply(fn: Function, advisedMetadata: any) {
if (this._applications.has(fn)) {
return;
}
this._applications.add(fn);
this.joinPoints.forEach(jp => jp.wove({ fn, matches: jp.match(fn), woveMetadata }, this.advice));
this.joinPoints.forEach(jp => jp.apply({ fn, matches: jp.match(fn), advisedMetadata }, this.advice));
}
}
8 changes: 4 additions & 4 deletions lib/src/join_points/accessor_use.ts
Expand Up @@ -16,15 +16,15 @@ export class AccessorJoinPoint extends JoinPoint {
return fn.prototype;
}

protected woveTarget(proto: any, key: string, advice: Advice, woveMetadata: any) {
protected wrapTarget(proto: any, key: string, advice: Advice, advisedMetadata: any) {
const className = proto.constructor.name;
const self = this;
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
if ((this.type === 'get' || this.type === 'set') && typeof descriptor[this.type] === 'function') {
const bak = descriptor[this.type];
descriptor[this.type] = function() {
const metadata = self.getMetadata(className, key, bak, arguments, this, woveMetadata);
return advice.wove(bak, metadata);
const metadata = self.getMetadata(className, key, bak, arguments, this, advisedMetadata);
return advice.apply(bak, metadata);
};
(descriptor[this.type] as any)['__woven__'] = true;
Object.defineProperty(proto, key, descriptor);
Expand Down Expand Up @@ -80,7 +80,7 @@ export function makeFieldSetAdviceDecorator(constr: new (...args: any[]) => Advi
const aspect = AspectRegistry.get(aspectName) || new Aspect();
aspect.pointcuts.push(pointcut);
AspectRegistry.set(aspectName, aspect);
Targets.forEach(({ target, config }) => aspect.wove(target, config));
Targets.forEach(({ target, config }) => aspect.apply(target, config));
return target;
};
};
Expand Down
8 changes: 4 additions & 4 deletions lib/src/join_points/method_call.ts
Expand Up @@ -35,13 +35,13 @@ export class MethodCallJoinPoint extends JoinPoint {
return res;
}

protected woveTarget(proto: { [key: string]: any }, key: string, advice: Advice, woveMetadata: any) {
protected wrapTarget(proto: { [key: string]: any }, key: string, advice: Advice, advisedMetadata: any) {
const className = proto.constructor.name;
const bak = proto[key];
const self = this;
proto[key] = function() {
const metadata = self.getMetadata(className, key, bak, arguments, this, woveMetadata);
return advice.wove(bak, metadata);
const metadata = self.getMetadata(className, key, bak, arguments, this, advisedMetadata);
return advice.apply(bak, metadata);
};
proto[key].__woven__ = true;
}
Expand All @@ -61,7 +61,7 @@ export function makeMethodCallAdviceDecorator(constr: any) {
aspect.pointcuts.push(pointcut);
AspectRegistry.set(aspectName, aspect);
// For lazy loading
Targets.forEach(({ target, config }) => aspect.wove(target, config));
Targets.forEach(({ target, config }) => aspect.apply(target, config));
return target;
};
};
Expand Down
2 changes: 1 addition & 1 deletion lib/src/join_points/preconditions.ts
@@ -1,6 +1,6 @@
import { Precondition } from '../core/join_point';
import { MethodSelector, PropertySelector } from './selectors';
import { weave } from '../core/wove';
import { weave } from '../core/advised';

export class MethodPrecondition implements Precondition {
constructor(private selector: MethodSelector) {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/join_points/static_method.ts
Expand Up @@ -21,20 +21,20 @@ export class StaticMethodJoinPoint extends JoinPoint {
return (
this.precondition.assert({
classDefinition: target,
methodName: key,
methodName: key
}) && typeof descriptor.value === 'function'
);
});
return res;
}

protected woveTarget(fn: any, key: string, advice: Advice, woveMetadata: any) {
protected wrapTarget(fn: any, key: string, advice: Advice, advisedMetadata: any) {
let className = fn.name;
let bak = fn[key];
let self = this;
fn[key] = function() {
let metadata = self.getMetadata(className, key, bak, arguments, this, woveMetadata);
return advice.wove(bak, metadata);
let metadata = self.getMetadata(className, key, bak, arguments, this, advisedMetadata);
return advice.apply(bak, metadata);
};
fn[key].__woven__ = true;
}
Expand All @@ -53,7 +53,7 @@ export function makeStaticMethodAdviceDecorator(constr: any) {
let aspect = AspectRegistry.get(aspectName) || new Aspect();
aspect.pointcuts.push(pointcut);
AspectRegistry.set(aspectName, aspect);
Targets.forEach(({ target, config }) => aspect.wove(target, config));
Targets.forEach(({ target, config }) => aspect.apply(target, config));
return target;
};
};
Expand All @@ -68,6 +68,6 @@ export function makeStaticMethodAdviceDecorator(constr: any) {
*/
export abstract class StaticMethodJointPoint extends StaticMethodJoinPoint {
constructor(precondition: Precondition) {
super(precondition)
super(precondition);
}
}
4 changes: 2 additions & 2 deletions test/advices/around_advice_using_complete.spec.ts
@@ -1,4 +1,4 @@
import { Wove, resetRegistry, Metadata } from '../../lib/src/core';
import { Advised, resetRegistry, Metadata } from '../../lib/src/core';

import { expect } from 'chai';
import { aroundMethod } from '../../lib';
Expand All @@ -14,7 +14,7 @@ describe('around advices', () => {
}
}

@Wove()
@Advised()
class Test {
get(it: string): string {
count++;
Expand Down

0 comments on commit ffa52fa

Please sign in to comment.