Skip to content

Instantly share code, notes, and snippets.

@kirkshoop
Created September 6, 2013 15:09
Show Gist options
  • Save kirkshoop/6465167 to your computer and use it in GitHub Desktop.
Save kirkshoop/6465167 to your computer and use it in GitHub Desktop.
Create state sequences to control behavior of the scenario
// when enable or disable is executing mark as working (both commands should be disabled)
observable(from(enable->IsExecuting())
.combine_latest([](bool ew, bool dw)
{
return ew || dw;
}, disable->IsExecuting()))
->Subscribe(observer(working));
// when enable is executed mark the scenario enabled, when disable is executed mark the scenario disabled
observable(from(observable(enable))
.select([this](RoutedEventPattern)
{
return accelerometer != nullptr;
})
.merge(observable(from(observable(disable))
.select([](RoutedEventPattern)
{
return false;
}))))
->Subscribe(observer(enabled)); // this is a subscription to the enable and disable ReactiveCommands
typedef TypedEventHandler<Accelerometer^, AccelerometerReadingChangedEventArgs^> AccelerometerReadingChangedTypedEventHandler;
auto readingChanged = from(rxrt::FromEventPattern<AccelerometerReadingChangedTypedEventHandler>(
[this](AccelerometerReadingChangedTypedEventHandler^ h)
{
return this->accelerometer->ReadingChanged += h;
},
[this](Windows::Foundation::EventRegistrationToken t)
{
this->accelerometer->ReadingChanged -= t;
}))
.select([](rxrt::EventPattern<Accelerometer^, AccelerometerReadingChangedEventArgs^> e)
{
// on sensor thread
return e.EventArgs()->Reading;
})
// push readings to ui thread
.observe_on_dispatcher()
.publish()
.ref_count();
auto currentWindow = Window::Current;
auto visiblityChanged = from(rxrt::FromEventPattern<WindowVisibilityChangedEventHandler, VisibilityChangedEventArgs>(
[currentWindow](WindowVisibilityChangedEventHandler^ h)
{
return currentWindow->VisibilityChanged += h;
},
[currentWindow](Windows::Foundation::EventRegistrationToken t)
{
currentWindow->VisibilityChanged -= t;
}))
.select([](rxrt::EventPattern<Platform::Object^, VisibilityChangedEventArgs^> e)
{
return e.EventArgs()->Visible;
})
.publish()
.ref_count();
auto visible = from(visiblityChanged)
.where([](bool v)
{
return !!v;
}).merge(
from(observable(navigated))
.where([](bool n)
{
return n;
}));
auto invisible = from(visiblityChanged)
.where([](bool v)
{
return !v;
});
// the scenario ends when:
auto endScenario =
// - disable is executed
from(observable(disable))
.select([](RoutedEventPattern)
{
return true;
}).merge(
// - the scenario is navigated from
from(observable(navigated))
.where([](bool n)
{
return !n;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment