"use strict"; var root_1 = require('./root'); var isArray_1 = require('./isArray'); var isPromise_1 = require('./isPromise'); var Observable_1 = require('../Observable'); var SymbolShim_1 = require('../util/SymbolShim'); var InnerSubscriber_1 = require('../InnerSubscriber'); function subscribeToResult(outerSubscriber, result, outerValue, outerIndex) { var destination = new InnerSubscriber_1.InnerSubscriber(outerSubscriber, outerValue, outerIndex); if (destination.isUnsubscribed) { return; } if (result instanceof Observable_1.Observable) { if (result._isScalar) { destination.next(result.value); destination.complete(); return; } else { return result.subscribe(destination); } } if (isArray_1.isArray(result)) { for (var i = 0, len = result.length; i < len && !destination.isUnsubscribed; i++) { destination.next(result[i]); } if (!destination.isUnsubscribed) { destination.complete(); } } else if (isPromise_1.isPromise(result)) { result.then(function (value) { if (!destination.isUnsubscribed) { destination.next(value); destination.complete(); } }, function (err) { return destination.error(err); }) .then(null, function (err) { // Escaping the Promise trap: globally throw unhandled errors root_1.root.setTimeout(function () { throw err; }); }); return destination; } else if (typeof result[SymbolShim_1.SymbolShim.iterator] === 'function') { for (var _i = 0, result_1 = result; _i < result_1.length; _i++) { var item = result_1[_i]; destination.next(item); if (destination.isUnsubscribed) { break; } } if (!destination.isUnsubscribed) { destination.complete(); } } else if (typeof result[SymbolShim_1.SymbolShim.observable] === 'function') { var obs = result[SymbolShim_1.SymbolShim.observable](); if (typeof obs.subscribe !== 'function') { destination.error('invalid observable'); } else { return obs.subscribe(new InnerSubscriber_1.InnerSubscriber(outerSubscriber, outerValue, outerIndex)); } } else { destination.error(new TypeError('unknown type returned')); } } exports.subscribeToResult = subscribeToResult; //# sourceMappingURL=subscribeToResult.js.map