Skip to content

Commit

Permalink
docs: add demo with aspect in external file
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Oct 10, 2018
1 parent 5b5acbc commit 265462a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
14 changes: 14 additions & 0 deletions demo/aspect.ts
@@ -0,0 +1,14 @@
import { aroundMethod, Metadata } from '../lib/index';

export default class ExternalAspect {
@aroundMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
around(metadata: Metadata) {
console.assert(metadata.className === 'Demo');
console.assert(metadata.method.name === 'get');

metadata.method.proceed = false;
metadata.method.result = 'ExternalAspect';

console.log('aspect');
}
}
12 changes: 12 additions & 0 deletions demo/class.ts
@@ -0,0 +1,12 @@
import './aspect';
import { Wove } from '../lib/index';

@Wove()
class Demo {
get(foo: any, bar: any): string {
return 'Demo';
}
}

const d = new Demo();
d.get(1, 2);
22 changes: 11 additions & 11 deletions lib/src/join_points/method_call.ts
Expand Up @@ -17,9 +17,9 @@ export class MethodCallJoinPoint extends JoinPoint {
keys = keys.filter(key => {
return BLACK_LIST.indexOf(key) < 0;
});
let res = keys
const res = keys
.map(key => {
let descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
const descriptor = Object.getOwnPropertyDescriptor(target.prototype, key);
if (
this.precondition.assert({
classDefinition: target,
Expand All @@ -36,11 +36,11 @@ export class MethodCallJoinPoint extends JoinPoint {
}

protected woveTarget(proto: { [key: string]: any }, key: string, advice: Advice, woveMetadata: any) {
let className = proto.constructor.name;
let bak = proto[key];
let self = this;
const className = proto.constructor.name;
const bak = proto[key];
const self = this;
proto[key] = function() {
let metadata = self.getMetadata(className, key, bak, arguments, this, woveMetadata);
const metadata = self.getMetadata(className, key, bak, arguments, this, woveMetadata);
return advice.wove(bak, metadata);
};
proto[key].__woven__ = true;
Expand All @@ -50,14 +50,14 @@ export class MethodCallJoinPoint extends JoinPoint {
export function makeMethodCallAdviceDecorator(constr: any) {
return function(...selectors: MethodSelector[]): MethodDecorator {
return function<T>(target: Object, prop: symbol | string, descriptor: TypedPropertyDescriptor<T>) {
let joinpoints = selectors.map(selector => {
const joinpoints = selectors.map(selector => {
return new MethodCallJoinPoint(new MethodPrecondition(selector));
});
let pointcut = new Pointcut();
const pointcut = new Pointcut();
pointcut.advice = <Advice>new constr(target, descriptor.value);
pointcut.joinPoints = joinpoints;
let aspectName = target.constructor.name;
let aspect = AspectRegistry.get(aspectName) || new Aspect();
const aspectName = target.constructor.name;
const aspect = AspectRegistry.get(aspectName) || new Aspect();
aspect.pointcuts.push(pointcut);
AspectRegistry.set(aspectName, aspect);
// For lazy loading
Expand All @@ -76,6 +76,6 @@ export function makeMethodCallAdviceDecorator(constr: any) {
*/
export abstract class MethodCallJointPoint extends MethodCallJoinPoint {
constructor(precondition: Precondition) {
super(precondition)
super(precondition);
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -16,7 +16,8 @@
"test/core/pointcut.spec.ts",
"test/core/preconditions.spec.ts",
"test/core/wove.spec.ts",
"demo/index.ts"
"demo/index.ts",
"demo/class.ts",

This comment has been minimized.

Copy link
@matthewadams

matthewadams Oct 10, 2018

Collaborator

The trailing comma on line 20 is breaking the CI build.

],
"angularCompilerOptions": {
"skipTemplateCodegen": true
Expand Down

1 comment on commit 265462a

@matthewadams
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just submitted PR #66 to prove this.

Please sign in to comment.