Skip to content

Commit

Permalink
use subscription array & remove EventEmitter unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
leo6104 authored and jkuri committed Oct 22, 2017
1 parent 5ba36ad commit 29d9af7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/mergeMap';
import { UploaderOptions, UploadFile, UploadOutput, UploadInput, UploadStatus, BlobFile } from './interfaces';
import { UploadFile, UploadOutput, UploadInput, UploadStatus, BlobFile } from './interfaces';

export function humanizeBytes(bytes: number): string {
if (bytes === 0) {
Expand Down Expand Up @@ -67,8 +67,8 @@ export class NgUploaderService {
this.serviceEvents.emit({ type: 'allAddedToQueue' });
}

initInputEvents(input: EventEmitter<UploadInput>): void {
input.subscribe((event: UploadInput) => {
initInputEvents(input: EventEmitter<UploadInput>): Subscription {
return input.subscribe((event: UploadInput) => {
switch (event.type) {
case 'uploadFile':
const uploadFileIndex = this.queue.findIndex(file => file === event.file);
Expand Down
21 changes: 12 additions & 9 deletions src/ngx-uploader/directives/ng-file-drop.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy, HostListener } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { UploadOutput, UploadInput, UploadFile, UploaderOptions, NgUploaderService } from '../../../';
import { UploadOutput, UploadInput, UploaderOptions, NgUploaderService } from '../../../';
import { Subscription } from 'rxjs/Subscription';

@Directive({
selector: '[ngFileDrop]'
Expand All @@ -13,22 +13,27 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
upload: NgUploaderService;
el: HTMLInputElement;

private _sub: Subscription[];

constructor(private elementRef: ElementRef) {
this.uploadOutput = new EventEmitter<UploadOutput>();
}

ngOnInit() {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency);

this.el = this.elementRef.nativeElement;

this.upload.serviceEvents.subscribe((event: UploadOutput) => {
this.uploadOutput.emit(event);
});
this._sub.push(
this.upload.serviceEvents.subscribe((event: UploadOutput) => {
this.uploadOutput.emit(event);
})
);

if (this.uploadInput instanceof EventEmitter) {
this.upload.initInputEvents(this.uploadInput);
this._sub.push(this.upload.initInputEvents(this.uploadInput));
}

this.el.addEventListener('drop', this.stopEvent, false);
Expand All @@ -37,9 +42,7 @@ export class NgFileDropDirective implements OnInit, OnDestroy {
}

ngOnDestroy() {
if (this.uploadInput) {
this.uploadInput.unsubscribe();
}
this._sub.forEach(sub => sub.unsubscribe());
}

stopEvent = (e: Event) => {
Expand Down
22 changes: 12 additions & 10 deletions src/ngx-uploader/directives/ng-file-select.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { UploadOutput, UploadInput, UploadFile, UploaderOptions, NgUploaderService } from '../../../';
import { UploadOutput, UploaderOptions, NgUploaderService } from '../../../';
import { Subscription } from 'rxjs/Subscription';

@Directive({
selector: '[ngFileSelect]'
Expand All @@ -13,32 +13,34 @@ export class NgFileSelectDirective implements OnInit, OnDestroy {
upload: NgUploaderService;
el: HTMLInputElement;

private _sub: Subscription[];

constructor(private elementRef: ElementRef) {
this.uploadOutput = new EventEmitter<UploadOutput>();
}

ngOnInit() {
this._sub = [];
const concurrency = this.options && this.options.concurrency || Number.POSITIVE_INFINITY;
this.upload = new NgUploaderService(concurrency);

this.el = this.elementRef.nativeElement;
this.el.addEventListener('change', this.fileListener, false);

this.upload.serviceEvents.subscribe((event: UploadOutput) => {
this.uploadOutput.emit(event);
});
this._sub.push(
this.upload.serviceEvents.subscribe((event: UploadOutput) => {
this.uploadOutput.emit(event);
})
);

if (this.uploadInput instanceof EventEmitter) {
this.upload.initInputEvents(this.uploadInput);
this._sub.push(this.upload.initInputEvents(this.uploadInput));
}
}

ngOnDestroy() {
this.el.removeEventListener('change', this.fileListener, false);

if (this.uploadInput) {
this.uploadInput.unsubscribe();
}
this._sub.forEach(sub => sub.unsubscribe());
}

fileListener = () => {
Expand Down

0 comments on commit 29d9af7

Please sign in to comment.