Skip to content

Instantly share code, notes, and snippets.

@kirkshoop
kirkshoop / gist:6396398
Created August 31, 2013 05:35
Excerpt from https://github.com/kirkshoop/rxaccelerometer showing how to create a ReactiveCommand and bind it to a Xaml button
typedef rxrt::EventPattern<Object^, Windows::UI::Xaml::RoutedEventArgs^> RoutedEventPattern;
// start out disabled
enabled = std::make_shared<rx::BehaviorSubject<bool>>(false);
// use enabled to control canExecute
disable = std::make_shared < rxrt::ReactiveCommand < RoutedEventPattern> >(observable(enabled));
from(observable(disable))
// stay on the ui thread
@kirkshoop
kirkshoop / reactive save
Created August 31, 2013 19:48
transformation of SaveAsync in a Windows 8.1 sample app to use RX++.
std::shared_ptr<rx::Observable<bool>> SuspensionManager::ReactiveSave(void)
{
...
// Serialize the session state synchronously to avoid asynchronous access to shared
// state
auto sessionData = ref new InMemoryRandomAccessStream();
auto sessionDataWriter = ref new DataWriter(sessionData->GetOutputStreamAt(0));
WriteObject(sessionDataWriter, _sessionState);
// one-time construction of reactive functions needed to save.
@kirkshoop
kirkshoop / save on suspending
Created September 6, 2013 05:37
Reactive code to handle the suspending event and save the state.
auto ct = std::make_shared<rx::CurrentThreadScheduler>();
typedef rxrt::EventPattern<Platform::Object^, SuspendingEventArgs^> SuspendingEventPattern;
rx::from(suspending)
.chain<rxrt::defer_operation>(
[](SuspendingEventPattern ep)
{
// defer this operation
return ep.EventArgs()->SuspendingOperation;
},
[](rxrt::OperationPattern<SuspendingOperation^>, SuspendingEventPattern)
@kirkshoop
kirkshoop / core of deferoperation
Created September 6, 2013 05:49
The core of DeferOperation
// must take the deferral early while the event is still on the stack.
auto op = make_operation_pattern(sop(t));
typedef decltype(op) OP;
return Using(
// resource factory
[=]()
{
return op;
},
@kirkshoop
kirkshoop / sob
Last active December 22, 2015 10:38
the save operation
[](rxrt::OperationPattern<SuspendingOperation^>, SuspendingEventPattern)
{
// do this while the operation is deferred
return SuspensionManager::ReactiveSave();
}
@kirkshoop
kirkshoop / sop
Created September 6, 2013 05:56
the operation to defer
[](SuspendingEventPattern ep)
{
// defer this operation
return ep.EventArgs()->SuspendingOperation;
}
@kirkshoop
kirkshoop / operationpattern
Created September 6, 2013 06:10
implementation of operation pattern
template <class O>
struct OperationPattern
{
typedef decltype(((O)nullptr)->GetDeferral()) D;
OperationPattern(O operation) :
operation(operation),
deferral(operation->GetDeferral())
{
}
@kirkshoop
kirkshoop / reactivecommand async registration and subscription
Created September 6, 2013 14:35
ReactiveCommand async registration and subscription
from(enable->RegisterAsyncFunction(
[](RoutedEventPattern)
{
// background thread
// enable and disable commands will be disabled until this is finished
std::this_thread::sleep_for(std::chrono::seconds(2));
return true;
})) // this is a subscription to the enable ReactiveCommand
@kirkshoop
kirkshoop / diplay accelerometer readings
Created September 6, 2013 15:03
Sync subscription to ReactiveCommand that subscribes to accelerometer readings and displays them.
// enable the scenario when enable is executed
from(observable(enable))
.where([this](RoutedEventPattern)
{
return accelerometer != nullptr;
})
.select_many([=](RoutedEventPattern)
{
return from(visible)
.select_many([=](bool)
@kirkshoop
kirkshoop / bind ReactiveCommand to Xaml button
Created September 6, 2013 15:05
Binding scenario buttons to the ReactiveCommand
rxrt::BindCommand(ScenarioEnableButton, enable);
rxrt::BindCommand(ScenarioDisableButton, disable);