122 lines
5.0 KiB
JavaScript
122 lines
5.0 KiB
JavaScript
|
"use strict";
|
||
|
var __extends = (this && this.__extends) || function (d, b) {
|
||
|
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||
|
function __() { this.constructor = d; }
|
||
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||
|
};
|
||
|
var isArray_1 = require('../util/isArray');
|
||
|
var isArrayLike_1 = require('../util/isArrayLike');
|
||
|
var isPromise_1 = require('../util/isPromise');
|
||
|
var PromiseObservable_1 = require('./PromiseObservable');
|
||
|
var IteratorObservable_1 = require('./IteratorObservable');
|
||
|
var ArrayObservable_1 = require('./ArrayObservable');
|
||
|
var ArrayLikeObservable_1 = require('./ArrayLikeObservable');
|
||
|
var iterator_1 = require('../symbol/iterator');
|
||
|
var Observable_1 = require('../Observable');
|
||
|
var observeOn_1 = require('../operators/observeOn');
|
||
|
var observable_1 = require('../symbol/observable');
|
||
|
/**
|
||
|
* We need this JSDoc comment for affecting ESDoc.
|
||
|
* @extends {Ignored}
|
||
|
* @hide true
|
||
|
*/
|
||
|
var FromObservable = (function (_super) {
|
||
|
__extends(FromObservable, _super);
|
||
|
function FromObservable(ish, scheduler) {
|
||
|
_super.call(this, null);
|
||
|
this.ish = ish;
|
||
|
this.scheduler = scheduler;
|
||
|
}
|
||
|
/**
|
||
|
* Creates an Observable from an Array, an array-like object, a Promise, an
|
||
|
* iterable object, or an Observable-like object.
|
||
|
*
|
||
|
* <span class="informal">Converts almost anything to an Observable.</span>
|
||
|
*
|
||
|
* <img src="./img/from.png" width="100%">
|
||
|
*
|
||
|
* Convert various other objects and data types into Observables. `from`
|
||
|
* converts a Promise or an array-like or an
|
||
|
* [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable)
|
||
|
* object into an Observable that emits the items in that promise or array or
|
||
|
* iterable. A String, in this context, is treated as an array of characters.
|
||
|
* Observable-like objects (contains a function named with the ES2015 Symbol
|
||
|
* for Observable) can also be converted through this operator.
|
||
|
*
|
||
|
* @example <caption>Converts an array to an Observable</caption>
|
||
|
* var array = [10, 20, 30];
|
||
|
* var result = Rx.Observable.from(array);
|
||
|
* result.subscribe(x => console.log(x));
|
||
|
*
|
||
|
* // Results in the following:
|
||
|
* // 10 20 30
|
||
|
*
|
||
|
* @example <caption>Convert an infinite iterable (from a generator) to an Observable</caption>
|
||
|
* function* generateDoubles(seed) {
|
||
|
* var i = seed;
|
||
|
* while (true) {
|
||
|
* yield i;
|
||
|
* i = 2 * i; // double it
|
||
|
* }
|
||
|
* }
|
||
|
*
|
||
|
* var iterator = generateDoubles(3);
|
||
|
* var result = Rx.Observable.from(iterator).take(10);
|
||
|
* result.subscribe(x => console.log(x));
|
||
|
*
|
||
|
* // Results in the following:
|
||
|
* // 3 6 12 24 48 96 192 384 768 1536
|
||
|
*
|
||
|
* @see {@link create}
|
||
|
* @see {@link fromEvent}
|
||
|
* @see {@link fromEventPattern}
|
||
|
* @see {@link fromPromise}
|
||
|
*
|
||
|
* @param {ObservableInput<T>} ish A subscribable object, a Promise, an
|
||
|
* Observable-like, an Array, an iterable or an array-like object to be
|
||
|
* converted.
|
||
|
* @param {Scheduler} [scheduler] The scheduler on which to schedule the
|
||
|
* emissions of values.
|
||
|
* @return {Observable<T>} The Observable whose values are originally from the
|
||
|
* input object that was converted.
|
||
|
* @static true
|
||
|
* @name from
|
||
|
* @owner Observable
|
||
|
*/
|
||
|
FromObservable.create = function (ish, scheduler) {
|
||
|
if (ish != null) {
|
||
|
if (typeof ish[observable_1.observable] === 'function') {
|
||
|
if (ish instanceof Observable_1.Observable && !scheduler) {
|
||
|
return ish;
|
||
|
}
|
||
|
return new FromObservable(ish, scheduler);
|
||
|
}
|
||
|
else if (isArray_1.isArray(ish)) {
|
||
|
return new ArrayObservable_1.ArrayObservable(ish, scheduler);
|
||
|
}
|
||
|
else if (isPromise_1.isPromise(ish)) {
|
||
|
return new PromiseObservable_1.PromiseObservable(ish, scheduler);
|
||
|
}
|
||
|
else if (typeof ish[iterator_1.iterator] === 'function' || typeof ish === 'string') {
|
||
|
return new IteratorObservable_1.IteratorObservable(ish, scheduler);
|
||
|
}
|
||
|
else if (isArrayLike_1.isArrayLike(ish)) {
|
||
|
return new ArrayLikeObservable_1.ArrayLikeObservable(ish, scheduler);
|
||
|
}
|
||
|
}
|
||
|
throw new TypeError((ish !== null && typeof ish || ish) + ' is not observable');
|
||
|
};
|
||
|
/** @deprecated internal use only */ FromObservable.prototype._subscribe = function (subscriber) {
|
||
|
var ish = this.ish;
|
||
|
var scheduler = this.scheduler;
|
||
|
if (scheduler == null) {
|
||
|
return ish[observable_1.observable]().subscribe(subscriber);
|
||
|
}
|
||
|
else {
|
||
|
return ish[observable_1.observable]().subscribe(new observeOn_1.ObserveOnSubscriber(subscriber, scheduler, 0));
|
||
|
}
|
||
|
};
|
||
|
return FromObservable;
|
||
|
}(Observable_1.Observable));
|
||
|
exports.FromObservable = FromObservable;
|
||
|
//# sourceMappingURL=FromObservable.js.map
|