Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribe when take* completes #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 68 additions & 27 deletions rx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1538,26 +1538,36 @@ function Observable:take(n)
end

local i = 1
local subscription

local function unsub()
if subscription then
subscription:unsubscribe()
end
end

local function onCompleted()
observer:onCompleted()
unsub()
end

local function onError(e)
observer:onError(e)
unsub()
end

local function onNext(...)
observer:onNext(...)

i = i + 1

if i > n then
observer:onCompleted()
onCompleted()
end
end

local function onError(e)
return observer:onError(e)
end

local function onCompleted()
return observer:onCompleted()
end

return self:subscribe(onNext, onError, onCompleted)
subscription = self:subscribe(onNext, onError, onCompleted)
return Subscription.create(unsub)
end)
end

Expand All @@ -1572,6 +1582,13 @@ function Observable:takeLast(count)

return Observable.create(function(observer)
local buffer = {}
local subscription

local function unsub()
if subscription then
subscription:unsubscribe()
end
end

local function onNext(...)
table.insert(buffer, util.pack(...))
Expand All @@ -1581,17 +1598,20 @@ function Observable:takeLast(count)
end

local function onError(message)
return observer:onError(message)
observer:onError(message)
unsub()
end

local function onCompleted()
for i = 1, #buffer do
observer:onNext(util.unpack(buffer[i]))
end
return observer:onCompleted()
observer:onCompleted()
unsub()
end

return self:subscribe(onNext, onError, onCompleted)
subscription = self:subscribe(onNext, onError, onCompleted)
return Subscription.create(unsub)
end)
end

Expand All @@ -1600,21 +1620,32 @@ end
-- @returns {Observable}
function Observable:takeUntil(other)
return Observable.create(function(observer)
local subscription

local function unsub()
if subscription then
subscription:unsubscribe()
end
end

local function onNext(...)
return observer:onNext(...)
end

local function onError(e)
return observer:onError(e)
observer:onError(e)
unsub()
end

local function onCompleted()
return observer:onCompleted()
observer:onCompleted()
unsub()
end

other:subscribe(onCompleted, onCompleted, onCompleted)

return self:subscribe(onNext, onError, onCompleted)
subscription = self:subscribe(onNext, onError, onCompleted)
return Subscription.create(unsub)
end)
end

Expand All @@ -1626,6 +1657,23 @@ function Observable:takeWhile(predicate)

return Observable.create(function(observer)
local taking = true
local subscription

local function unsub()
if subscription then
subscription:unsubscribe()
end
end

local function onError(message)
observer:onError(message)
unsub()
end

local function onCompleted()
observer:onCompleted()
unsub()
end

local function onNext(...)
if taking then
Expand All @@ -1636,20 +1684,13 @@ function Observable:takeWhile(predicate)
if taking then
return observer:onNext(...)
else
return observer:onCompleted()
return onCompleted()
end
end
end

local function onError(message)
return observer:onError(message)
end

local function onCompleted()
return observer:onCompleted()
end

return self:subscribe(onNext, onError, onCompleted)
subscription = self:subscribe(onNext, onError, onCompleted)
return Subscription.create(unsub)
end)
end

Expand Down Expand Up @@ -2310,4 +2351,4 @@ return {
AsyncSubject = AsyncSubject,
BehaviorSubject = BehaviorSubject,
ReplaySubject = ReplaySubject
}
}
19 changes: 19 additions & 0 deletions tests/take.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ describe('take', function()
expect(#onError).to.equal(0)
expect(#onCompleted).to.equal(1)
end)

it('unsubscribes when it completes', function ()
local keepGoing = true
local unsub = spy()
local observer

local source = Rx.Observable.create(function (_observer)
observer = _observer
return Rx.Subscription.create(unsub)
end)

source
:take(1)
:subscribe(Rx.Observer.create())
expect(#unsub).to.equal(0)

observer:onNext()
expect(#unsub).to.equal(1)
end)
end)
18 changes: 18 additions & 0 deletions tests/takeLast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ describe('takeLast', function()
it('produces no elements if the source Observable produces no elements', function()
expect(Rx.Observable.empty():takeLast(1)).to.produce.nothing()
end)

it('unsubscribes when it completes', function ()
local unsub = spy()
local observer

local source = Rx.Observable.create(function (_observer)
observer = _observer
return Rx.Subscription.create(unsub)
end)

source
:takeLast(1)
:subscribe(Rx.Observer.create())
expect(#unsub).to.equal(0)

observer:onCompleted()
expect(#unsub).to.equal(1)
end)
end)
17 changes: 17 additions & 0 deletions tests/takeUntil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,21 @@ describe('takeUntil', function()
subject:onCompleted()
expect(onNext).to.equal({})
end)

it('unsubscribes when it completes', function ()
local trigger = Rx.Subject.create()
local unsub = spy()

local source = Rx.Observable.create(function (observer)
return Rx.Subscription.create(unsub)
end)

source
:takeUntil(trigger)
:subscribe(Rx.Observer.create())
expect(#unsub).to.equal(0)

trigger()
expect(#unsub).to.equal(1)
end)
end)
23 changes: 23 additions & 0 deletions tests/takeWhile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ describe('takeWhile', function()
it('calls onError if the predicate errors', function()
expect(Rx.Observable.fromRange(3):takeWhile(error)).to.produce.error()
end)

it('unsubscribes when it completes', function ()
local keepGoing = true
local unsub = spy()
local observer

local source = Rx.Observable.create(function (_observer)
observer = _observer
return Rx.Subscription.create(unsub)
end)

source
:takeWhile(function () return keepGoing end)
:subscribe(Rx.Observer.create())
expect(#unsub).to.equal(0)

observer:onNext()
expect(#unsub).to.equal(0)

keepGoing = false
observer:onNext()
expect(#unsub).to.equal(1)
end)
end)