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 6661783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:19:14+00:00 2026-05-26T02:19:14+00:00

Using Rx, I want to observe a legacy object that exposes both the method

  • 0

Using Rx, I want to observe a legacy object that exposes both the method GetItems and the event NewItem.

When GetItems is called, it’ll synchronously return a list of any items it has in cache. It’ll also spawn an async fetch of items that will be published via the NewItem event as they are received.

How can I observe both of these sources (sync + async) in a consistent way by formulating a LINQ query such that both result-sets are captured? The production order is of no importance.

  • 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-26T02:19:15+00:00Added an answer on May 26, 2026 at 2:19 am

    Let me see if I’ve understood your legacy object. I’m assuming it is a generic type that looks like this:

    public class LegacyObject<T>
    {
        public IEnumerable<T> GetItems();
        public event EventHandler<NewItemEventArgs<T>> NewItem;
    }
    

    With the new item event args like this:

    public class NewItemEventArgs<T> : System.EventArgs
    {
        public T NewItem { get; private set; }
        public NewItemEventArgs(T newItem)
        {
            this.NewItem = newItem;
        }
    }
    

    Now, I’ve created a .ToObservable() extension method for LegacyObject<T>:

    public static IObservable<T> ToObservable<T>(
        this LegacyObject<T> @this)
    {
        return Observable.Create<T>(o =>
        {
            var gate = new object();
            lock (gate)
            {
                var list = new List<T>();
                var subject = new Subject<T>();
                var newItems = Observable
                    .FromEventPattern<NewItemEventArgs<T>>(
                        h => @this.NewItem += h,
                        h => @this.NewItem -= h)
                    .Select(ep => ep.EventArgs.NewItem);
                var inner = newItems.Subscribe(ni =>
                {
                    lock (gate)
                    {
                        if (!list.Contains(ni))
                        {
                            list.Add(ni);
                            subject.OnNext(ni);
                        }
                    }
                });
                list.AddRange(@this.GetItems());
                var outer = list.ToArray().ToObservable()
                    .Concat(subject).Subscribe(o);
                return new CompositeDisposable(inner, outer);
            }
        });
    }
    

    This method creates a new observable for every subscriber – which is the correct thing to do when writing extension methods like this.

    It creates a gate object to lock access to the internal list.

    Because you said that the act of calling GetItems spawns the async function to get new items I’ve made sure that the NewItem subscription is created before the call to GetItems.

    The inner subscription checks if the new item is in the list or not and only calls OnNext on the subject if it’s not in the list.

    The call to GetItems is made and the values are added to the internal list via AddRange.

    There is an unlikely, but possible, chance that the items won’t be added to the list before the NewItem event begins firing on another thread. This is why there is a lock around the access to the list. The inner subscription will wait until it can obtain the lock before attempting to add items to the list and this will happen after the initial items are added to the list.

    Finally the internal list is turned into an observable, concatenated with the subject, and the o observer subscribes to this observable.

    The two subscriptions are returned as a single IDisposable using CompositeDisposable.

    And that is it for the ToObservable method.

    Now, I tested this by creating a constructor on the legacy object that let me pass in both an enumerable and an observable of values. The enumerable is returned when the GetItems is called and the observable drives the NewItem event.

    So, my test code looked like this:

    var tester = new Subject<int>();
    var legacy = new LegacyObject<int>(new [] { 1, 2, 3, }, tester);
    
    var values = legacy.ToObservable();
    
    values.Subscribe(v => Console.WriteLine(v));
    
    tester.OnNext(3);
    tester.OnNext(4);
    tester.OnNext(4);
    tester.OnNext(5);
    

    And the values written to the console were:

    1
    2
    3
    4
    5
    

    Let me know if this meets your needs.

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

Sidebar

Related Questions

I want to effectively throttle an event stream, so that my delegate is called
I want using GPS data (I got it from $GPRMC) in an desktop application(that
Using DQL I want to return all the documents under the Cabinet test .
Using SPList I want to get all the webpart urls that associated with the
I want to unregister a document.fire (prototype) event. How can i do that? it
I want to create a signature for a file using OpenSSL. I believe that
Using Python I want to be able to draw text at different angles using
Using MEF I want to do the following. I have a WPF Shell. To
Using T4 I want to generate some code based on examining what files are
Using Awk I want to match the entire record using a regular expression. By

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.