mgechev / aspect.js

JavaScript library for aspect-oriented programming using modern syntax.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to retrieve the call chain / stack trace in the aspect?

andrashatvani opened this issue · comments

I've got a LoggerAspect, an HttpService and many components and services which use the HttpService. How can I retrieve the HttpService caller's name in the LoggerAspect?

You can find the caller of the HttpService without creating a specific instance of the HttpService for each individual consumer, or passing the consumer as argument.

If you have:

const HttpService = { get: () => {} }

const Consumer = {
  makeRequest() {
    HttpService.get();
  }
};

Consumer.makeRequest();

There's no way to know if Consumer delegated it's call to HttpService or not.

I've found https://www.stacktracejs.com/#!/docs/stacktrace-js - wouldn't that be an option?

console.trace is another option but you'll get the consumer's name as string, which won't work after mangling.