Browse Source

Observable.replicate;

bjorn 10 years ago
parent
commit
3b3a298d02
6 changed files with 64 additions and 0 deletions
  1. 12 0
      doc/README.md
  2. 17 0
      rx.lua
  3. 1 0
      src/aliases.lua
  4. 17 0
      src/observable.lua
  5. 2 0
      src/operators/replicate.lua
  6. 15 0
      tests/observable.lua

+ 12 - 0
doc/README.md

@@ -20,6 +20,7 @@ RxLua
   - [fromTable](#fromtabletable-iterator-keys)
   - [fromCoroutine](#fromcoroutinecoroutine)
   - [defer](#deferfactory)
+  - [replicate](#replicatevalue-count)
   - [dump](#dumpname-formatter)
   - [all](#allpredicate)
   - [amb](#ambobservables)
@@ -246,6 +247,17 @@ Creates an Observable that creates a new Observable for each observer using a fa
 
 ---
 
+#### `.replicate(value, count)`
+
+Returns an Observable that repeats a value a specified number of times.
+
+| Name | Type | Default | Description |
+|------|------|---------|-------------|
+| `value` | * |  | The value to repeat. |
+| `count` | number (optional) |  | The number of times to repeat the value.  If left unspecified, the value is repeated an infinite number of times. |
+
+---
+
 #### `:dump(name, formatter)`
 
 Subscribes to this Observable and prints values it produces.

+ 17 - 0
rx.lua

@@ -219,6 +219,23 @@ function Observable.defer(fn)
   }, Observable)
 end
 
+--- Returns an Observable that repeats a value a specified number of times.
+-- @arg {*} value - The value to repeat.
+-- @arg {number=} count - The number of times to repeat the value.  If left unspecified, the value
+--                        is repeated an infinite number of times.
+-- @returns {Observable}
+function Observable.replicate(value, count)
+  return Observable.create(function(observer)
+    while count == nil or count > 0 do
+      observer:onNext(value)
+      if count then
+        count = count - 1
+      end
+    end
+    observer:onCompleted()
+  end)
+end
+
 --- Subscribes to this Observable and prints values it produces.
 -- @arg {string=} name - Prefixes the printed messages with a name.
 -- @arg {function=tostring} formatter - A function that formats one or more values to be printed.

+ 1 - 0
src/aliases.lua

@@ -1,3 +1,4 @@
 local Observable = require 'observable'
 
 Observable.wrap = Observable.buffer
+Observable['repeat'] = Observable.replicate

+ 17 - 0
src/observable.lua

@@ -134,6 +134,23 @@ function Observable.defer(fn)
   }, Observable)
 end
 
+--- Returns an Observable that repeats a value a specified number of times.
+-- @arg {*} value - The value to repeat.
+-- @arg {number=} count - The number of times to repeat the value.  If left unspecified, the value
+--                        is repeated an infinite number of times.
+-- @returns {Observable}
+function Observable.replicate(value, count)
+  return Observable.create(function(observer)
+    while count == nil or count > 0 do
+      observer:onNext(value)
+      if count then
+        count = count - 1
+      end
+    end
+    observer:onCompleted()
+  end)
+end
+
 --- Subscribes to this Observable and prints values it produces.
 -- @arg {string=} name - Prefixes the printed messages with a name.
 -- @arg {function=tostring} formatter - A function that formats one or more values to be printed.

+ 2 - 0
src/operators/replicate.lua

@@ -0,0 +1,2 @@
+local Observable = require 'observable'
+

+ 15 - 0
tests/observable.lua

@@ -210,6 +210,21 @@ describe('Observable', function()
     end)
   end)
 
+  describe('replicate', function()
+    it('returns an Observable', function()
+      expect(Rx.Observable.replicate()).to.be.an(Rx.Observable)
+    end)
+
+    it('returns an Observable that produces the first argument a specified number of times', function()
+      expect(Rx.Observable.replicate(1, 3)).to.produce(1, 1, 1)
+    end)
+
+    it('produces nothing if the count is less than or equal to zero', function()
+      expect(Rx.Observable.replicate(1, 0)).to.produce.nothing()
+      expect(Rx.Observable.replicate(1, -1)).to.produce.nothing()
+    end)
+  end)
+
   describe('dump', function()
   end)