Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time to integrate Rx with .NET #1197

Open
weitzhandler opened this issue Apr 24, 2020 · 6 comments
Open

Time to integrate Rx with .NET #1197

weitzhandler opened this issue Apr 24, 2020 · 6 comments

Comments

@weitzhandler
Copy link
Contributor

weitzhandler commented Apr 24, 2020

Feature request

Please integrate System.Reactive into .NET and Standard, and eventually integrate with the .NET runtime core and languages.

Currently, events in C# can only be targeted either within their declaration class, or with the += and -= operators.

This limitation results in plenty of tedious and redundant code, and a lot of verbosity. For example:

Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
  h = PropertyChanged += h,
  h = PropertyChanged -= h)

If we'd integrate Rx in .NET, we could end up removing such limitations, and reach the final goal where we're able to do something like the following:

EventPattern<TextChangedEventArgs> observable = myTextBox.TextChanged;

Hence:

var observable = 
  myTexBox
  .TextChanged
  .Throttle(TimeSpan.FromSeconds(.5))
  .DistinctUntilChanged()
  .Select(ep => (TextBox)ep.Sender))
  .Select(tb => tb.Text)
  .Where(text => text != null && text.Length > 3)
  .Select(text => text.ToLower());

var subscription = 
  observable.Subscribe(text => MessgeBox.Show(text));

See C# Suggestion issue.
Related

Bringing Rx to C# events will make it a super powerful language, especially around UIs.

@richbryant
Copy link

but this is like saying "please integrate Dapper into .NET". Only the .NET team can make that kind of call.

@weitzhandler
Copy link
Contributor Author

You can't compare dotnet/reactive to StackExchange/Dapper.

@deviousasti
Copy link

F# already has this feature, so it's not too far-fetched for C# to have it.
F# converts events into an IEvent<TDelegate, TArgs> which exposes an AddHandler method, as well as the IObservable.Subscribe method.

There's an Event module that lets you compose events.

window.KeyPress 
|> Event.filter(fun evt -> evt.Code = Keyboard.Key.Escape) 
|> Event.add (fun _ -> window.Close())

But since it natively implements an observable, you can also do:

window.KeyPress |> Observable.op... |> Observable.subscribe(fun evt -> printfn "Pressed %A" evt.Code)

@Zalutskii
Copy link

Zalutskii commented Dec 4, 2020

When the source code generators are fully implemented, it will be possible to create a generator that replaces the standard events with RX.
An example of such a generator can be found here: https://github.com/Zalutskii/Reactive-Extensions-event-generator

@weitzhandler
Copy link
Contributor Author

Thanks for your link.
There is also https://github.com/reactiveui/Pharmacist. I think @glennawatson did some work bringing source generators to Pharmacist.

@glennawatson
Copy link

The source generator is currently in our incubator org. https://github.com/reactivemarbles/ObservableEventsSourceGenerator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants