Skip to content

Commit 68ba732

Browse files
authored
fix: unsubscribe ChangeFilter subscriptions onDestroy (#387)
1 parent 0001363 commit 68ba732

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ A starter project on Github: https://github.com/xieziyu/ngx-echarts-starter
5454

5555
# Latest Update
5656

57+
- 2023.03.20: v15.0.2:
58+
59+
- Fix: unsubscribe ChangeFilter subscriptions onDestroy.
60+
5761
- 2022.12.01: v15.0.1:
5862

5963
- Feat: Support nullable @Inputs. Issue [#378](https://github.com/xieziyu/ngx-echarts/issues/378)

package-lock.json

Lines changed: 36 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/ngx-echarts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-echarts",
3-
"version": "15.0.1",
3+
"version": "15.0.2",
44
"author": "Xie, Ziyu",
55
"license": "MIT",
66
"keywords": [
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { SimpleChanges } from '@angular/core';
2+
import { Subject, Subscription } from 'rxjs';
3+
4+
export class ChangeFilterV2 {
5+
private subject = new Subject<SimpleChanges>();
6+
private subscriptions: Subscription = new Subscription();
7+
8+
doFilter(changes: SimpleChanges) {
9+
this.subject.next(changes);
10+
}
11+
12+
dispose() {
13+
this.subscriptions.unsubscribe();
14+
}
15+
16+
notEmpty<T>(key: string, handler: (t: T) => void) {
17+
this.subscriptions.add(this.subject.subscribe(changes => {
18+
if (changes[key]) {
19+
const value: T = changes[key].currentValue;
20+
if (value !== undefined && value !== null) {
21+
handler(value);
22+
}
23+
}
24+
}));
25+
}
26+
27+
has<T>(key: string, handler: (t: T) => void) {
28+
this.subscriptions.add(this.subject.subscribe(changes => {
29+
if (changes[key]) {
30+
const value: T = changes[key].currentValue;
31+
handler(value);
32+
}
33+
}));
34+
}
35+
36+
notFirst<T>(key: string, handler: (t: T) => void) {
37+
this.subscriptions.add(this.subject.subscribe(changes => {
38+
if (changes[key] && !changes[key].isFirstChange()) {
39+
const value: T = changes[key].currentValue;
40+
handler(value);
41+
}
42+
}));
43+
}
44+
45+
notFirstAndEmpty<T>(key: string, handler: (t: T) => void) {
46+
this.subscriptions.add(this.subject.subscribe(changes => {
47+
if (changes[key] && !changes[key].isFirstChange()) {
48+
const value: T = changes[key].currentValue;
49+
if (value !== undefined && value !== null) {
50+
handler(value);
51+
}
52+
}
53+
}));
54+
}
55+
}

projects/ngx-echarts/src/lib/ngx-echarts.directive.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@angular/core';
1616
import { Observable, Subject, Subscription, asyncScheduler } from 'rxjs';
1717
import { switchMap, throttleTime } from 'rxjs/operators';
18-
import { ChangeFilter } from './change-filter';
18+
import { ChangeFilterV2 } from './change-filter-v2';
1919
import type { EChartsOption, ECharts } from 'echarts';
2020

2121
export interface NgxEchartsConfig {
@@ -95,6 +95,7 @@ export class NgxEchartsDirective implements OnChanges, OnDestroy, OnInit, AfterV
9595
private resize$ = new Subject<void>();
9696
private resizeSub: Subscription;
9797
private initChartTimer?: number;
98+
private changeFilter = new ChangeFilterV2();
9899

99100
constructor(
100101
@Inject(NGX_ECHARTS_CONFIG) config: NgxEchartsConfig,
@@ -105,11 +106,7 @@ export class NgxEchartsDirective implements OnChanges, OnDestroy, OnInit, AfterV
105106
}
106107

107108
ngOnChanges(changes: SimpleChanges) {
108-
const filter = ChangeFilter.of(changes);
109-
filter.notFirstAndEmpty<any>('options').subscribe((opt) => this.onOptionsChange(opt));
110-
filter.notFirstAndEmpty<any>('merge').subscribe((opt) => this.setOption(opt));
111-
filter.has<boolean>('loading').subscribe((v) => this.toggleLoading(!!v));
112-
filter.notFirst<string | ThemeOption>('theme').subscribe(() => this.refreshChart());
109+
this.changeFilter.doFilter(changes);
113110
}
114111

115112
ngOnInit() {
@@ -126,6 +123,11 @@ export class NgxEchartsDirective implements OnChanges, OnDestroy, OnInit, AfterV
126123
}))
127124
this.resizeOb.observe(this.el.nativeElement);
128125
}
126+
127+
this.changeFilter.notFirstAndEmpty('options', (opt) => this.onOptionsChange(opt));
128+
this.changeFilter.notFirstAndEmpty('merge', (opt) => this.setOption(opt));
129+
this.changeFilter.has<boolean>('loading', (v) => this.toggleLoading(!!v));
130+
this.changeFilter.notFirst<string | ThemeOption>('theme', () => this.refreshChart());
129131
}
130132

131133
ngOnDestroy() {
@@ -139,6 +141,7 @@ export class NgxEchartsDirective implements OnChanges, OnDestroy, OnInit, AfterV
139141
if (this.resizeOb) {
140142
this.resizeOb.unobserve(this.el.nativeElement);
141143
}
144+
this.changeFilter.dispose();
142145
this.dispose();
143146
}
144147

0 commit comments

Comments
 (0)