UNPKG

rx-synchronizer

Version:

Commonly-used synchronizer bases that can be composed into more specific purposes.

71 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var javascriptutilities_1 = require("javascriptutilities"); var rx_utilities_js_1 = require("rx-utilities-js"); var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var ProgressSync = require("./progress"); var sync_util_1 = require("./sync-util"); var deepEqual = require("deep-equal"); var Impl = /** @class */ (function () { function Impl(progressSync) { this.progressSync = progressSync || new ProgressSync.Impl(); this.subscription = new rxjs_1.Subscription(); } Impl.prototype.synchronize = function (dependency) { var subscription = this.subscription; var fetchStream = dependency.paramStream.pipe((function () { if (dependency.allowDuplicateParams) { return function (v) { return v; }; } return function (v) { return v.pipe(operators_1.distinctUntilChanged(function (v1, v2) { return deepEqual(v1.value, v2.value); })); }; })(), operators_1.map(function (param) { try { var actualParam = param.getOrThrow(); return sync_util_1.createObservable(dependency.fetchWithParam(actualParam)).pipe(operators_1.map(function (v1) { return javascriptutilities_1.Try.success(v1); }), rx_utilities_js_1.catchJustReturn(function (e) { return javascriptutilities_1.Try.failure(e); })); } catch (e) { return rxjs_1.of(javascriptutilities_1.Try.failure(e)); } }), (function () { if (dependency.resultReceiptScheduler) { return operators_1.observeOn(dependency.resultReceiptScheduler); } return operators_1.observeOn(rxjs_1.asyncScheduler); })(), operators_1.share()); var fetchCompletedStream = fetchStream.pipe(operators_1.switchMap(function (v) { return v; }), operators_1.share()); subscription.add(fetchCompletedStream .pipe((function () { if (dependency.allowInvalidResult) { return function (v) { return v.pipe(operators_1.map(function (_a) { var value = _a.value; return value; })); }; } return function (v) { return v.pipe(rx_utilities_js_1.mapNonNilOrEmpty(function (_a) { var value = _a.value; return value; })); }; })(), operators_1.takeUntil(dependency.stopStream)) .subscribe(dependency.resultReceiver)); subscription.add(fetchCompletedStream .pipe(operators_1.map(function (_a) { var error = _a.error; return error; }), operators_1.takeUntil(dependency.stopStream)) .subscribe(dependency.errorReceiver)); this.progressSync.synchronize({ progressReceiver: dependency.progressReceiver, progressStartStream: fetchStream.pipe(operators_1.map(function () { return true; })), progressEndStream: fetchCompletedStream.pipe(operators_1.map(function () { return false; })), stopStream: dependency.stopStream }); }; return Impl; }()); exports.Impl = Impl; //# sourceMappingURL=fetch.js.map