:) Unsubscribe para Pros and Juniors
Problema
English
When you subscribe to an observable or event in JavaScript, you needto unsubscribe to release memory in the system when you finish to use it. Otherwise, you will have a memory leak.
Português
Quando você se inscrever em um evento observável ou em JavaScript, será necessário cancelar a assinatura da memória do sistema quando terminar de usá-lo. Caso contrário, você terá um vazamento de memória.
AutoLeave()
English.
Class decorator that will automatically unsubscribe from observable subscriptions when the component is destroyed .
PT.
Decorador de classe que irá automaticamente cancelar os observáveis quando o componente for destruído e não só .
Installation ️
npm install ngx-leave --save
##AutoLeave
import {AutoLeave} from "ngx-leave";
@AutoLeave({subscriptions: ['um
Option
description
default
subscriptions
array all name of subscription
[]
arrayName
name variable with subscription name
''
method
function name to lib call
ngOnDestroy
Nota: subscriptions will be ignore if you set a value on arrayName.
@AutoLeave({subscriptions: ['um
Leave
import {Leave} from 'ngx-leave';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
})
export class testComponent implements OnInit, OnDestroy {
constructor( ) { }
ngOnInit() {
interval(1000)
.pipe(Leave(this))
.subscribe(val => console.log(val));
}
// This method need to be implemented
ngOnDestroy() { }
}
Option
Leave(this,functionName);
Usando em uma class
import {Leave} from 'ngx-leave';
export class Widget {
constructor() {
interval(1000)
.pipe(Leave(this, 'deleteFunc'))
.subscribe(console.log);
}
// need to be created
deleteFunc() {}
}
We test
Ação
Descrição
Test Suite
4 passed, 4 total
Tests
17 passed, 17 total
Snapshots
0 total
Time
6.129s
Ran all test suites
Obrigado / Thanks
All Devs
,'tree
Option
description
default
__INLINE_CODE_1__
array all name of subscription
__INLINE_CODE_2__
__INLINE_CODE_3__
name variable with subscription name
__INLINE_CODE_4__
__INLINE_CODE_5__
function name to lib call
__INLINE_CODE_6__
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__
Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__
We test
Ação
Descrição
__INLINE_CODE_10__
4 passed, 4 total
__INLINE_CODE_11__
17 passed, 17 total
__INLINE_CODE_12__
0 total
__INLINE_CODE_13__
6.129s
__INLINE_CODE_14__
Obrigado / Thanks
All Devs
]})
@Component({
selector: 'app'
})
export class appComponent implements OnInit, OnDestroy{
um$: Subscription;
tree$: Subscription;
constructor( ) { }
ngOnInit() {
this.um$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val));
this.tree$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val));
}
// This method need to be implemented
ngOnDestroy() { }
}
| Option | description | default |
|---|---|---|
| __INLINE_CODE_1__ | array all name of subscription | __INLINE_CODE_2__ |
| __INLINE_CODE_3__ | name variable with subscription name | __INLINE_CODE_4__ |
| __INLINE_CODE_5__ | function name to lib call | __INLINE_CODE_6__ |
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
,'treeLeave
import {Leave} from 'ngx-leave';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
})
export class testComponent implements OnInit, OnDestroy {
constructor( ) { }
ngOnInit() {
interval(1000)
.pipe(Leave(this))
.subscribe(val => console.log(val));
}
// This method need to be implemented
ngOnDestroy() { }
}
Option
Leave(this,functionName);
Usando em uma class
import {Leave} from 'ngx-leave';
export class Widget {
constructor() {
interval(1000)
.pipe(Leave(this, 'deleteFunc'))
.subscribe(console.log);
}
// need to be created
deleteFunc() {}
}
We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
,'tree| Option | description | default |
|---|---|---|
| __INLINE_CODE_1__ | array all name of subscription | __INLINE_CODE_2__ |
| __INLINE_CODE_3__ | name variable with subscription name | __INLINE_CODE_4__ |
| __INLINE_CODE_5__ | function name to lib call | __INLINE_CODE_6__ |
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
]}) @Component({ selector: 'app' }) export class appComponent implements OnInit, OnDestroy{ um$: Subscription; tree$: Subscription; constructor( ) { } ngOnInit() { this.um$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val)); this.tree$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val)); } // This method need to be implemented ngOnDestroy() { } }| Option | description | default |
|---|---|---|
| __INLINE_CODE_1__ | array all name of subscription | __INLINE_CODE_2__ |
| __INLINE_CODE_3__ | name variable with subscription name | __INLINE_CODE_4__ |
| __INLINE_CODE_5__ | function name to lib call | __INLINE_CODE_6__ |
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
]})Leave
import {Leave} from 'ngx-leave';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
})
export class testComponent implements OnInit, OnDestroy {
constructor( ) { }
ngOnInit() {
interval(1000)
.pipe(Leave(this))
.subscribe(val => console.log(val));
}
// This method need to be implemented
ngOnDestroy() { }
}
Option
Leave(this,functionName);
Usando em uma class
import {Leave} from 'ngx-leave';
export class Widget {
constructor() {
interval(1000)
.pipe(Leave(this, 'deleteFunc'))
.subscribe(console.log);
}
// need to be created
deleteFunc() {}
}
We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
,'tree| Option | description | default |
|---|---|---|
| __INLINE_CODE_1__ | array all name of subscription | __INLINE_CODE_2__ |
| __INLINE_CODE_3__ | name variable with subscription name | __INLINE_CODE_4__ |
| __INLINE_CODE_5__ | function name to lib call | __INLINE_CODE_6__ |
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs
]}) @Component({ selector: 'app' }) export class appComponent implements OnInit, OnDestroy{ um$: Subscription; tree$: Subscription; constructor( ) { } ngOnInit() { this.um$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val)); this.tree$ = from([0, 1, 2, 3, 4, 5, 6, 7]).subscribe(val => console.log(val)); } // This method need to be implemented ngOnDestroy() { } }| Option | description | default |
|---|---|---|
| __INLINE_CODE_1__ | array all name of subscription | __INLINE_CODE_2__ |
| __INLINE_CODE_3__ | name variable with subscription name | __INLINE_CODE_4__ |
| __INLINE_CODE_5__ | function name to lib call | __INLINE_CODE_6__ |
Nota: __INLINE_CODE_7__ will be ignore if you set a value on __INLINE_CODE_8__.
__INLINE_CODE_9__
Leave
__CODE_BLOCK_1__Option
Leave(this,functionName);
Usando em uma class
__CODE_BLOCK_2__We test
| Ação | Descrição |
|---|---|
| __INLINE_CODE_10__ | 4 passed, 4 total |
| __INLINE_CODE_11__ | 17 passed, 17 total |
| __INLINE_CODE_12__ | 0 total |
| __INLINE_CODE_13__ | 6.129s |
| __INLINE_CODE_14__ |
Obrigado / Thanks
All Devs