Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clean-decorators

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clean-decorators

Decorators to clean up observable subscriptions

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

clean-decorators

clean-decorators is a small utility library to cleanup subscriptions from observables on a method call.

It takes any property that has a unsubscribe method. If the property is undefined/null or has no unsubscribe method at the time of the cleanup. Nothing will happen.

Install

npm install clean-decorators 

About reflect-metadata

The library has a dependency on reflect-metadata but does not import it. You need to import it yourself.

@Cleanable()

@Cleanable defines a cleanable property.

@Clean()

@Clean unsubscribes every property defined by the @Cleanable decorators.

Example

class MyClass {

  @Cleanable() myProperty: any;
  @Cleanable() myProperty2: any;

  constructor () {
    this.myProperty = /* observable.subscribe(() => {}) */
    this.myProperty2 = /* observable2.subscribe(() => {}) */
  }

  @Clean()
  clean () {
    // Now myProperty and myProperty2 are unsubscribed
  }
}

Advanced use

@Cleanable() takes CleanableMetadata which is an object containing an id. By default, this id is an empty string. The @Clean() decorator also takes an id in his CleanMetadata options.

It can be useful if you need two categories of subscriptions that you want to unsubscribe in different methods.

Example

class MyClass {

  @Cleanable({ id: 'a' }) myProperty: any;
  @Cleanable({ id: 'b' }) myProperty2: any;

  constructor () {
    this.myProperty = /* observable.subscribe(() => {}) */
    this.myProperty2 = /* observable2.subscribe(() => {}) */
  }

  @Clean({ id: 'a' })
  cleanA () {
    // Now myProperty is unsubscribed
  }

  @Clean({ id: 'b' })
  cleanB () {
    // Now myProperty2 is unsubscribed
  }
}

The CleanMetadata decorator also takes a before parameter which defines if the decorator runs before the original method or otherwise.

FAQs

Package last updated on 25 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc