This is a Scala adapter to RxJs.
val o = Observable.interval(200).take(5) o.subscribe(n => println("n = " + n)) Observable.just(1, 2, 3, 4).reduce(_ + _)
Example usage in Browser:
Observable.fromEvent(document.getElementById("btn"),"click") .mapTo(1) .scan(0)(_ + _) .subscribe(n => println(s"Clicked $n times"))
Add the following to your sbt build definition:
libraryDependencies += "com.github.lukajcb" %%% "rxscala-js" % "0.15.0"
then import the types from the package
RxScala.js doesn't actually come bundled with the underlying
rx.js file, so you'll need to either add them manually or specify them as
jsDependencies += "org.webjars.npm" % "rxjs" % "5.4.0" / "bundles/Rx.min.js" commonJSName "Rx"
Differences from RxJS
Similary to RxScala, this wrapper attempts to expose an API which is as Scala-idiomatic as possible. Some examples:
// instead of concat: def ++[U >: T](that: Observable[U]): Observable[U] // curried in Scala collections, so curry fold also here: def foldLeft[R](seed: R)(accumulator: (R, T) => R): Observable[R] // called skip in RxJS, but drop in Scala def drop(n: Int): Observable[T] // like in the collection API def zipWithIndex: Observable[(T, Int)] // the implicit evidence argument ensures that switch can only be called on Observables of Observables: def switch[U](implicit evidence: Observable[T] <:< Observable[Observable[U]]): Observable[U]
- The API documentation can be found here.
If you're new to Rx, I suggest starting with this interactive tutorial.
- The basics - How to use some of the most important operations in RxScala.js
- Spaceship Reactive - A port of Spaceship Reactive found in Sergi Mansillas awesome book Reactive Programming with RxJS. Code can be found here.
- RxScala.js as a state store - A basic example on how to write a simple state store with RxScala.js. (Find the working example here)
Bugs and Feedback
For bugs, questions and discussions please use the Github Issues.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.