fix: use shareReplay for inflight requests (#11810)

This commit is contained in:
jrandolf 2024-02-02 08:09:33 +01:00 committed by GitHub
parent fa353d3a59
commit 0f0813db38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 39 deletions

View File

@ -19,11 +19,13 @@ import {
map, map,
merge, merge,
mergeMap, mergeMap,
mergeScan,
of, of,
race,
raceWith, raceWith,
ReplaySubject,
startWith, startWith,
switchMap, switchMap,
take,
takeUntil, takeUntil,
timer, timer,
type Observable, type Observable,
@ -604,8 +606,7 @@ export abstract class Page extends EventEmitter<PageEvents> {
#requestHandlers = new WeakMap<Handler<HTTPRequest>, Handler<HTTPRequest>>(); #requestHandlers = new WeakMap<Handler<HTTPRequest>, Handler<HTTPRequest>>();
#requestsInFlight = 0; #inflight$ = new ReplaySubject<number>(1);
#inflight$: Observable<number>;
/** /**
* @internal * @internal
@ -613,39 +614,37 @@ export abstract class Page extends EventEmitter<PageEvents> {
constructor() { constructor() {
super(); super();
this.#inflight$ = fromEmitterEvent(this, PageEvent.Request).pipe( fromEmitterEvent(this, PageEvent.Request)
takeUntil(fromEmitterEvent(this, PageEvent.Close)), .pipe(
mergeMap(request => { mergeMap(originalRequest => {
return concat( return concat(
of(1), of(1),
race( merge(
fromEmitterEvent(this, PageEvent.Response).pipe( fromEmitterEvent(this, PageEvent.RequestFailed),
filter(response => { fromEmitterEvent(this, PageEvent.RequestFinished),
return response.request()._requestId === request._requestId; fromEmitterEvent(this, PageEvent.Response).pipe(
}) map(response => {
), return response.request();
fromEmitterEvent(this, PageEvent.RequestFailed).pipe( })
filter(failure => { )
return failure._requestId === request._requestId; ).pipe(
}) filter(request => {
), return request._requestId === originalRequest._requestId;
fromEmitterEvent(this, PageEvent.RequestFinished).pipe( }),
filter(success => { take(1),
return success._requestId === request._requestId; map(() => {
return -1;
}) })
) )
).pipe( );
map(() => { }),
return -1; mergeScan((acc, addend) => {
}) return of(acc + addend);
) }, 0),
); takeUntil(fromEmitterEvent(this, PageEvent.Close)),
}) startWith(0)
); )
.subscribe(this.#inflight$);
this.#inflight$.subscribe(count => {
this.#requestsInFlight += count;
});
} }
/** /**
@ -1776,13 +1775,11 @@ export abstract class Page extends EventEmitter<PageEvents> {
} = options; } = options;
return this.#inflight$.pipe( return this.#inflight$.pipe(
startWith(this.#requestsInFlight), switchMap(inflight => {
switchMap(() => { if (inflight > concurrency) {
if (this.#requestsInFlight > concurrency) {
return EMPTY; return EMPTY;
} else {
return timer(idleTime);
} }
return timer(idleTime);
}), }),
map(() => {}), map(() => {}),
raceWith( raceWith(

View File

@ -22,8 +22,10 @@ export {
ignoreElements, ignoreElements,
lastValueFrom, lastValueFrom,
map, map,
ReplaySubject,
merge, merge,
mergeMap, mergeMap,
mergeScan,
NEVER, NEVER,
noop, noop,
Observable, Observable,
@ -34,6 +36,7 @@ export {
retry, retry,
startWith, startWith,
switchMap, switchMap,
take,
takeUntil, takeUntil,
tap, tap,
throwIfEmpty, throwIfEmpty,