Skip to content

WITH REPRO STEPS - switchMap not unsubscribing if imported from internal #5641

Closed
@radex

Description

@radex

Hi! This is a reopen of #4363, #4230, #3306.

The issue was never solved because reproducible sample was never provided… U N T I L   N O W :))

Here's a repro: https://github.com/radex/rxjs-import-bug-repro

This:

const x = interval(100).pipe(
      switchMap(() => interval(20).pipe(tap(x => console.log(x)))),
      tap(x => console.warn(x)),
    ).subscribe()

    setTimeout(() => {
      console.log('END')
      x.unsubscribe()
    }, 1000)

Naturally should stop logging to console after 1 second. Even if I do imports like this:

import { interval } from 'rxjs/_esm2015/internal/observable/interval'
import { timer } from 'rxjs/_esm2015/internal/observable/timer'
import { take } from 'rxjs/_esm2015/internal/operators/take'
import { switchMap } from 'rxjs/_esm2015/internal/operators/switchMap'
import { takeUntil } from 'rxjs/_esm2015/internal/operators/takeUntil'
import { tap } from 'rxjs/_esm2015/internal/operators/tap'

All is well. However, if I do this:

import { interval } from 'rxjs/observable/interval'
import { timer } from 'rxjs/observable/timer'
import { take, switchMap, takeUntil, tap } from 'rxjs/operators'

and add this to webpack:

const rxPaths = require('rxjs/_esm2015/path-mapping')()

// remove `rxjs-compat`
const fixedRxPaths = {}
for (let k in rxPaths) {
  const value = rxPaths[k]
  const fixed = value.startsWith('rxjs-compat') ? value.replace('rxjs-compat/_esm2015', 'rxjs/_esm2015/internal') : value
  fixedRxPaths[k] = fixed
}

// ....

config = {
...
  resolve: {
    ...
    alias: {
      ...fixedRxPaths
    }
  }
}

Then I can reproduce the problem. Contrived example? Sure. But this is an simplified extraction from a real app. Why would I do such an awful thing and not just import from rxjs as is documented? Because I'm shipping the app both on web and with React Native. And the latter's packager is not smart enough to tree shake unused code. That's why doing code transforms (in webpack and babel) and point to internal so that I can avoid a big chunk of RxJS weight on RN (and keep same code for web)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions