From 24450712be484dfaf13d10b57f2596b8b87097e7 Mon Sep 17 00:00:00 2001 From: Junseong Jang Date: Thu, 22 Nov 2018 18:59:54 +0900 Subject: [PATCH] Fix switch operator subscription to unsubscribe from inner subscription The switch operator should unsubscribe from inner subscribed source --- rx.lua | 19 ++++++++++++++----- src/operators/switch.lua | 19 ++++++++++++++----- tests/switch.lua | 13 +++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/rx.lua b/rx.lua index 33dc603..4ab9bfc 100644 --- a/rx.lua +++ b/rx.lua @@ -1459,7 +1459,7 @@ end -- @returns {Observable} function Observable:switch() return Observable.create(function(observer) - local subscription + local innerSubscription local function onNext(...) return observer:onNext(...) @@ -1474,14 +1474,23 @@ function Observable:switch() end local function switch(source) - if subscription then - subscription:unsubscribe() + if innerSubscription then + innerSubscription:unsubscribe() end - subscription = source:subscribe(onNext, onError, nil) + innerSubscription = source:subscribe(onNext, onError, nil) end - return self:subscribe(switch, onError, onCompleted) + local subscription = self:subscribe(switch, onError, onCompleted) + return Subscription.create(function() + if innerSubscription then + innerSubscription:unsubscribe() + end + + if subscription then + subscription:unsubscribe() + end + end) end) end diff --git a/src/operators/switch.lua b/src/operators/switch.lua index f362025..8cdea08 100644 --- a/src/operators/switch.lua +++ b/src/operators/switch.lua @@ -5,7 +5,7 @@ local Observable = require 'observable' -- @returns {Observable} function Observable:switch() return Observable.create(function(observer) - local subscription + local innerSubscription local function onNext(...) return observer:onNext(...) @@ -20,13 +20,22 @@ function Observable:switch() end local function switch(source) - if subscription then - subscription:unsubscribe() + if innerSubscription then + innerSubscription:unsubscribe() end - subscription = source:subscribe(onNext, onError, nil) + innerSubscription = source:subscribe(onNext, onError, nil) end - return self:subscribe(switch, onError, onCompleted) + local subscription = self:subscribe(switch, onError, onCompleted) + return Subscription.create(function() + if innerSubscription then + innerSubscription:unsubscribe() + end + + if subscription then + subscription:unsubscribe() + end + end) end) end diff --git a/tests/switch.lua b/tests/switch.lua index 884d900..227a2fa 100644 --- a/tests/switch.lua +++ b/tests/switch.lua @@ -36,4 +36,17 @@ describe('switch', function() expect(#onError).to.equal(0) expect(#onCompleted).to.equal(1) end) + + it('should unsubscribe from inner subscription too', function() + local unsubscribeA = spy() + local observableA = Rx.Observable.create(function(observer) + return Rx.Subscription.create(unsubscribeA) + end) + + local subject = Rx.Subject.create() + local subscription = subject:switch():subscribe() + subject:onNext(observableA) + subscription:unsubscribe() + expect(#unsubscribeA).to.equal(1) + end) end)