You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
Lua
43 lines
1.2 KiB
Lua
local Observable = require "rx.observable"
|
|
local Subscription = require "rx.subscription"
|
|
|
|
--- Given an Observable that produces Observables, returns an Observable that produces the values
|
|
-- produced by the most recently produced Observable.
|
|
-- @returns {Observable}
|
|
function Observable:switch()
|
|
return Observable.create(function(observer)
|
|
local innerSubscription
|
|
|
|
local function onNext(...)
|
|
return observer:onNext(...)
|
|
end
|
|
|
|
local function onError(message)
|
|
return observer:onError(message)
|
|
end
|
|
|
|
local function onCompleted()
|
|
return observer:onCompleted()
|
|
end
|
|
|
|
local function switch(source)
|
|
if innerSubscription then
|
|
innerSubscription:unsubscribe()
|
|
end
|
|
|
|
innerSubscription = source:subscribe(onNext, onError, nil)
|
|
end
|
|
|
|
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
|