The from static method

The fromArray function is one of many static factory methods that are provided by the Flux class. If we want to create our own source of events, we can use the from function. Let's create an instance of the Flux class that emits the Unit object when a user clicks on the button.

We can implement this case as follows:

Flux.from<Unit> { subscriber ->
findViewById<Button>(R.id.button).setOnClickListener {
subscriber.onNext(Unit)
}
}.subscribe {
Toast.makeText(this, "Clicked!", Toast.LENGTH_LONG).show()
}

The preceding snippet shows how to wrap an Observer into an instance of the Flux class. This example illustrates using the from function to create a new instance of the Flux class.

Let's run an application and press the THE OBSERVER PATTERN button:

The preceding screenshot shows how an example works. When a user clicks the button, the onNext method is invoked and the Observable emits a value. The lambda that we passed to the subscribe method is invoked, and it shows a message.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208