ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.03k stars 390 forks source link

Converting a callback to an observable? #542

Closed serg06 closed 4 years ago

serg06 commented 4 years ago

Hey guys, I'm working with an old HTTP framework and I'm trying to convert the requests into observables.

The framework works like this:

// Define callback object
class MyCallback : CkCallback
{
    void OnUpdate(HttpUpdate update)
    {
        // handle progress, or success, or failure, or canceled
    }
}

// Set callback
CkHttp http;
auto callback = new MyCallbackObject;
http.set_callback_object(callback);

// Start asynchronous GET
CkTask task = http.GetAsync("http://www.google.ca/");

// (Optional) Cancel asynchronous GET
task.Cancel();

I want to turn this into an observable, but I'm not really sure how to do it. Any tips on it? I've looked it up online but haven't found anything similar in RxCpp or RxJS.

BlueSolei commented 4 years ago

You should use a Subject. Here is a good example of adapting a push interface to RX, with support of cancelation.