johannesjo / angular2-promise-buttons

Chilled loading buttons for angular2
https://johannesjo.github.io/angular2-promise-buttons/#demo
MIT License
86 stars 28 forks source link

Running a closed subscription #40

Closed bouncehead13 closed 4 years ago

bouncehead13 commented 4 years ago

Use case

When the promise button is clicked and calls an API, this is occurring as a subscription. On completion of the subscription, the view changes to display the information.

Switching back to the original view (think of this as a form with a submit button), the original subscription, closed subscription, gets reassigned and the button begins to spin and stops shortly.

Proposed solution

In the code now

this.promise = new Promise((resolve) => {
    (passedValue as Subscription).add(resolve);
});

If the subscription is already closed, don't create the Promise wrapper. This will solve the problem when switching between views and assigning a subscription already closed. In the meantime, we are clearing the property after success.

this.buttonPromise = this.http.get(...).subscribe(data => {
    ...
    this.buttonPromise = undefined; // clear out before switching views
}, (err) => {
    this.apiError = true;
});
johannesjo commented 4 years ago

Hey there! Thank you very much for digging into this. Makes a lot of sense. Haven't thought about this case to be honest. Would you be willing to provide a fix for this? :)

bouncehead13 commented 4 years ago

@johannesjo Created a pull request. Let me know your thoughts.

johannesjo commented 4 years ago

Love it! Especially the unit tests. Thank you very much!