A small tour through ScalaRx (Part II)

A fortnight ago, we took a look at some of the functionalities that ScalaRx library provides us with. Today, we are going to end this small tour by getting on with Observables and all the advantages they offer.

Previously on Scalera…

In the previous post we saw how reactive variables work.  We learned how to declare them, modify them and create Rx structures that were modified when the reactive variables underwent a change.

The main idea of the post was that the objective of the ScalaRx library was to interpret reactive variables and their use as a flow that propagates changes whenever there is an update in any of the reactive variables. We can think of it as a butterfly effect.

Now, let’s see how we can make use of such changes with the Observable type.

giphy

The Observable type

By means of the Observable type, we will be able to perform certain actions when a reactive variable changes its value. For that purpose, we can create observations both of a Var and of Rx blocks. There are two constructors: trigger and foreach. With the first of them, we won’t have a reference to the new value. In order to get it, we would have to use a call to .now. On the other hand, with foreach we will have a reference to the new flow-propagated value.

Let’s create a pair of Observables which show on screen the new value. For that purpose, we will use the two versions of a constructor:

import rx._
val reactiveVar = Var("Hello World")
val o1 = reactiveVar.trigger {
  println(reactiveVar.now)
}
val o2 = reactiveVar.foreach { newValue =>
  println(newValue)
}

This example, once executed, will show on screen two “Hello World” given that the observables also act in the initialization of the reactive variable. If we want to miss the first change of the reactive variable, we can use triggerLater.

import rx._
val reactiveVar = Var("Hello World")
val o1 = reactiveVar.triggerLater {
  println(reactiveVar.now)
}

Finally, if we want to eliminate an Observable, we can use the kill instruction.

import rx._
val reactiveVar = Var("Hello World")
val o1 = reactiveVar.triggerLater {
 println(reactiveVar.now)
}
o1.kill()

By doing this, there won’t be any further reactions to changes in the reactive variable.

Conclusions

After these two posts, we can say that ScalaRx is based on data flows with several elements. As ScalaRx documentation rightly states, those elements can be seen as:

  • Var: root flow nodes that will provoke an avalanche with their changes.
  • Rx: intermediate flow nodes that will react to changes.
  • Observables: leaf flow nodes that will react to changes without propagating the flow further.

We can make use of this abstraction to create different types of applications or tools. For instance, we could have some dynamic configuration parameters (Var), being used by other variables (Rx) so that, if changes occur to any of them, a series of actions will be performed in order to be able to adapt to the new configuration:

import rx._
val host = Var(etcdHost)
val port = Var(etcdPort)
val databaseUri = Rx {host() + ":" + port()}
val obsDatabaseUri = databaseUri.triggerLater {
  connection.resetWith(<wbr>databaseUri.now())
}

This can be a common pattern to adapt to dynamic configuration parameters coming from ETCD or Consul, for instance.

And this is just an example. Obviously, we can do many more complex things with the flow started by changes in reactive variables. But we’ll leave that to future posts. Peace out!

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s