Skip to content

Commit 5db73e0

Browse files
committed
fix(*): handle rxjs subscriptions
1 parent b433095 commit 5db73e0

File tree

5 files changed

+47
-87
lines changed

5 files changed

+47
-87
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
"postversion": "git push origin master && git push --tags && npm run github-release && node publish.js",
4949
"webdriver-update": "node ./node_modules/protractor/bin/webdriver-manager update"
5050
},
51-
"pre-commit": [
52-
"lint",
53-
"test"
54-
],
5551
"devDependencies": {
5652
"@angular/cli": "1.3.0",
5753
"@angular/common": "4.1.3",
@@ -86,7 +82,6 @@
8682
"karma-phantomjs-launcher": "1.0.4",
8783
"phantomjs-polyfill": "0.0.2",
8884
"phantomjs-prebuilt": "2.1.14",
89-
"pre-commit": "1.2.2",
9085
"protractor": "5.1.2",
9186
"rimraf": "2.6.1",
9287
"rxjs": "5.4.0",

src/tree-internal.component.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Input, Component, OnInit, OnDestroy, ElementRef, Inject } from '@angular/core';
1+
import { Component, ElementRef, Inject, Input, OnDestroy, OnInit } from '@angular/core';
22
import * as TreeTypes from './tree.types';
33
import { Tree } from './tree';
44
import { TreeController } from './tree-controller';
55
import { NodeMenuService } from './menu/node-menu.service';
6-
import { NodeMenuItemSelectedEvent, NodeMenuItemAction } from './menu/menu.events';
6+
import { NodeMenuItemAction, NodeMenuItemSelectedEvent } from './menu/menu.events';
77
import { NodeEditableEvent, NodeEditableEventAction } from './editable/editable.events';
88
import { TreeService } from './tree.service';
99
import * as EventUtils from './utils/event.utils';
1010
import { NodeDraggableEvent } from './draggable/draggable.events';
11-
import { Observable } from 'rxjs/Rx';
1211
import * as _get from 'lodash/get';
12+
import { Subscription } from 'rxjs/Subscription';
1313

1414
@Component({
1515
selector: 'tree-internal',
@@ -66,6 +66,8 @@ export class TreeInternalComponent implements OnInit, OnDestroy {
6666
public isLeftMenuVisible = false;
6767
public controller: TreeController;
6868

69+
private subscriptions: Subscription[] = [];
70+
6971
public constructor(@Inject(NodeMenuService) private nodeMenuService: NodeMenuService,
7072
@Inject(TreeService) public treeService: TreeService,
7173
@Inject(ElementRef) public element: ElementRef) {
@@ -79,16 +81,16 @@ export class TreeInternalComponent implements OnInit, OnDestroy {
7981

8082
this.settings = this.settings || { rootIsVisible: true };
8183

82-
this.nodeMenuService.hideMenuStream(this.element)
84+
this.subscriptions.push(this.nodeMenuService.hideMenuStream(this.element)
8385
.subscribe(() => {
8486
this.isRightMenuVisible = false;
8587
this.isLeftMenuVisible = false;
86-
});
88+
}));
8789

88-
this.treeService.unselectStream(this.tree)
89-
.subscribe(() => this.isSelected = false);
90+
this.subscriptions.push(this.treeService.unselectStream(this.tree)
91+
.subscribe(() => this.isSelected = false));
9092

91-
this.treeService.draggedStream(this.tree, this.element)
93+
this.subscriptions.push(this.treeService.draggedStream(this.tree, this.element)
9294
.subscribe((e: NodeDraggableEvent) => {
9395
if (this.tree.hasSibling(e.captured.tree)) {
9496
this.swapWithSibling(e.captured.tree, this.tree);
@@ -97,13 +99,15 @@ export class TreeInternalComponent implements OnInit, OnDestroy {
9799
} else {
98100
this.moveNodeToParentTreeAndRemoveFromPreviousOne(e, this.tree);
99101
}
100-
});
102+
}));
101103
}
102104

103105
public ngOnDestroy(): void {
104106
if (_get(this.tree, 'node.id', '')) {
105107
this.treeService.deleteController(this.tree.node.id);
106108
}
109+
110+
this.subscriptions.forEach(sub => sub && sub.unsubscribe());
107111
}
108112

109113
private swapWithSibling(sibling: Tree, tree: Tree): void {

src/tree.component.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { Input, Component, OnInit, EventEmitter, Output, Inject, OnChanges, SimpleChanges, ViewChild } from '@angular/core';
1+
import {
2+
Input, Component, OnInit, EventEmitter, Output, Inject, OnChanges, SimpleChanges, ViewChild,
3+
OnDestroy
4+
} from '@angular/core';
25
import { TreeService } from './tree.service';
36
import * as TreeTypes from './tree.types';
47
import { NodeEvent } from './tree.events';
58
import { Tree } from './tree';
69
import { TreeController } from './tree-controller';
10+
import { Subscription } from 'rxjs/Subscription';
711

812
@Component({
913
selector: 'tree',
1014
template: `<tree-internal #rootComponent [tree]="tree" [settings]="settings"></tree-internal>`,
1115
providers: [TreeService]
1216
})
13-
export class TreeComponent implements OnInit, OnChanges {
17+
export class TreeComponent implements OnInit, OnChanges, OnDestroy {
1418
private static EMPTY_TREE: Tree = new Tree({value: ''});
1519

1620
/* tslint:disable:no-input-rename */
@@ -45,6 +49,8 @@ export class TreeComponent implements OnInit, OnChanges {
4549
public tree: Tree;
4650
@ViewChild('rootComponent') public rootComponent;
4751

52+
private subscriptions: Subscription[] = [];
53+
4854
public constructor(@Inject(TreeService) private treeService: TreeService) {
4955
}
5056

@@ -57,33 +63,33 @@ export class TreeComponent implements OnInit, OnChanges {
5763
}
5864

5965
public ngOnInit(): void {
60-
this.treeService.nodeRemoved$.subscribe((e: NodeEvent) => {
66+
this.subscriptions.push(this.treeService.nodeRemoved$.subscribe((e: NodeEvent) => {
6167
this.nodeRemoved.emit(e);
62-
});
68+
}));
6369

64-
this.treeService.nodeRenamed$.subscribe((e: NodeEvent) => {
70+
this.subscriptions.push(this.treeService.nodeRenamed$.subscribe((e: NodeEvent) => {
6571
this.nodeRenamed.emit(e);
66-
});
72+
}));
6773

68-
this.treeService.nodeCreated$.subscribe((e: NodeEvent) => {
74+
this.subscriptions.push(this.treeService.nodeCreated$.subscribe((e: NodeEvent) => {
6975
this.nodeCreated.emit(e);
70-
});
76+
}));
7177

72-
this.treeService.nodeSelected$.subscribe((e: NodeEvent) => {
78+
this.subscriptions.push(this.treeService.nodeSelected$.subscribe((e: NodeEvent) => {
7379
this.nodeSelected.emit(e);
74-
});
80+
}));
7581

76-
this.treeService.nodeMoved$.subscribe((e: NodeEvent) => {
82+
this.subscriptions.push(this.treeService.nodeMoved$.subscribe((e: NodeEvent) => {
7783
this.nodeMoved.emit(e);
78-
});
84+
}));
7985

80-
this.treeService.nodeExpanded$.subscribe((e: NodeEvent) => {
86+
this.subscriptions.push(this.treeService.nodeExpanded$.subscribe((e: NodeEvent) => {
8187
this.nodeExpanded.emit(e);
82-
});
88+
}));
8389

84-
this.treeService.nodeCollapsed$.subscribe((e: NodeEvent) => {
90+
this.subscriptions.push(this.treeService.nodeCollapsed$.subscribe((e: NodeEvent) => {
8591
this.nodeCollapsed.emit(e);
86-
});
92+
}));
8793
}
8894

8995
public getController(): TreeController {
@@ -93,4 +99,8 @@ export class TreeComponent implements OnInit, OnChanges {
9399
public getControllerByNodeId(id: number | string): TreeController {
94100
return this.treeService.getController(id);
95101
}
102+
103+
ngOnDestroy(): void {
104+
this.subscriptions.forEach(sub => sub && sub.unsubscribe());
105+
}
96106
}

src/tree.service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import {
2-
NodeRemovedEvent,
3-
NodeRenamedEvent,
2+
NodeCollapsedEvent,
43
NodeCreatedEvent,
5-
NodeSelectedEvent,
6-
NodeMovedEvent,
74
NodeExpandedEvent,
8-
NodeCollapsedEvent
5+
NodeMovedEvent,
6+
NodeRemovedEvent,
7+
NodeRenamedEvent,
8+
NodeSelectedEvent
99
} from './tree.events';
10-
import { RenamableNode, TreeModel } from './tree.types';
10+
import { RenamableNode } from './tree.types';
1111
import { Tree } from './tree';
1212
import { TreeController } from './tree-controller';
13-
import { Subject, Observable } from 'rxjs/Rx';
14-
import { Injectable, Inject, ElementRef } from '@angular/core';
13+
import { Observable, Subject } from 'rxjs/Rx';
14+
import { ElementRef, Inject, Injectable } from '@angular/core';
1515
import { NodeDraggableService } from './draggable/node-draggable.service';
1616
import { NodeDraggableEvent } from './draggable/draggable.events';
1717

0 commit comments

Comments
 (0)