Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Extend a spawn method. #845

@xgrommx

Description

@xgrommx

Hello @mattpodwysocki. I look on Rx.Observable.spawn and it's awesome that to return Observable. But will be better if exists another method like a wrap the same as in https://github.com/tj/co/blob/master/index.js#L26. I create a small code like so:

Rx.Observable.wrap = function (fn) {
  createObservable.__generatorFunction__ = fn;
  return createObservable;

  function createObservable() {
    return Rx.Observable.spawn.call(this, fn.apply(this, arguments));
  }
};

var spawned = Rx.Observable.wrap(function* (val) {
  var v = yield thunk(val);
  var w = yield Rx.Observable.from([24]).first();
  var x = yield Rx.Observable.just(42).delay(1000);
  var y = yield Rx.Observable.just(56);
  var z = yield Promise.resolve(78);
  return v + w + x + y + z;
})(100);

spawned.subscribe(
  function (x) { console.log('next %s', x); },
  function (e) { console.log('error %s', e); },
  function () { console.log('completed'); }
); // Return -> next 300

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