simontonsoftware / s-libs

A collection of libraries for any of javascript, rxjs, or angular.
MIT License
43 stars 5 forks source link

[ng-core] Error with `mixInInjectableSuperclass()` using `manage()` #92

Closed Seynabou08 closed 1 year ago

Seynabou08 commented 1 year ago

The use of javascript's private definition in mixInInjectableSuperclass was causing an error when calling the class' manage method.

export function mixInSubscriptionManager<B extends Constructor>(Base: B) {
  return class extends Base implements Unsubscribable {
    private subscriptions = new Subscription();

    // eslint-disable-next-line max-params
    subscribeTo<T>(
      observable: Observable<T>,
      next: (value: T) => void = noop,
      error?: (error: any) => void,
      complete?: () => void,
    ): void {
      this.subscriptions.add(
        observable.subscribe({
          next: next.bind(this),
          error: error?.bind(this),
          complete: complete?.bind(this),
        }),
      );
    }

    manage(subscription: Subscription): void {
      this.subscriptions.add(subscription);
    }

    unsubscribe(): void {
      this.subscriptions.unsubscribe();
      this.subscriptions = new Subscription();
    }
  };
}
ersimont commented 1 year ago

So far I am unable to reproduce the issue. I tried running this code in a test in ng-core, in the test of a project that consumed ng-core, and in the prod code of a project that consumed ng-core.

    class InjectableDate extends mixInInjectableSuperclass(Date) {}
    const dateManager = new InjectableDate();
    dateManager.manage(new Subscription());

I know you're on Angular 12, and I'm testing with Angular 15. Maybe Typescript got better at dealing with private properties in between? If you can help reproduce I can try fixing it. But I also know you're in the process of upgrading up to Angular 14, so it may be easiest to try again on your end once you get there, then revisit if it's still broken.

Seynabou08 commented 1 year ago

Ok sounds good, thank you! I will link this issue to the PR for angular 14!