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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:32:37+00:00 2026-05-31T05:32:37+00:00

I need to proxy all the different event streams through one subject. I came

  • 0

I need to proxy all the different event streams through one subject.

I came up with this code:

var mySubject,
    getObservable;

getObservable = function (subject, eventName) {
    return subject
        .asObservable()
        .filter(function (x) {
            return x.EventName === eventName;
        })
        .flatMap(function (x) {
            if (x.Type === 'onNext') {
                return Rx.Observable.return(x.Data);
            }

            if (x.Type === 'onError') {
                return Rx.Observable.throw(x.Data);
            }

            return Rx.Observable.empty();
        });
};

mySubject = new Rx.Subject();

getObservable(mySubject, 'foo')
    .subscribe(function(x){ 
        console.log('foo onNext ' + x); 
    }, function(x){ 
        console.log('foo onError ' + x); 
    }, function(){ 
        console.log('foo onComplete');
    });

getObservable(mySubject, 'bar')
    .subscribe(function(x){ 
        console.log('bar onNext ' + x); 
    }, function(x){ 
        console.log('bar onError ' + x); 
    }, function(){ 
        console.log('bar onComplete');
    });

mySubject.onNext({Type: 'onNext', EventName: 'foo', Data: 5});
mySubject.onNext({Type: 'onCompleted', EventName: 'foo'});

mySubject.onNext({Type: 'onNext', EventName: 'bar', Data: 5});
mySubject.onNext({Type: 'onError', EventName: 'bar', Data: 'Error message'});

Got output:

foo onNext 5

bar onNext 5
bar onError Error message

Expected output:

foo onNext 5  
foo onCompleted

bar onNext 5
bar onError Error message

For the bar event, that works like a charm: onNext will be propagated and as soon as the error raises the onError function gets called and the event stream finishes. However, I can’t get it to work for the onComplete.

Whenever a complete notification raises I do see that Rx.Observable.empty() gets called but that doesn’t cause the subscribers onComplete handler to be called. Instead, its calling its onNext handler.

  • 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-31T05:32:38+00:00Added an answer on May 31, 2026 at 5:32 am

    The getObservable function returns an observable that subscribes to a eventName event sent through a subject.

    let getObservable = function (subject, eventName) {
        return Rx.Observable.create(function (observer) {
            subject
                .asObservable()
                .filter(function(x) {
                    return x.EventName === eventName;
                })
                .map(function(x) {
                    if (x.Type === 'onNext') {
                        observer.onNext(x.Data);
                    }
    
                    if (x.Type === 'onError') {
                        observer.onError(x.Data);
                    }
    
                    if (x.Type === 'onCompleted') {
                        observer.onCompleted();
                    }
    
                    return x;
                })
                .subscribe();
        });
    };
    

    This is a working example using data from the original question:

    var mySubject,
        getObservable;
    
    getObservable = function (subject, eventName) {
        return Rx.Observable.create(function (observer) {
            subject
                .asObservable()
                .filter(function(x) {
                    return x.EventName === eventName;
                })
                .map(function(x) {
                    if (x.Type === 'onNext') {
                        observer.onNext(x.Data);
                    }
    
                    if (x.Type === 'onError') {
                        observer.onError(x.Data);
                    }
                    
                    if (x.Type === 'onCompleted') {
                        observer.onCompleted();
                    }
                    
                    return x;
                })
                .subscribe();
        });
    };
    
    mySubject = new Rx.Subject();
    
    getObservable(mySubject, 'foo')
        .subscribe(function(x){ 
            console.log('SomethingHappened onNext ' + x); 
        }, function(x){ 
            console.log('SomethingHappened onError ' + x); 
        }, function(){ 
            console.log('SomethingHappened onComplete');
        });
    
    
    getObservable(mySubject, 'bar')
        .subscribe(function(x){ 
            console.log('DataUpdated onNext ' + x); 
        }, function(x){ 
            console.log('DataUpdated onError ' + x); 
        }, function(){ 
            console.log('DataUpdated onComplete');
        });
    
    mySubject.onNext({Type: 'onNext', EventName: 'foo', Data: 5});
    mySubject.onNext({Type: 'onCompleted', EventName: 'foo'});
    
    mySubject.onNext({Type: 'onNext', EventName: 'bar', Data: 5});
    mySubject.onNext({Type: 'onError', EventName: 'bar', Data: 'Error message'});
    <script src='https://rawgit.com/Reactive-Extensions/RxJS/master/dist/rx.all.js'></script>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to proxy a feed and leave most of the nodes unspoiled and
I need a proxy for web development debugging. Preferably something standalone so I can
I need a simple proxy PHP function/script that can download a file from a
I need to make a proxy script that can access a page hidden behind
I need to set up proxy authentication against a SOCKS proxy. I found out
I need to use PHP to connect to a URL via a proxy server.
I have nginx acting as a reverse proxy to apache. I now need to
I have an LCDS server sitting behind a corporate proxy/firewall. I need to use
I have nginx setup, acting as a reverse proxy to apache. However, I need
My website is growing. I need to partition feature sets to different group of

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.