safe unsubscribe (#75)

This commit is contained in:
Cilibiu Bogdan
2017-11-22 20:38:51 +02:00
committed by Denys Vuika
parent 702614d8b0
commit 69f43d75f7
7 changed files with 47 additions and 71 deletions

View File

@@ -25,7 +25,7 @@ import { Subscription } from 'rxjs/Rx';
styleUrls: [ './current-user.component.scss' ] styleUrls: [ './current-user.component.scss' ]
}) })
export class CurrentUserComponent implements OnInit, OnDestroy { export class CurrentUserComponent implements OnInit, OnDestroy {
private personSubscription: Subscription; private subscriptions: Subscription[] = [];
user: any = null; user: any = null;
@@ -35,14 +35,13 @@ export class CurrentUserComponent implements OnInit, OnDestroy {
) {} ) {}
ngOnInit() { ngOnInit() {
this.personSubscription = this.peopleApi.getCurrentPerson() this.subscriptions = this.subscriptions.concat([
.subscribe((person: any) => { this.peopleApi.getCurrentPerson().subscribe((person: any) => this.user = person.entry)
this.user = person.entry; ]);
});
} }
ngOnDestroy() { ngOnDestroy() {
this.personSubscription.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
} }
get userFirstName(): string { get userFirstName(): string {

View File

@@ -34,7 +34,7 @@ export class FavoritesComponent extends PageComponent implements OnInit, OnDestr
@ViewChild(DocumentListComponent) @ViewChild(DocumentListComponent)
documentList: DocumentListComponent; documentList: DocumentListComponent;
private subscriptions: any = []; private subscriptions: Subscription[] = [];
constructor( constructor(
private router: Router, private router: Router,

View File

@@ -35,15 +35,7 @@ export class FilesComponent extends PageComponent implements OnInit, OnDestroy {
isValidPath = true; isValidPath = true;
private nodePath: PathElement[]; private nodePath: PathElement[];
private onCopyNode: Subscription; private subscriptions: Subscription[] = [];
private onRemoveItem: Subscription;
private onCreateFolder: Subscription;
private onEditFolder: Subscription;
private onDeleteNode: Subscription;
private onMoveNode: Subscription;
private onRestoreNode: Subscription;
private onFileUploadComplete: Subscription;
private onToggleFavorite: Subscription;
constructor( constructor(
private router: Router, private router: Router,
@@ -85,30 +77,21 @@ export class FilesComponent extends PageComponent implements OnInit, OnDestroy {
); );
}); });
this.onCopyNode = nodeActionsService.contentCopied this.subscriptions = this.subscriptions.concat([
.subscribe((nodes) => this.onContentCopied(nodes)); nodeActionsService.contentCopied.subscribe((nodes) => this.onContentCopied(nodes)),
this.onCreateFolder = contentService.folderCreate.subscribe(() => this.load()); contentService.folderCreate.subscribe(() => this.load()),
this.onEditFolder = contentService.folderEdit.subscribe(() => this.load()); contentService.folderEdit.subscribe(() => this.load()),
this.onDeleteNode = contentManagementService.deleteNode.subscribe(() => this.load()); contentManagementService.deleteNode.subscribe(() => this.load()),
this.onMoveNode = contentManagementService.moveNode.subscribe(() => this.load()); contentManagementService.moveNode.subscribe(() => this.load()),
this.onRestoreNode = contentManagementService.restoreNode.subscribe(() => this.load()); contentManagementService.restoreNode.subscribe(() => this.load()),
this.onToggleFavorite = contentManagementService.toggleFavorite.subscribe(() => this.load()); contentManagementService.toggleFavorite.subscribe(() => this.load()),
this.onFileUploadComplete = uploadService.fileUploadComplete uploadService.fileUploadComplete.subscribe(file => this.onFileUploadedEvent(file)),
.subscribe(file => this.onFileUploadedEvent(file)); uploadService.fileUploadDeleted.subscribe((file) => this.onFileUploadedEvent(file))
this.onRemoveItem = uploadService.fileUploadDeleted ]);
.subscribe((file) => this.onFileUploadedEvent(file));
} }
ngOnDestroy() { ngOnDestroy() {
this.onCopyNode.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
this.onRemoveItem.unsubscribe();
this.onCreateFolder.unsubscribe();
this.onEditFolder.unsubscribe();
this.onDeleteNode.unsubscribe();
this.onMoveNode.unsubscribe();
this.onRestoreNode.unsubscribe();
this.onFileUploadComplete.unsubscribe();
this.onToggleFavorite.unsubscribe();
this.browsingFilesService.onChangeParent.next(null); this.browsingFilesService.onChangeParent.next(null);
} }

View File

@@ -29,19 +29,20 @@ import { BrowsingFilesService } from '../../common/services/browsing-files.servi
export class LayoutComponent implements OnInit, OnDestroy { export class LayoutComponent implements OnInit, OnDestroy {
node: MinimalNodeEntryEntity; node: MinimalNodeEntryEntity;
browsingFilesSubscription: Subscription; private subscriptions: Subscription[] = [];
constructor( constructor(
private contentService: ContentService, private contentService: ContentService,
private browsingFilesService: BrowsingFilesService) {} private browsingFilesService: BrowsingFilesService) {}
ngOnInit() { ngOnInit() {
this.browsingFilesSubscription = this.browsingFilesService.onChangeParent this.subscriptions.concat([
.subscribe((node: MinimalNodeEntryEntity) => this.node = node); this.browsingFilesService.onChangeParent.subscribe((node: MinimalNodeEntryEntity) => this.node = node)
]);
} }
ngOnDestroy() { ngOnDestroy() {
this.browsingFilesSubscription.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
} }
canCreateContent(node: MinimalNodeEntryEntity): boolean { canCreateContent(node: MinimalNodeEntryEntity): boolean {

View File

@@ -32,10 +32,7 @@ export class RecentFilesComponent extends PageComponent implements OnInit, OnDes
@ViewChild(DocumentListComponent) @ViewChild(DocumentListComponent)
documentList: DocumentListComponent; documentList: DocumentListComponent;
private onDeleteNode: Subscription; private subscriptions: Subscription[] = [];
private onMoveNode: Subscription;
private onRestoreNode: Subscription;
private onToggleFavorite: Subscription;
constructor( constructor(
private router: Router, private router: Router,
@@ -44,17 +41,16 @@ export class RecentFilesComponent extends PageComponent implements OnInit, OnDes
} }
ngOnInit() { ngOnInit() {
this.onDeleteNode = this.content.deleteNode.subscribe(() => this.refresh()); this.subscriptions = this.subscriptions.concat([
this.onMoveNode = this.content.moveNode.subscribe(() => this.refresh()); this.content.deleteNode.subscribe(() => this.refresh()),
this.onRestoreNode = this.content.restoreNode.subscribe(() => this.refresh()); this.content.moveNode.subscribe(() => this.refresh()),
this.onToggleFavorite = this.content.toggleFavorite.subscribe(() => this.refresh()); this.content.restoreNode.subscribe(() => this.refresh()),
this.content.toggleFavorite.subscribe(() => this.refresh())
]);
} }
ngOnDestroy() { ngOnDestroy() {
this.onDeleteNode.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
this.onMoveNode.unsubscribe();
this.onRestoreNode.unsubscribe();
this.onToggleFavorite.unsubscribe();
} }
onNodeDoubleClick(node: MinimalNodeEntryEntity) { onNodeDoubleClick(node: MinimalNodeEntryEntity) {

View File

@@ -33,10 +33,7 @@ export class SharedFilesComponent extends PageComponent implements OnInit, OnDes
@ViewChild(DocumentListComponent) @ViewChild(DocumentListComponent)
documentList: DocumentListComponent; documentList: DocumentListComponent;
private onDeleteNode: Subscription; private subscriptions: Subscription[] = [];
private onMoveNode: Subscription;
private onRestoreNode: Subscription;
private onToggleFavorite: Subscription;
constructor( constructor(
private router: Router, private router: Router,
@@ -46,17 +43,16 @@ export class SharedFilesComponent extends PageComponent implements OnInit, OnDes
} }
ngOnInit() { ngOnInit() {
this.onDeleteNode = this.content.deleteNode.subscribe(() => this.refresh()); this.subscriptions = this.subscriptions.concat([
this.onMoveNode = this.content.moveNode.subscribe(() => this.refresh()); this.content.deleteNode.subscribe(() => this.refresh()),
this.onRestoreNode = this.content.restoreNode.subscribe(() => this.refresh()); this.content.moveNode.subscribe(() => this.refresh()),
this.onToggleFavorite = this.content.toggleFavorite.subscribe(() => this.refresh()); this.content.restoreNode.subscribe(() => this.refresh()),
this.content.toggleFavorite.subscribe(() => this.refresh())
]);
} }
ngOnDestroy() { ngOnDestroy() {
this.onDeleteNode.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
this.onMoveNode.unsubscribe();
this.onRestoreNode.unsubscribe();
this.onToggleFavorite.unsubscribe();
} }
onNodeDoubleClick(link: { nodeId?: string }) { onNodeDoubleClick(link: { nodeId?: string }) {

View File

@@ -31,9 +31,10 @@ import { BrowsingFilesService } from '../../common/services/browsing-files.servi
}) })
export class SidenavComponent implements OnInit, OnDestroy { export class SidenavComponent implements OnInit, OnDestroy {
node: MinimalNodeEntryEntity = null; node: MinimalNodeEntryEntity = null;
onChangeParentSubscription: Subscription;
navigation = []; navigation = [];
private subscriptions: Subscription[] = [];
constructor( constructor(
private browsingFilesService: BrowsingFilesService, private browsingFilesService: BrowsingFilesService,
private contentService: ContentService, private contentService: ContentService,
@@ -46,14 +47,14 @@ export class SidenavComponent implements OnInit, OnDestroy {
} }
ngOnInit() { ngOnInit() {
this.onChangeParentSubscription = this.browsingFilesService.onChangeParent this.subscriptions.concat([
.subscribe((node: MinimalNodeEntryEntity) => { this.browsingFilesService.onChangeParent
this.node = node; .subscribe((node: MinimalNodeEntryEntity) => this.node = node)
}); ]);
} }
ngOnDestroy() { ngOnDestroy() {
this.onChangeParentSubscription.unsubscribe(); this.subscriptions.forEach(s => s.unsubscribe());
} }
canCreateContent(parentNode: MinimalNodeEntryEntity): boolean { canCreateContent(parentNode: MinimalNodeEntryEntity): boolean {