I have and async Load method that is triggered by a observable subscription.
I want to prevent concurrent calls of LoadAsync using following strategy:
I'm almost sure this strategy has a name, but I can't recall it.
LoadAsync won't be called if previous calls has not finished yet.
- LoadAsync(3) has started only after LoadAsync(1) has finished
LoadAsync is called only for the most recent value
- LoadAsync(2) has never been called
I currently use this switchmap equivalent, but it cancels the LoadAsync methods in favor of new calls.
Observable<int> sourceObservable..
sourceObservable
.Select(value => Observable.FromAsync(() => LoadAsync(value)))
.Switch()
async Task<int> LoadAsync(int value)
{
await Task.Delay(1000);
return value;
}
in javascript it would be:
source$.pipe(switchMap(Load))
function load(id): Observable<int> {
return delay(1000).pipe(map(() => id));
}
EDIT: I wonder why nobody asked for this flattening strategy yet. I thought it's pretty common requirement.
This is how CI/CD pipelines often works, for example. If there are too many pushes to a branch and you don't have the capacity to build them all, but you want to ensure that the latest commit will be built