Skip to content

Commit

Permalink
Merge pull request #41 from StrangePan/issue-40
Browse files Browse the repository at this point in the history
Fix zip operators not returning a subscription, issue #40
  • Loading branch information
bjornbytes authored May 7, 2019
2 parents cf92f8e + 823c952 commit 7836365
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 8 additions & 1 deletion rx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,7 @@ function Observable.zip(...)
return Observable.create(function(observer)
local values = {}
local active = {}
local subscriptions = {}
for i = 1, count do
values[i] = {n = 0}
active[i] = true
Expand Down Expand Up @@ -1858,8 +1859,14 @@ function Observable.zip(...)
end

for i = 1, count do
sources[i]:subscribe(onNext(i), onError, onCompleted(i))
subscriptions[i] = sources[i]:subscribe(onNext(i), onError, onCompleted(i))
end

return Subscription.create(function()
for i = 1, count do
if subscriptions[i] then subscriptions[i]:unsubscribe() end
end
end)
end)
end

Expand Down
9 changes: 8 additions & 1 deletion src/operators/zip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function Observable.zip(...)
return Observable.create(function(observer)
local values = {}
local active = {}
local subscriptions = {}
for i = 1, count do
values[i] = {n = 0}
active[i] = true
Expand Down Expand Up @@ -60,7 +61,13 @@ function Observable.zip(...)
end

for i = 1, count do
sources[i]:subscribe(onNext(i), onError, onCompleted(i))
subscriptions[i] = sources[i]:subscribe(onNext(i), onError, onCompleted(i))
end

return Subscription.create(function()
for i = 1, count do
if subscriptions[i] then subscriptions[i]:unsubscribe() end
end
end)
end)
end
19 changes: 19 additions & 0 deletions tests/zip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ describe('zip', function()
expect(Rx.Observable.fromRange(1, 5):zip()).to.produce(1, 2, 3, 4, 5)
end)

it('unsubscribes from all input observables', function()
local unsubscribeA = spy()
local subscriptionA = Rx.Subscription.create(unsubscribeA)
local observableA = Rx.Observable.create(function(observer)
return subscriptionA
end)

local unsubscribeB = spy()
local subscriptionB = Rx.Subscription.create(unsubscribeB)
local observableB = Rx.Observable.create(function(observer)
return subscriptionB
end)

local subscription = Rx.Observable.zip(observableA, observableB):subscribe()
subscription:unsubscribe()
expect(#unsubscribeA).to.equal(1)
expect(#unsubscribeB).to.equal(1)
end)

it('groups values produced by the sources by their index', function()
local observableA = Rx.Observable.fromRange(1, 3)
local observableB = Rx.Observable.fromRange(2, 4)
Expand Down

0 comments on commit 7836365

Please sign in to comment.