esnext.observable.from.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var call = require('../internals/function-call');
  5. var anObject = require('../internals/an-object');
  6. var isConstructor = require('../internals/is-constructor');
  7. var getIterator = require('../internals/get-iterator');
  8. var getMethod = require('../internals/get-method');
  9. var iterate = require('../internals/iterate');
  10. var wellKnownSymbol = require('../internals/well-known-symbol');
  11. var OBSERVABLE_FORCED = require('../internals/observable-forced');
  12. var $$OBSERVABLE = wellKnownSymbol('observable');
  13. // `Observable.from` method
  14. // https://github.com/tc39/proposal-observable
  15. $({ target: 'Observable', stat: true, forced: OBSERVABLE_FORCED }, {
  16. from: function from(x) {
  17. var C = isConstructor(this) ? this : getBuiltIn('Observable');
  18. var observableMethod = getMethod(anObject(x), $$OBSERVABLE);
  19. if (observableMethod) {
  20. var observable = anObject(call(observableMethod, x));
  21. return observable.constructor === C ? observable : new C(function (observer) {
  22. return observable.subscribe(observer);
  23. });
  24. }
  25. var iterator = getIterator(x);
  26. return new C(function (observer) {
  27. iterate(iterator, function (it, stop) {
  28. observer.next(it);
  29. if (observer.closed) return stop();
  30. }, { IS_ITERATOR: true, INTERRUPTED: true });
  31. observer.complete();
  32. });
  33. }
  34. });