Observable for a callback in Rx
c#, callback, observable, system.reactive
Solution
You can use a `Subject<T>` which can be used to move from the imperative programming world into the functional world of Rx.
`Subject<T>` implements both `IObservable<T>` and `IObserver<T>`, so you can call its `OnNext`, `OnError` and `OnCompleted` methods and the subscribers will be notified.
If you want to expose the `Subject<T>` as a property then you should do so using `.AsObservable()` as this hides the fact that the `IObservable<T>` is in fact a `Subject<T>`. It makes things such as `((Subject<string>) obj.Event).OnNext("Foo")` impossible.
Problem
I'm looking for an elegant way to create an `Observable` from a plain callback delegate with Rx, something similar to `Observable.FromEventPattern`? Say, I'm wrapping Win32 `EnumWindows` API which calls back the `EnumWindowsProc` I provide. I know I could create a temporary C# event adapter for this callback and pass it `FromEventPattern`. Also, I could probably implement `IObservable` manually, so it would call `IObserver.OnNext` from my `EnumWindowsProc` callback. Is the there an existing pattern for wrapping a callback in Rx that I'm missing?