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

Why AdvicePool, adviceMetadata, IMetadata are not exported from kaop-ts? #89

Closed
kailashthakuri opened this issue Apr 16, 2018 · 6 comments

Comments

@kailashthakuri
Copy link

kailashthakuri commented Apr 16, 2018

kaop

I have installed kaop-ts. Is there any other dependencies or package i have to install along with kaop-ts?

@k1r0s
Copy link
Owner

k1r0s commented Apr 16, 2018

Thanks @kailashthakuri

You're using an outdated API, please refer to the docs https://github.com/k1r0s/kaop-ts/blob/master/docs/api.md#how-do-i-define-an-advice

You no longer need to import AdvicePool, adviceMetadata, etc

May I know where did you find that code snippet? need to update it

@k1r0s
Copy link
Owner

k1r0s commented Apr 16, 2018

@kailashthakuri try this code snippet

const { beforeMethod } = require(".");

const yourAdvice = meta => {
  console.log(meta.target)
  console.log(meta.args)
}

class YourService {
  @beforeMethod(yourAdvice)
  static someMethod(num, str) {

  }
}

YourService.someMethod(1, "asdasd");

@kailashthakuri
Copy link
Author

Thanks, But I just want to track which component and method get called for event tracking. I don't want to send parameter to my advice. i am trying to implement AOP in angular5. I want to intercept the method called and keep log for event tracking purpose.

@k1r0s
Copy link
Owner

k1r0s commented Apr 16, 2018

mmm okay,

try this out:

const { beforeMethod } = require("kaop-ts");

const Log = meta => {
  console.log(meta.key)
  console.log(meta.scope)
  console.log(meta.args)
}

function applyToAll(advice) {
  return function(target) {
    const wove = beforeMethod(advice);
    for (let key in target.prototype) {
      Object.defineProperty(target.prototype, key,
        wove(target, key, Object.getOwnPropertyDescriptor(target.prototype, key)));
    }
  }

}

const track = applyToAll(Log)

@track
class YourService {
  method1(num, str) {

  }
  method2(num, str) {

  }
  method3(num, str) {

  }
  method4(num, str) {

  }
}

const b = new YourService;
const a = new YourService;

b.method1(1, 123)
a.method2("asdas", 131231)
a.method4({asd: 1}, 131231)

@kailashthakuri
Copy link
Author

kailashthakuri commented Apr 16, 2018

Thanks a lot.
Here. I am using using annotation in method level.

(k1r0s) I edited your comment to put syntax highlight

export class SigninComponent implements OnInit {
  constructor(private store: Store<fromApp.AppState>, private eventTrackingService: EventTrackingService) { }
  ngOnInit() {
  }
  @log()
  submitForm(form: NgForm) {
    let userName = form.value.userName;
    let password = form.value.password;
    this.store.dispatch(new AuthActions.trySignin({ username: userName, password: password }));
  }
}

log.service.ts
import {  beforeMethod } from "kaop-ts";

export const log = () => beforeMethod(meta => {
 console.log("Method Name : "+ meta.key);
 console.log("Class : "+  meta.target);
})

Here how can i get component Name? meta.target is not returning component name.

@k1r0s
Copy link
Owner

k1r0s commented Apr 16, 2018

console.log(meta.target.name) should do the trick

https://github.com/k1r0s/kaop-ts/blob/master/docs/api.md#metadata

as pointed in the docs:

meta.target // Class definition

@kailashthakuri going to close this since questions are out of issue's scope.

Thanks!

@k1r0s k1r0s closed this as completed Apr 16, 2018
@k1r0s k1r0s changed the title Why AdvicePool, adviceMetadata, IMetadata is not imported from kaop-ts? Why AdvicePool, adviceMetadata, IMetadata are not exported from kaop-ts? Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants