top of page

Merging IObservable<Egg>

The Reactive Merge operator came up recently in our office during a discussion about chickens.

Actual Eight-Bot Chicago chickens inspecting their eggs

Photo credit: Eight-Bot engineer Jeff Fichtner

Like any good machine, it's a time saver because it performs a repetitive activity the same way every time. That frees up our human time so we can do more interesting things, like watch movies or play video games.

How does the machine know when to start cooking automatically? We give it a mechanism for observing the appearance of an egg and reacting to it.

In Reactive vocabulary, a hen is like an IObservable<Egg>. She lays eggs periodically – we don't know how often, or how many total eggs, or for how long. The machine subscribes to that observable, and reacts to it by performing a number of steps that put a pancake on a plate in the end. Using the ReactiveUI extensions:

But what if you have more than one hen?

We could build four separate instances of our machine and hook up one machine to each chicken. But that would require a lot of resources. Instead, we could just put a bin under all our hens that funnels any of their eggs into the same machine. Observable.Merge is like that bin that takes in several different IObservable<Egg> sequences and merges them.

We don't care which chicken the egg came from, and Observable.Merge doesn't bother to tell us. The only thing that matters is that all of the combined sequences emit the same thing – an Egg – so we can treat them the same way and do the same thing in response to any of them:

This frees up even more of our time so we can go do even more interesting things. And we get pancakes!

 

Useful Resources:

Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page