[go: up one dir, main page]

Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

phl3x0r/ng-disposable

Repository files navigation

ng-disposable

Description

ng-disposable is a small library that lets you implement a disposable interface and add it to a list of services in order to tear down multiple services at once on a given condition.

Useful if you manage subscriptions or other state inside services that should be cleaned up simultanously e.g. on logout

Simply implement the Disposable interface and add the service to the DISPOSABLES injection token list using multi: true Then invkode the dispose function on the provided DisposableService

Usage

Implement the Disposable interface:

export class MyService implements Disposable {
  public dispose() {
    // tear down
  }
}

Add your disposable service to the module:

@NgModule({
  imports: [DisposableModule.forRoot()],
  providers: [
    MyService,
    {
      provide: DISPOABLES,
      useExisting: MyService,
      multi: true
    }
  ]
})
export class MyModule {}

use the DisposableService to tear down all disposables:

export class AppComponent {
  constructor(private service: DisposableService) {}

  public logout() {
    this.service.dispose();
  }
}

Contribute