javascript - What happens to previous Subscriptions in RxJS? -
while i'm pretty used using rxjs, , reactive programming, there 1 thing that's been bothering me, can't head around.
let's have simple function run every time 1 clicks button scan
function scan() { this.startscaning(10).subscribe(scanneditem => console.log(scanneditem)) }
inside our scan function, use startscanning method starts scanning (i.e. bluetooth devices) 10 seconds, , returns observable subscribe , log discovered devices/items.
ok, far good, bothers me happens if user clicks button 10 times in row. happens previous subscriptions? , how supposed handle this? need unsubscribe every time, need unsubscribe @ all?
a nice explanation appreciated, possible further readings/examples, thanks
the way handle flip boolean while process running , bind button's [disabled]
property value, e.g.
isscanning: boolean function scan() { this.isscanning = true this.startscaning(10).subscribe({ next: scanneditem => console.log(scanneditem), complete: () => this.isscanning = false }) } <button (click)="scan()" [disabled]="isscanning">click me!</button>
(you might want add sort of indicator it's processing while button disabled - use font awesome's spinner icons *ngif="isscanning"
that)
as rest, depends on how startscaning
method implemented. you'd have ten separate observables each of automatically complete ten seconds after respective click, there wouldn't need worry manually unsubscribing or unless heavy process (but imo should still disable button anyway ux reasons).
looking @ question again, assumed you're using angular didn't that. if you're not, general principle same, difference you'll need use different way of setting button's disabled
state.
Comments
Post a Comment