117 lines
4.0 KiB
JavaScript
117 lines
4.0 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
var canReportError_1 = require("./util/canReportError");
|
||
|
var toSubscriber_1 = require("./util/toSubscriber");
|
||
|
var observable_1 = require("./symbol/observable");
|
||
|
var pipe_1 = require("./util/pipe");
|
||
|
var config_1 = require("./config");
|
||
|
var Observable = (function () {
|
||
|
function Observable(subscribe) {
|
||
|
this._isScalar = false;
|
||
|
if (subscribe) {
|
||
|
this._subscribe = subscribe;
|
||
|
}
|
||
|
}
|
||
|
Observable.prototype.lift = function (operator) {
|
||
|
var observable = new Observable();
|
||
|
observable.source = this;
|
||
|
observable.operator = operator;
|
||
|
return observable;
|
||
|
};
|
||
|
Observable.prototype.subscribe = function (observerOrNext, error, complete) {
|
||
|
var operator = this.operator;
|
||
|
var sink = toSubscriber_1.toSubscriber(observerOrNext, error, complete);
|
||
|
if (operator) {
|
||
|
sink.add(operator.call(sink, this.source));
|
||
|
}
|
||
|
else {
|
||
|
sink.add(this.source || (config_1.config.useDeprecatedSynchronousErrorHandling && !sink.syncErrorThrowable) ?
|
||
|
this._subscribe(sink) :
|
||
|
this._trySubscribe(sink));
|
||
|
}
|
||
|
if (config_1.config.useDeprecatedSynchronousErrorHandling) {
|
||
|
if (sink.syncErrorThrowable) {
|
||
|
sink.syncErrorThrowable = false;
|
||
|
if (sink.syncErrorThrown) {
|
||
|
throw sink.syncErrorValue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return sink;
|
||
|
};
|
||
|
Observable.prototype._trySubscribe = function (sink) {
|
||
|
try {
|
||
|
return this._subscribe(sink);
|
||
|
}
|
||
|
catch (err) {
|
||
|
if (config_1.config.useDeprecatedSynchronousErrorHandling) {
|
||
|
sink.syncErrorThrown = true;
|
||
|
sink.syncErrorValue = err;
|
||
|
}
|
||
|
if (canReportError_1.canReportError(sink)) {
|
||
|
sink.error(err);
|
||
|
}
|
||
|
else {
|
||
|
console.warn(err);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
Observable.prototype.forEach = function (next, promiseCtor) {
|
||
|
var _this = this;
|
||
|
promiseCtor = getPromiseCtor(promiseCtor);
|
||
|
return new promiseCtor(function (resolve, reject) {
|
||
|
var subscription;
|
||
|
subscription = _this.subscribe(function (value) {
|
||
|
try {
|
||
|
next(value);
|
||
|
}
|
||
|
catch (err) {
|
||
|
reject(err);
|
||
|
if (subscription) {
|
||
|
subscription.unsubscribe();
|
||
|
}
|
||
|
}
|
||
|
}, reject, resolve);
|
||
|
});
|
||
|
};
|
||
|
Observable.prototype._subscribe = function (subscriber) {
|
||
|
var source = this.source;
|
||
|
return source && source.subscribe(subscriber);
|
||
|
};
|
||
|
Observable.prototype[observable_1.observable] = function () {
|
||
|
return this;
|
||
|
};
|
||
|
Observable.prototype.pipe = function () {
|
||
|
var operations = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
operations[_i] = arguments[_i];
|
||
|
}
|
||
|
if (operations.length === 0) {
|
||
|
return this;
|
||
|
}
|
||
|
return pipe_1.pipeFromArray(operations)(this);
|
||
|
};
|
||
|
Observable.prototype.toPromise = function (promiseCtor) {
|
||
|
var _this = this;
|
||
|
promiseCtor = getPromiseCtor(promiseCtor);
|
||
|
return new promiseCtor(function (resolve, reject) {
|
||
|
var value;
|
||
|
_this.subscribe(function (x) { return value = x; }, function (err) { return reject(err); }, function () { return resolve(value); });
|
||
|
});
|
||
|
};
|
||
|
Observable.create = function (subscribe) {
|
||
|
return new Observable(subscribe);
|
||
|
};
|
||
|
return Observable;
|
||
|
}());
|
||
|
exports.Observable = Observable;
|
||
|
function getPromiseCtor(promiseCtor) {
|
||
|
if (!promiseCtor) {
|
||
|
promiseCtor = config_1.config.Promise || Promise;
|
||
|
}
|
||
|
if (!promiseCtor) {
|
||
|
throw new Error('no Promise impl found');
|
||
|
}
|
||
|
return promiseCtor;
|
||
|
}
|
||
|
//# sourceMappingURL=Observable.js.map
|