Explorar o código

Observable.defaultIfEmpty;

bjorn %!s(int64=9) %!d(string=hai) anos
pai
achega
88579fc917
Modificáronse 6 ficheiros con 96 adicións e 0 borrados
  1. 11 0
      doc/README.md
  2. 32 0
      rx.lua
  3. 34 0
      src/operators/defaultIfEmpty.lua
  4. 17 0
      tests/defaultIfEmpty.lua
  5. 1 0
      tests/observable.lua
  6. 1 0
      tools/concat.lua

+ 11 - 0
doc/README.md

@@ -30,6 +30,7 @@ RxLua
   - [concat](#concatsources)
   - [contains](#containsvalue)
   - [count](#countpredicate)
+  - [defaultIfEmpty](#defaultifemptyvalues)
   - [distinct](#distinct)
   - [distinctUntilChanged](#distinctuntilchangedcomparator)
   - [filter](#filterpredicate)
@@ -333,6 +334,16 @@ Returns an Observable that produces a single value representing the number of va
 
 ---
 
+#### `:defaultIfEmpty(values)`
+
+Returns a new Observable that produces a default set of items if the source Observable produces no values.
+
+| Name | Type | Default | Description |
+|------|------|---------|-------------|
+| `values` | *... |  | Zero or more values to produce if the source completes without emitting anything. |
+
+---
+
 #### `:distinct()`
 
 Returns a new Observable that produces the values from the original with duplicates removed.

+ 32 - 0
rx.lua

@@ -555,6 +555,38 @@ function Observable:count(predicate)
   end)
 end
 
+--- Returns a new Observable that produces a default set of items if the source Observable produces
+-- no values.
+-- @arg {*...} values - Zero or more values to produce if the source completes without emitting
+--                      anything.
+-- @returns {Observable}
+function Observable:defaultIfEmpty(...)
+  local defaults = util.pack(...)
+
+  return Observable.create(function(observer)
+    local hasValue = false
+
+    local function onNext(...)
+      hasValue = true
+      observer:onNext(...)
+    end
+
+    local function onError(e)
+      observer:onError(e)
+    end
+
+    local function onCompleted()
+      if not hasValue then
+        observer:onNext(util.unpack(defaults))
+      end
+
+      observer:onCompleted()
+    end
+
+    return self:subscribe(onNext, onError, onCompleted)
+  end)
+end
+
 --- Returns a new Observable that produces the values from the original with duplicates removed.
 -- @returns {Observable}
 function Observable:distinct()

+ 34 - 0
src/operators/defaultIfEmpty.lua

@@ -0,0 +1,34 @@
+local Observable = require 'observable'
+local util = require 'util'
+
+--- Returns a new Observable that produces a default set of items if the source Observable produces
+-- no values.
+-- @arg {*...} values - Zero or more values to produce if the source completes without emitting
+--                      anything.
+-- @returns {Observable}
+function Observable:defaultIfEmpty(...)
+  local defaults = util.pack(...)
+
+  return Observable.create(function(observer)
+    local hasValue = false
+
+    local function onNext(...)
+      hasValue = true
+      observer:onNext(...)
+    end
+
+    local function onError(e)
+      observer:onError(e)
+    end
+
+    local function onCompleted()
+      if not hasValue then
+        observer:onNext(util.unpack(defaults))
+      end
+
+      observer:onCompleted()
+    end
+
+    return self:subscribe(onNext, onError, onCompleted)
+  end)
+end

+ 17 - 0
tests/defaultIfEmpty.lua

@@ -0,0 +1,17 @@
+describe('defaultIfEmpty', function()
+  it('errors if the source errors', function()
+    expect(Rx.Observable.throw():defaultIfEmpty(1).subscribe).to.fail()
+  end)
+
+  it('produces the values from the source unchanged if at least one value is produced', function()
+    expect(Rx.Observable.fromRange(3):defaultIfEmpty(7)).to.produce(1, 2, 3)
+  end)
+
+  it('produces the values specified if the source produces no values', function()
+    expect(Rx.Observable.empty():defaultIfEmpty(7, 8)).to.produce({{7, 8}})
+  end)
+
+  it('does not freak out if no values are specified', function()
+    expect(Rx.Observable.empty():defaultIfEmpty()).to.produce({{}})
+  end)
+end)

+ 1 - 0
tests/observable.lua

@@ -187,6 +187,7 @@ describe('Observable', function()
   dofile('tests/concat.lua')
   dofile('tests/contains.lua')
   dofile('tests/count.lua')
+  dofile('tests/defaultIfEmpty.lua')
   dofile('tests/distinct.lua')
   dofile('tests/distinctUntilChanged.lua')
   dofile('tests/filter.lua')

+ 1 - 0
tools/concat.lua

@@ -16,6 +16,7 @@ local files = {
   'src/operators/concat.lua',
   'src/operators/contains.lua',
   'src/operators/count.lua',
+  'src/operators/defaultIfEmpty.lua',
   'src/operators/distinct.lua',
   'src/operators/distinctUntilChanged.lua',
   'src/operators/filter.lua',