Skip to content

Commit

Permalink
feat: defer to avoid sharing subject between subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Apr 7, 2021
1 parent 79628e7 commit 959ae75
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
defer,
from,
Observable,
ObservableInput,
Expand All @@ -18,23 +19,23 @@ import {exhaustMap, finalize, throttle} from "rxjs/operators"
export function exhaustMapWithTrailing<T, R>(
project: (value: T, index: number) => ObservableInput<R>
): OperatorFunction<T, R> {
return (source): Observable<R> => {
const release = new Subject<void>()

return source.pipe(
throttle(() => release, {
leading: true,
trailing: true,
}),
exhaustMap((value, index) =>
from(project(value, index)).pipe(
finalize(() => {
release.next()
})
return (source): Observable<R> =>
defer(() => {
const release = new Subject<void>()
return source.pipe(
throttle(() => release, {
leading: true,
trailing: true,
}),
exhaustMap((value, index) =>
from(project(value, index)).pipe(
finalize(() => {
release.next()
})
)
)
)
)
}
})
}

/**
Expand Down

0 comments on commit 959ae75

Please sign in to comment.