Skip to content

Strange behaviour of Observable.of([x]).map #1140

@chrisprice

Description

@chrisprice

I have a use case for creating an observable of arrays and came across some odd behaviour. The map function is invoked twice when using Observable.of(x) but not when using Observable.from([x]). Is this expected?

  it('should emit one value', function (done) {
    var calls = 0;
    Observable.of([42]).map(function (x) {
      expect(++calls).toBe(1);
      expect(x).toEqual([42]);
      return x;
    }).subscribe(function (x) {
      expect(++calls).toBe(2);
      expect(x).toEqual([42]);
    }, function (x) {
      done.fail('should not be called');
    }, done);
  });
Failures:
1) Observable.of should emit one value
  Message:
    Expected 2 to be 1.
  Stack:
    Error: Expected 2 to be 1.
        at ScalarObservable.<anonymous> (/Users/chris/RxJS/spec/observables/of-spec.js:51:23)
        at ScalarObservable.proto.map (/Users/chris/RxJS/dist/cjs/observable/ScalarObservable.js:61:45)
        at Object.<anonymous> (/Users/chris/RxJS/spec/observables/of-spec.js:50:25)
  Message:
    Expected 3 to be 2.
  Stack:
    Error: Expected 3 to be 2.
        at SafeSubscriber.<anonymous> (/Users/chris/RxJS/spec/observables/of-spec.js:55:23)
        at SafeSubscriber.tryCatcher [as _next] (/Users/chris/RxJS/dist/cjs/util/tryOrOnError.js:4:31)
        at SafeSubscriber.next (/Users/chris/RxJS/dist/cjs/Subscriber.js:89:18)
        at ScalarObservable._subscribe (/Users/chris/RxJS/dist/cjs/observable/ScalarObservable.js:44:24)

1999 specs, 1 failure

However, swapping Observable.of([42]) for Observable.from([[42]]) as below, doesn't trigger the failure.

  it('should emit one value', function (done) {
    var calls = 0;
    Observable.from([[42]]).map(function (x) {
      expect(++calls).toBe(1);
      expect(x).toEqual([42]);
      return x;
    }).subscribe(function (x) {
      expect(++calls).toBe(2);
      expect(x).toEqual([42]);
    }, function (x) {
      done.fail('should not be called');
    }, done);
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions