Events

Events

  • Install the appropriate reactiveui-events-* package into your application.
  • You can use the events package standlone, without any references to ReactiveUI. reactiveui-events-* will always be a seperate package that has no dependancy on thereactiveui package.
  • This package looks at all the EventHandlers for a platform and generates Observable.FromEventPattern extension methods via this Moustache template.
  • Don't use EventHandlers ever, use the generated Observable.FromEventPattern versions.
  • Combine multiple Observable.FromEventPatterntogether to get amazing composition.
  • Remember to dispose of your subscriptions using the features provided by the Reactive Extensions.
var codes = new[]
{
    Key.Up,
    Key.Up,
    Key.Down,
    Key.Down,
    Key.Left,
    Key.Right,
    Key.Left,
    Key.Right,
    Key.A,
    Key.B
};

// convert the array into an sequence
var koanmi = codes
    .ToObservable();

this.Events().KeyUp

    // we want the keycode
    .Select(x => x.Key)
    .Do(key => Debug.WriteLine($"{key} was pressed."))

    // get the last ten keys
    .Window(10)

    // compare to known konami code sequence
    .SelectMany(x => x.SequenceEqual(koanmi))
    .Do(isMatch => Debug.WriteLine(isMatch))

    // where we match
    .Where(x => x)
    .Do(x => Debug.WriteLine("Konami sequence"))
    .Subscribe(y => { });

Source-code for this meetup talk

See Also