Reactive Mythology: Interrupt Patterns

Shashank Tomar
ProAndroidDev
Published in
5 min readOct 13, 2017

--

Part of Reactive Mythology series

As a part of interrupts, we will be covering the following topics:

  • Stop task on interrupt
  • Throw errors on some interrupt
  • Pause and Resume streams based on interrupts

Note: The examples here are in Kotlin and can easily be transformed to other languages.

Meet the Kiboshi clan of Reactula. They are hard to miss and are famous for their anger issues and geeky science experiments.

The Reactive Gods have chosen them to build a spaceship for the epic battle, “The Invasion by Earthlings”. The Kiboshi’s couldn’t have asked for more and killing aliens was on the top of their ‘Things to do before you die’ list. Thus, they start building an epic war machine, a spaceship that could fire at a given constant rate.

Here is the Kiboshi’s first attempt at the warship:

The dream of popping aliens was never so close, but the Reactive Gods are never easy to please, are they?

Stop task on interrupt

The God Of Time, Gogoti doesn’t like wars. He is a sleepy, compassionate guy, who wants his subjects to chill and get drunk as soon as the sun sets. He commands the Kiboshi’s to shut down their war machine when he signals dusk.

And since this is planet Reactula, everything here is a stream. Gogoti promise to hand the Kiboshi’s an observable for the time of the day, which will signal dawn, afternoon or dusk.

The Kiboshi’s are distressed. They’ve built this beautiful spaceship which can fire for eternity. They were so obsessed with the thought of killing the aliens, that the idea to stop firing never crossed their minds. But the Reactive Gods must be pleased, and just before the Kiboshi’s were getting drunk and falling into oblivion, they heard a whisper - takeUntil.

After few drinks and beating up historians, they found the sacred scripture describing takeUntil.

This hint was enough for the Kiboshi’s. They have now changed their spaceship to use takeUntil and as expected, it stops firing at dusk.

Gogoti is pleased, and the Kiboshi’s are back on their mission of killing aliens and living a happy drunk life. The warship they built, has been firing for the whole day without any problem. All of Reactula is celebrating their inevitable victory over the aliens. Glory songs are being written for the Kiboshi’s and they are flooded with marriage offers.

But life on Reactula is unexpected.

Throw error on some interrupt

Wars are never that easy. They receive a distress call from one of the watch towers, a Kiboshi clan member, who has some disoriented love for the Earthlings was spotted in the firing zone.

They cannot kill a friendly unit, that’s an eternal sin. They know that with a great warship, comes great responsibility. The Kiboshi’s quickly gather to find a solution, but they are too drunk to think straight. This reminds them of the great blessing Goddess Dukuria gave them: All it will take to find a solution, will be one crazy drunk Kiboshi.

The Kiboshi’s never thought of the merge this way. The merge wisdom says “Thou shalt marry and produce together”. But because of the consensus required, it can also be used to stop emitting on error.

The watchtowers handed them an observable of error which fires nothing but a FriendlyFireException if a friendly unit is spotted in the firing zone.

And the Kiboshi’s immediately modified their warship to use it (yes they follow Continuous Delivery).

Any precious Reactula life that enters the war zone will be saved. Everyone is back to celebrating.

Pause and resume on interrupts

The Kiboshi’s had a great sleep. The next morning, they are ready to resume the war and see their glorified warship bring havoc to aliens. But rather, they see the furious Gogoti. Their warship has no mechanism to resume at dawn.

The Kiboshi’s need to find a way to pause and resume the warship, it should be paused at dusk and resumed at dawn. They realize that this wouldn’t be easy. The historians already hate them, and drinking won’t help this time. Gogoti is angry, and messing up with the God of Time can be costly. If he’s not pleased, he can twist their Frame of Relativity, which is the last thing they want. They have to look for inner peace to find the answer.

They send a clan member across the sea to live among the monks, meditate and seek the truth of pause-able streams.

At the monastery, the Kiboshi clan member understood that Transformer, a powerful concept, when combined with compose, can be used to chain complicated reactive constructs. But there was still a missing piece to the solution and he was anxious to find it. He came across one of the fundamental learnings in the monastery: To seek truth, you have to let go of your preconceived opinions, and learn to give up your closely held attachments, as the nature doesn’t have any obligation to confirm to your opinions. Yeah thats too much to fit in a Kiboshi’s head, but this reminds him of an old relationship advise a friend once gave.

That’s all it takes for him to attain wisdom. He comes back to the clan to spread the knowledge.

Transformer, compose and switchOnNext can be used in a generic way to pause and resume streams. This will serve the Kiboshi’s for coming generations.

The pause event unsubscribes from the source stream and the resume event resubscribes to it. The Kiboshi’s easily transformed the timeOfDay stream into a pause resume event stream.

And they modified their warship to pause and resume on dusk and dawn events.

Reactula is again on its path to a happy life full of streams. The warship can fire till eternity, and the Kiboshi’s are already being printed on Reactula currency. But as we know, the life on Reactula is unpredictable.

--

--