|
@@ -247,6 +247,55 @@ function Observable:all(predicate)
|
|
|
end)
|
|
|
end
|
|
|
|
|
|
+--- Given a set of Observables, produces values from only the first one to produce a value.
|
|
|
+-- @arg {Observable...} observables
|
|
|
+-- @returns {Observable}
|
|
|
+function Observable.amb(a, b, ...)
|
|
|
+ if not a or not b then return a end
|
|
|
+
|
|
|
+ return Observable.create(function(observer)
|
|
|
+ local subscriptionA, subscriptionB
|
|
|
+
|
|
|
+ local function onNextA(...)
|
|
|
+ if subscriptionB then subscriptionB:unsubscribe() end
|
|
|
+ observer:onNext(...)
|
|
|
+ end
|
|
|
+
|
|
|
+ local function onErrorA(e)
|
|
|
+ if subscriptionB then subscriptionB:unsubscribe() end
|
|
|
+ observer:onError(e)
|
|
|
+ end
|
|
|
+
|
|
|
+ local function onCompletedA()
|
|
|
+ if subscriptionB then subscriptionB:unsubscribe() end
|
|
|
+ observer:onCompleted()
|
|
|
+ end
|
|
|
+
|
|
|
+ local function onNextB(...)
|
|
|
+ if subscriptionA then subscriptionA:unsubscribe() end
|
|
|
+ observer:onNext(...)
|
|
|
+ end
|
|
|
+
|
|
|
+ local function onErrorB(e)
|
|
|
+ if subscriptionA then subscriptionA:unsubscribe() end
|
|
|
+ observer:onError(e)
|
|
|
+ end
|
|
|
+
|
|
|
+ local function onCompletedB()
|
|
|
+ if subscriptionA then subscriptionA:unsubscribe() end
|
|
|
+ observer:onCompleted()
|
|
|
+ end
|
|
|
+
|
|
|
+ subscriptionA = a:subscribe(onNextA, onErrorA, onCompletedA)
|
|
|
+ subscriptionB = b:subscribe(onNextB, onErrorB, onCompletedB)
|
|
|
+
|
|
|
+ return Subscription.create(function()
|
|
|
+ subscriptionA:unsubscribe()
|
|
|
+ subscriptionB:unsubscribe()
|
|
|
+ end)
|
|
|
+ end):amb(...)
|
|
|
+end
|
|
|
+
|
|
|
--- Returns a new Observable that runs a combinator function on the most recent values from a set
|
|
|
-- of Observables whenever any of them produce a new value. The results of the combinator function
|
|
|
-- are produced by the new Observable.
|
|
@@ -1180,6 +1229,15 @@ function Subject:subscribe(onNext, onError, onCompleted)
|
|
|
end
|
|
|
|
|
|
table.insert(self.observers, observer)
|
|
|
+
|
|
|
+ return Subscription.create(function()
|
|
|
+ for i = 1, #self.observers do
|
|
|
+ if self.observers[i] == observer then
|
|
|
+ table.remove(self.observers, i)
|
|
|
+ return
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end)
|
|
|
end
|
|
|
|
|
|
--- Pushes zero or more values to the Subject. They will be broadcasted to all Observers.
|