Skip to content

Commit

Permalink
feat: Added Rx.switchScan and Rx.mergeScan (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
InvisibleWater authored Nov 3, 2024
1 parent e6a8265 commit 5d0f762
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/rx/src/Shared/Rx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ function Rx.takeWhile(predicate)

return source:Subscribe(function(...)
index += 1

if predicate(index, ...) then
sub:Fire(...)
else
Expand All @@ -2043,4 +2043,44 @@ function Rx.takeWhile(predicate)
end
end

--[=[
Applies an accumulator function over the source Observable where the accumulator function itself returns an Observable,
emitting values only from the most recently returned Observable.
https://rxjs.dev/api/index/function/switchScan
@param accumulator function
@param seed any | nil
@return (source: Observable) -> Observable
]=]
function Rx.switchScan(accumulator, seed)
assert(type(accumulator) == "function", "Bad accumulator")

return Rx.pipe({
Rx.scan(accumulator, seed),
Rx.switchAll()
})
end

--[=[
Applies an accumulator function over the source Observable where the accumulator function itself returns an Observable,
then each intermediate Observable returned is merged into the output Observable.
https://rxjs.dev/api/operators/mergeScan
@param accumulator function
@param seed any | nil
@return (source: Observable) -> Observable
]=]
function Rx.mergeScan(accumulator, seed)
assert(type(accumulator) == "function", "Bad accumulator")

return Rx.pipe({
Rx.scan(accumulator, seed),
Rx.flatMap(function(x)
return x
end)
})
end

return Rx

0 comments on commit 5d0f762

Please sign in to comment.