HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

RX fluent subscribing to observables

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
observablesfluentsubscribing

Problem

I have three observables:

var itemsManipulationStarted =
                Observable.FromEvent(Items, "ManipulationStarted");

var pageManipulationDelta = Observable.FromEvent(this, "ManipulationDelta");

var pageManipulationCompleted =
                Observable.FromEvent(this, "ManipulationCompleted");


They are always called in that sequense: itemsManipulationStarted -> pageManipulationDelta -> pageManipulationCompleted

After every call to any observable I need to make some actions:

itemsManipulationStarted.Subscribe(e => Items.IsHitTestVisible = false);

pageManipulationDelta.SkipUntil(itemsManipulationStarted)
                     .Subscribe(e => e.EventArgs.Complete());

pageManipulationCompleted.SkipUntil(pageManipulationDelta)
                         .Subscribe(e => Items.IsHitTestVisible = true);


Is it clear or is there another way to write some "fluent" subscribing?

Solution

If you're on C# 5.0, you can use await with IObservables to write code like this:

await itemsManipulationStarted.FirstAsync();
Items.IsHitTestVisible = false;

var e = await pageManipulationDelta.FirstAsync();
e.EventArgs.Complete();

await pageManipulationCompleted.FirstAsync();
Items.IsHitTestVisible = true;


Though I'm not completely sure this will do what you want, because of timing.

Code Snippets

await itemsManipulationStarted.FirstAsync();
Items.IsHitTestVisible = false;

var e = await pageManipulationDelta.FirstAsync();
e.EventArgs.Complete();

await pageManipulationCompleted.FirstAsync();
Items.IsHitTestVisible = true;

Context

StackExchange Code Review Q#64376, answer score: 4

Revisions (0)

No revisions yet.