Skip to content

Commit

Permalink
fix(SwalDirective): SwalDirectives now do a "cooperative mutex" when …
Browse files Browse the repository at this point in the history
…subscribing to an existing SwalComponent instance (#40)
  • Loading branch information
toverux committed Feb 14, 2018
1 parent 2af800a commit 19bc9c3
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/swal.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ComponentFactoryResolver, ComponentRef, Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output,
ViewContainerRef
} from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { SweetAlertOptions, SweetAlertType } from 'sweetalert2';
import { SwalComponent } from './swal.component';

Expand Down Expand Up @@ -86,9 +85,6 @@ export class SwalDirective implements OnInit, OnDestroy {
*/
private swalOptions: SweetAlertOptions;

private confirmSubscription: Subscription;
private cancelSubscription: Subscription;

public constructor(
private readonly viewContainerRef: ViewContainerRef,
private readonly resolver: ComponentFactoryResolver) {
Expand All @@ -106,9 +102,6 @@ export class SwalDirective implements OnInit, OnDestroy {
this.swalRef = this.viewContainerRef.createComponent(factory);
this.swalInstance = this.swalRef.instance;
}

this.confirmSubscription = this.swalInstance.confirm.asObservable().subscribe(v => this.confirm.emit(v));
this.cancelSubscription = this.swalInstance.cancel.asObservable().subscribe(v => this.cancel.emit(v));
}

/**
Expand All @@ -119,9 +112,6 @@ export class SwalDirective implements OnInit, OnDestroy {
if (this.swalRef) {
this.swalRef.destroy();
}

this.confirmSubscription.unsubscribe();
this.cancelSubscription.unsubscribe();
}

/**
Expand All @@ -139,6 +129,14 @@ export class SwalDirective implements OnInit, OnDestroy {
this.swalInstance.options = this.swalOptions;
}

this.swalInstance.show();
const confirmSub = this.swalInstance.confirm.asObservable().subscribe(v => this.confirm.emit(v));
const cancelSub = this.swalInstance.cancel.asObservable().subscribe(v => this.cancel.emit(v));

this.swalInstance.show().then(unsubscribe);

function unsubscribe() {
confirmSub.unsubscribe();
cancelSub.unsubscribe();
}
}
}

0 comments on commit 19bc9c3

Please sign in to comment.