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 =
this.myProperty2 =
}
@Clean()
clean () {
}
}
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 =
this.myProperty2 =
}
@Clean({ id: 'a' })
cleanA () {
}
@Clean({ id: 'b' })
cleanB () {
}
}
The CleanMetadata
decorator also takes a before
parameter which defines if the decorator runs before the original method or otherwise.