Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:35:29+00:00 2026-05-26T20:35:29+00:00

I’m learning Reactive Extensions, and I’ve been trying to find out if it’s a

  • 0

I’m learning Reactive Extensions, and I’ve been trying to find out if it’s a match for a task like this.

I have a Process() method that processes a batch of requests as a unit of work, and invoking a callback when all requests have completed.

The important thing here is that each request will call the callback either synchronous or asynchronous depending on it’s implementation, and the batch processor must be able to handle both.

But no threads are started from the batch processor, any new threads (or other async execution) will be initiated from inside the request handlers if necessary. I don’t know if this match the use cases of rx.

My current working code looks (almost) like this:

public void Process(ICollection<IRequest> requests, Action<List<IResponse>> onCompleted)
{
    IUnitOfWork uow = null;
    try
    {
        uow = unitOfWorkFactory.Create();

        var responses = new List<IResponse>();
        var outstandingRequests = requests.Count;
        foreach (var request in requests)
        {
            var correlationId = request.CorrelationId;
            Action<IResponse> requestCallback = response =>
            {
                response.CorrelationId = correlationId;
                responses.Add(response);
                outstandingRequests--;
                if (outstandingRequests != 0)
                    return;

                uow.Commit();
                onCompleted(responses);
            };

            requestProcessor.Process(request, requestCallback);
        }
    }
    catch(Exception)
    {
        if (uow != null) 
            uow.Rollback();
    }

    if (uow != null) 
        uow.Commit();
}        

How would you implement this using rx? Is it reasonable?

Note, that the unit of work is to be committed synchronously even if there are async requests that have not yet returned.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:35:30+00:00Added an answer on May 26, 2026 at 8:35 pm

    My approach to this is two-step.

    First create a general-purpose operator that turns Action<T, Action<R>> into Func<T, IObservable<R>>:

    public static class ObservableEx
    {
        public static Func<T, IObservable<R>> FromAsyncCallbackPattern<T, R>(
            this Action<T, Action<R>> call)
        {
            if (call == null) throw new ArgumentNullException("call");
            return t =>
            {
                var subject = new AsyncSubject<R>();
                try
                {
                    Action<R> callback = r =>
                    {
                        subject.OnNext(r);
                        subject.OnCompleted();
                    };
                    call(t, callback);
                }
                catch (Exception ex)
                {
                    return Observable.Throw<R>(ex, Scheduler.ThreadPool);
                }
                return subject.AsObservable<R>();
            };
        }
    }
    

    Next, turn the call void Process(ICollection<IRequest> requests, Action<List<IResponse>> onCompleted) into IObservable<IResponse> Process(IObservable<IRequest> requests):

    public IObservable<IResponse> Process(IObservable<IRequest> requests)
    {
        Func<IRequest, IObservable<IResponse>> rq2rp =
            ObservableEx.FromAsyncCallbackPattern
                <IRequest, IResponse>(requestProcessor.Process);
    
        var query = (
            from rq in requests
            select rq2rp(rq)).Concat();
    
        var uow = unitOfWorkFactory.Create();
        var subject = new ReplaySubject<IResponse>();
    
        query.Subscribe(
            r => subject.OnNext(r),
            ex =>
            {
                uow.Rollback();
                subject.OnError(ex);
            },
            () =>
            {
                uow.Commit();
                subject.OnCompleted();
            });
    
        return subject.AsObservable();
    }
    

    Now, not only does this run the processing async, but it also ensures the correct order of the results.

    In fact, since you are starting with a collection, you could even do this:

    var rqs = requests.ToObservable();
    
    var rqrps = rqs.Zip(Process(rqs),
        (rq, rp) => new
        {
            Request = rq,
            Response = rp,
        });
    

    Then you would have an observable that pairs up each request/response without the need for a CorrelationId property.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.