Przeglądaj źródła

Add Observable.concat; Update README; Add concat example;

bjorn 10 lat temu
rodzic
commit
bab9d281bd
4 zmienionych plików z 57 dodań i 13 usunięć
  1. 2 13
      README.md
  2. 14 0
      doc/README.md
  3. 11 0
      examples/concat.lua
  4. 30 0
      rx.lua

+ 2 - 13
README.md

@@ -11,21 +11,10 @@ Cheer someone on using functional reactive programming:
 ```lua
 local Rx = require 'rx'
 
-local observable = Rx.Observable.fromCoroutine(function()
-  for i = 2, 8, 2 do
-    coroutine.yield(i)
-  end
-
-  return 'who do we appreciate'
-end)
-
-observable
+Rx.Observable.fromRange(2, 8, 2)
+  :concat(Rx.Observable.fromValue('who do we appreciate'))
   :map(function(value) return value .. '!' end)
   :subscribe(print)
-
-repeat
-  Rx.scheduler:update()
-until Rx.scheduler:isEmpty()
 ```
 
 See [examples](examples) for more.

+ 14 - 0
doc/README.md

@@ -160,6 +160,20 @@ Returns:
 
 ---
 
+#### `:concat(observables)`
+
+Returns a new Observable that produces the values produced by all the specified Observables in the order they are specified.
+
+Arguments:
+
+- `observables` (`Observable...`) - The Observables to concatenate.
+
+Returns:
+
+- `Observable`
+
+---
+
 #### `:distinct()`
 
 Returns a new Observable that produces the values from the original with duplicates removed.

+ 11 - 0
examples/concat.lua

@@ -0,0 +1,11 @@
+local Rx = require 'rx'
+
+local first = Rx.Observable.fromRange(3)
+local second = Rx.Observable.fromRange(4, 6)
+local third = Rx.Observable.fromRange(7, 11, 2)
+
+first:concat(second, third):dump('concat')
+
+print('Equivalent to:')
+
+Rx.Observable.concat(first, second, third):dump('concat')

+ 30 - 0
rx.lua

@@ -209,6 +209,36 @@ function Observable:combineLatest(...)
   end)
 end
 
+--- Returns a new Observable that produces the values produced by all the specified Observables in
+-- the order they are specified.
+-- @arg {Observable...} observables - The Observables to concatenate.
+-- @returns {Observable}
+function Observable:concat(other, ...)
+  if not other then return self end
+
+  local others = {...}
+
+  return Observable.create(function(observer)
+    local function onNext(value)
+      return observer:onNext(value)
+    end
+
+    local function onError(message)
+      return observer:onError(message)
+    end
+
+    local function onComplete()
+      return observer:onComplete()
+    end
+
+    local function chain()
+      return other:concat(unpack(others)):subscribe(onNext, onError, onComplete)
+    end
+
+    return self:subscribe(onNext, onError, chain)
+  end)
+end
+
 --- Returns a new Observable that produces the values from the original with duplicates removed.
 -- @returns {Observable}
 function Observable:distinct()