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.

Using more than one form of imports causes multiple copies/identities of Observable

See original GitHub issue

alternative title for SEO: “all operators are undefined, not on the prototype”

RxJS version: 5.5.0 Code to reproduce: Use two different styles of imports in the same project.

import 'rxjs';
import { Observable } from 'rxjs/Observable';

console.log(Observable.prototype.map);
// undefined

Or

import * as Rx from 'rxjs';
import { Observable } from 'rxjs/Observable';

console.log(Rx.Observable === Observable);
// false

Expected behavior:

They use the same copy of rxjs, so that there is only one Observable identity. Adding operators individually via import 'rxjs/add/operator/map' or import 'rxjs' both patch the same Observable.prototype as import { Observable } from 'rxjs/Observable'

Actual behavior:

They read from different copies of rxjs in the same package so adding operators to one doesn’t add them to the other.

This becomes a major issue when dealing with libraries like angular, redux-observable, etc which use import { Observable } from 'rxjs/Observable' but the consuming devs use import 'rxjs'. None of the operators get added because they are not the same Observable.

This was first reported in https://github.com/redux-observable/redux-observable/issues/341 by several users, but I’m also experiencing it as well.

Additional information:

You won’t reproduce this using CJS or with Babel. You need a bundler that supports tree shaking via "module". See https://github.com/ReactiveX/rxjs/issues/2984#issuecomment-338351370 for my working theory on the cause.

Workaround

The workaround is to use "rxjs/Rx" instead of "rxjs".

import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

console.log(Observable.prototype.map);
// [object Function]

This works because then you won’t using the ES modules copy, instead using the default CJS code–this also means you won’t get what limited tree shaking you might have gotten, but you didn’t get that before 5.5.0 and because operators are on the prototype they are not shaken, so the difference is modest. So using this workaround will behavior identical to how it was in 5.4

Cc/ @jasonaden @IgorMinar @benlesh

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
IgorMinarcommented, Oct 26, 2017

Wasn’t this fixed in 5.5.1?

On Thu, Oct 26, 2017, 4:01 PM Roman Vasilev notifications@github.com wrote:

// RxJS since 5.5+ import { Observable as Observable1 } from ‘rxjs’; import { Observable as Observable2 } from ‘rxjs/Observable’; console.log(Observable2 === Observable1); // => false, but expected true

Fix on webpack side:

resolve: { alias: { rxjs$: ‘rxjs/_esm5/Rx.js’, rxjs: ‘rxjs/_esm5’ } },

Full config https://github.com/unlight/webpack-resolve-alias-playground/blob/master/webpack.config.ts and repo example https://github.com/unlight/webpack-resolve-alias-playground

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ReactiveX/rxjs/issues/2984#issuecomment-339675892, or mute the thread https://github.com/notifications/unsubscribe-auth/AANM6CHjtoBdmuHJ4gjLvI4pgMtuSVHJks5swJDPgaJpZM4QBQvp .

1reaction
jasonadencommented, Oct 21, 2017

@cartant Good point on the related Angular issue. I was looking at it from the context of a single project, not the combination of different projects importing different ways.

I’ll push a commit to remove the “module” key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to Imports - Observable
Observable lets you quickly reuse code by importing named cells from other notebooks. Example of Importing a Function. Let's say you saw Mike...
Read more >
How can I use the same observable with multiple mappings?
I want to process this data in two different ways (using the same data) and use the resulting Observables stored in group and...
Read more >
RxJS 6: What's new and what has changed? - Auth0
Changes to import paths​​ The recommendation for TypeScript developers is to use rxjs-tslint to refactor import paths. The following rules have ...
Read more >
RxJS custom operators - Angular inDepth
In this blog post, I will introduce you to the concept of custom RxJS operators and present exemplary implementations. Identity operator. Link to...
Read more >
LiveData overview - Android Developers
Use LiveData to handle data in a lifecycle-aware fashion. ... LiveData is an observable data holder class. Unlike a regular observable, ...
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