( workCreator: (event: T) => ObservableInput<R>, mapper?: (_: T, inner: R) => S )
| 18 | * @param mapper A function to combine each emission of the togglable with the trigger itself, making it the new value of the togglable. |
| 19 | */ |
| 20 | export function queueOnlyLatest<T, R, S = R>( |
| 21 | workCreator: (event: T) => ObservableInput<R>, |
| 22 | mapper?: (_: T, inner: R) => S |
| 23 | ): OperatorFunction<T, S> { |
| 24 | return function (source: Observable<T>) { |
| 25 | return new Observable((notify) => { |
| 26 | let handlerSub: Subscription; |
| 27 | |
| 28 | let work: Observable<R>; |
| 29 | // each new concurrent request overwrites this |
| 30 | let nextWork: Observable<R> | null; |
| 31 | const nextViaMapper = ([result, trigger]: [R, T]) => { |
| 32 | const _result = (mapper ? mapper(trigger, result) : result) as S; |
| 33 | notify.next(_result); |
| 34 | }; |
| 35 | |
| 36 | let workObserver: PartialObserver<[R, T]> = { |
| 37 | complete() { |
| 38 | handlerSub = nextWork?.subscribe({ |
| 39 | next: nextViaMapper, |
| 40 | }); |
| 41 | }, |
| 42 | next: nextViaMapper, |
| 43 | error: (e) => notify.next(e), |
| 44 | }; |
| 45 | |
| 46 | function makeWork(trigger: T) { |
| 47 | return from(workCreator(trigger)).pipe(withLatestFrom(of(trigger))); |
| 48 | } |
| 49 | |
| 50 | return source.subscribe({ |
| 51 | next(trigger) { |
| 52 | if (!handlerSub || handlerSub.closed) { |
| 53 | // clear our queue |
| 54 | nextWork = null; |
| 55 | // start this work |
| 56 | work = makeWork(trigger); |
| 57 | handlerSub = work.subscribe(workObserver); |
| 58 | } else { |
| 59 | // let the existing handlerSub call nextWork, which we'll populate |
| 60 | nextWork = makeWork(trigger); |
| 61 | } |
| 62 | }, |
| 63 | error(e) { |
| 64 | notify.error(e); |
| 65 | }, |
| 66 | complete() { |
| 67 | notify.complete(e); |
| 68 | }, |
| 69 | }); |
| 70 | }); |
| 71 | }; |
| 72 | } |
no test coverage detected