k1r0s/kaop-ts

Async advices

Closed this issue · 1 comments

Hello

Experimenting a little with your library. Specifically, Async advices. You had mentioned for Async you will have to meta.commit() to prevent it from handing. I am attempting to do an http call (using static methods?) and passing in the httpclient and meta to be used in the static method. Doesn’t seem to like my approach. I seem to be missing something?

export class MyServiceService {

  constructor(private http: HttpClient, private meta: Meta) { }
  
  @beforeMethod(Permissions.fetch(this.http, this.meta), Permissions.transform)
  @afterMethod(LogAspect.log)
  getCustomers(): void {
    console.log("Doing getCustomers");
  }
}


export class Permissions {

static fetch = (injected: any, meta: any) => {
        let httpClient = injected as HttpClient;
        if (httpClient) {
            httpClient.get<any>('https://api.npms.io/v2/search?q=scope:angular').subscribe(data => {
                console.log("HTTP CALL DATA::::::: ", data.total);
            })
        }
        meta.commit() // It must be called and reachable, otherwise the flow hangs
    }
Error:

ERROR in src/app/service/my-service.service.ts:24:17 - error TS2345: Argument of type 'void' is not assignable to parameter of type 'AdviceRef<any>'.
24   @beforeMethod(Permissions.fetch(this.http, this.meta), Permissions.transform)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~src/app/service/my-service.service.ts:24:35 - error TS2532: Object is possibly 'undefined'.
24   @beforeMethod(Permissions.fetch(this.http, this.meta), Permissions.transform)
~~~~src/app/service/my-service.service.ts:24:46 - error TS2532: Object is possibly 'undefined'.
24   @beforeMethod(Permissions.fetch(this.http, this.meta), Permissions.transform)

k1r0s commented

Your code is wrong.

Please read the documentation carefully and use the examples.

meta argument is injected by the library, You don't have to do this.meta on the advice call. If you need to use a service from the subject you just use an interface to inform the compiler that @beforeMethod was used on an instance that meets specific criteria.

Please check this repo: https://github.com/k1r0s/angular2-srp-showcase/ It was done with an old angular version but here I've done many samples of async behaviors working with angular.

In any case your code does not follow the documentation by any means, you're using the library wrong.