Skip to content

flatMap completes before inner observable #5

@veyh

Description

@veyh

Hey, thanks for mentioning this fork in the originial repo. Looks good but I found something off with flatMap. It seems to complete even before the inner observable does. For example

local a = Subject.create()

Observable.of(1)
  :flatMap(function ()
    return a
  end)
  :dump()

prints

onCompleted

which is not what I expected, as the equivalent in rxjs prints nothing:

const { of, Subject } = require("rxjs");
const { flatMap } = require("rxjs/operators");
const a = new Subject;

of(1).pipe(
  flatMap(() => a)
)
.subscribe(
  value => console.log("onNext", value),
  err => console.log("onError", err),
  () => console.log("onCompleted")
);

And if I were to run this

local a = Subject.create()

Observable.of(1)
  :flatMap(function ()
    return a
  end)
  :dump()

a:onNext(555)
a:onCompleted()

the output would still be

onCompleted

whereas in rxjs

const { of, Subject } = require("rxjs");
const { flatMap } = require("rxjs/operators");
const a = new Subject;

of(1).pipe(
  flatMap(() => a)
)
.subscribe(
  value => console.log("onNext", value),
  err => console.log("onError", err),
  () => console.log("onCompleted")
);

a.next(555);
a.complete();

prints

onNext 555
onCompleted

Here's a test you could put in tests/flatMap.lua to further illustrate this

it('completes after inner stream', function ()
  local a = Subject.create()
  local values = {}
  local done = false

  Observable.of(1)
    :flatMap(function ()
      return a
    end)
    :subscribe(
      function (value)
        table.insert(values, value)
      end,

      function (_err)
      end,

      function ()
        done = true
      end
    )

  expect(#values).to.equal(0)

  a:onNext(555)
  expect(#values).to.equal(1) -- fails here
  expect(values[1]).to.equal(555)
  expect(done).to.equal(false)

  a:onCompleted()
  expect(done).to.equal(true)
end)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions