subscribeToResult.js 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /** PURE_IMPORTS_START ._root,._isArrayLike,._isPromise,._isObject,.._Observable,.._symbol_iterator,.._InnerSubscriber,.._symbol_observable PURE_IMPORTS_END */
  2. import { root } from './root';
  3. import { isArrayLike } from './isArrayLike';
  4. import { isPromise } from './isPromise';
  5. import { isObject } from './isObject';
  6. import { Observable } from '../Observable';
  7. import { iterator as Symbol_iterator } from '../symbol/iterator';
  8. import { InnerSubscriber } from '../InnerSubscriber';
  9. import { observable as Symbol_observable } from '../symbol/observable';
  10. export function subscribeToResult(outerSubscriber, result, outerValue, outerIndex) {
  11. var destination = new InnerSubscriber(outerSubscriber, outerValue, outerIndex);
  12. if (destination.closed) {
  13. return null;
  14. }
  15. if (result instanceof Observable) {
  16. if (result._isScalar) {
  17. destination.next(result.value);
  18. destination.complete();
  19. return null;
  20. }
  21. else {
  22. destination.syncErrorThrowable = true;
  23. return result.subscribe(destination);
  24. }
  25. }
  26. else if (isArrayLike(result)) {
  27. for (var i = 0, len = result.length; i < len && !destination.closed; i++) {
  28. destination.next(result[i]);
  29. }
  30. if (!destination.closed) {
  31. destination.complete();
  32. }
  33. }
  34. else if (isPromise(result)) {
  35. result.then(function (value) {
  36. if (!destination.closed) {
  37. destination.next(value);
  38. destination.complete();
  39. }
  40. }, function (err) { return destination.error(err); })
  41. .then(null, function (err) {
  42. // Escaping the Promise trap: globally throw unhandled errors
  43. root.setTimeout(function () { throw err; });
  44. });
  45. return destination;
  46. }
  47. else if (result && typeof result[Symbol_iterator] === 'function') {
  48. var iterator = result[Symbol_iterator]();
  49. do {
  50. var item = iterator.next();
  51. if (item.done) {
  52. destination.complete();
  53. break;
  54. }
  55. destination.next(item.value);
  56. if (destination.closed) {
  57. break;
  58. }
  59. } while (true);
  60. }
  61. else if (result && typeof result[Symbol_observable] === 'function') {
  62. var obs = result[Symbol_observable]();
  63. if (typeof obs.subscribe !== 'function') {
  64. destination.error(new TypeError('Provided object does not correctly implement Symbol.observable'));
  65. }
  66. else {
  67. return obs.subscribe(new InnerSubscriber(outerSubscriber, outerValue, outerIndex));
  68. }
  69. }
  70. else {
  71. var value = isObject(result) ? 'an invalid object' : "'" + result + "'";
  72. var msg = ("You provided " + value + " where a stream was expected.")
  73. + ' You can provide an Observable, Promise, Array, or Iterable.';
  74. destination.error(new TypeError(msg));
  75. }
  76. return null;
  77. }
  78. //# sourceMappingURL=subscribeToResult.js.map