|
@@ -361,6 +361,44 @@ function Observable:buffer(size)
|
|
end)
|
|
end)
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+--- Returns an Observable that intercepts any errors from the previous and replace them with values
|
|
|
|
+-- produced by a new Observable.
|
|
|
|
+-- @arg {function|Observable} handler - An Observable or a function that returns an Observable to
|
|
|
|
+-- replace the source Observable in the event of an error.
|
|
|
|
+-- @returns {Observable}
|
|
|
|
+function Observable:catch(handler)
|
|
|
|
+ handler = handler and (type(handler) == 'function' and handler or util.constant(handler))
|
|
|
|
+
|
|
|
|
+ return Observable.create(function(observer)
|
|
|
|
+ local subscription
|
|
|
|
+
|
|
|
|
+ local function onNext(...)
|
|
|
|
+ return observer:onNext(...)
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ local function onError(e)
|
|
|
|
+ if not handler then
|
|
|
|
+ return observer:onCompleted()
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ local continue = handler(e)
|
|
|
|
+ if continue then
|
|
|
|
+ if subscription then subscription:unsubscribe() end
|
|
|
|
+ continue:subscribe(observer)
|
|
|
|
+ else
|
|
|
|
+ observer:onError(e)
|
|
|
|
+ end
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ local function onCompleted()
|
|
|
|
+ observer:onCompleted()
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ subscription = self:subscribe(onNext, onError, onCompleted)
|
|
|
|
+ return subscription
|
|
|
|
+ end)
|
|
|
|
+end
|
|
|
|
+
|
|
--- Returns a new Observable that runs a combinator function on the most recent values from a set
|
|
--- 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
|
|
-- of Observables whenever any of them produce a new value. The results of the combinator function
|
|
-- are produced by the new Observable.
|
|
-- are produced by the new Observable.
|