question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Observables cannot be passed between window contexts.

See original GitHub issue

RxJS version: 5.4

Code to reproduce:

if (window.opener) {
  window.myObservable = window.opener.myObservable;
} else {
  window.myObservable = Rx.Observable.of(1, 2, 3);
  window.open(location.href);
}

window.myObservable
  .switchMap(function(x) {
    return Rx.Observable.of(x);
  })
  .subscribe(function(x) {
    console.log(x);
  });

Expected behavior: Both parent and child window should print 1, 2, 3 to the console.

Actual behavior: The child window throws an exception because myObservable from the parent window’s context cannot be identified as an Observable in the child window’s context.

Additional information: The issue is caused by a reliance on instanceof for determining if an object is an Observable which fails for objects passed between javascript contexts. Instead, a more robust solution would test for the shape of an Observable, perhaps using a type guard similar to isPromise

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
marceloemanoelcommented, Jan 30, 2018

If it can be of any help, I couldn’t make it work with

Observable.from(originalObservable);

But I got it working with

Observable.create(observer => originalObservable.subscribe(observer))
1reaction
hermanbankencommented, Jun 14, 2017

Ah good to see that I was not just going crazy 😜 .

I think this is related to #2489 too, since things would get more complicated if we do this:

import switchMap from 'rxjs/somewhere/switchMap'

window.myObservable  // <---- from context B, previously assigned
  .pipe(switchMap(function(x) {  // <---- pipe from context B, switchMap from context A
    let X = Rx.Observable.of(x); // <----- run in window A, using Rx from context A
    return X;
  }))
  .subscribe(function(x) {
    console.log(x);
  });

at that point switchMap is from context B and for everything done during the setup phase of the Observable it can only assume valid what is from context B, while it is asked to work on something from context A, through pipe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass results between chained observables
In order to achieve this you can write context depending version of map , contentMap , mergMap operators so that the final solution...
Read more >
Troubleshooting Embedding
If you have a cell that runs an update method, make sure the cell is named and references the cell it is modifying....
Read more >
Using observables to pass values
An observable can deliver multiple values of any type —literals, messages, or events, depending on the context. The API for receiving values is...
Read more >
Observable
Observables are able to deliver values either synchronously or asynchronously. What is the difference between an Observable and a function? Observables can " ......
Read more >
Observable | RxJS API Document
combineLatest combines the values from all the Observables passed as arguments. This is done by subscribing to each Observable in order and, whenever...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found