subscribeToResult.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var root_1 = require('./root');
  3. var isArrayLike_1 = require('./isArrayLike');
  4. var isPromise_1 = require('./isPromise');
  5. var isObject_1 = require('./isObject');
  6. var Observable_1 = require('../Observable');
  7. var iterator_1 = require('../symbol/iterator');
  8. var InnerSubscriber_1 = require('../InnerSubscriber');
  9. var observable_1 = require('../symbol/observable');
  10. function subscribeToResult(outerSubscriber, result, outerValue, outerIndex) {
  11. var destination = new InnerSubscriber_1.InnerSubscriber(outerSubscriber, outerValue, outerIndex);
  12. if (destination.closed) {
  13. return null;
  14. }
  15. if (result instanceof Observable_1.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_1.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_1.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_1.root.setTimeout(function () { throw err; });
  44. });
  45. return destination;
  46. }
  47. else if (result && typeof result[iterator_1.iterator] === 'function') {
  48. var iterator = result[iterator_1.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[observable_1.observable] === 'function') {
  62. var obs = result[observable_1.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_1.InnerSubscriber(outerSubscriber, outerValue, outerIndex));
  68. }
  69. }
  70. else {
  71. var value = isObject_1.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. exports.subscribeToResult = subscribeToResult;
  79. //# sourceMappingURL=subscribeToResult.js.map