@parity/light.js
Version:
A high-level reactive JS library optimized for light clients
42 lines (41 loc) • 1.55 kB
JavaScript
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
Object.defineProperty(exports, "__esModule", { value: true });
var operators_1 = require("rxjs/operators");
var rxjs_1 = require("rxjs");
/**
* RxJs operator that takes a promise as argument and switches the source
* observable to the promise converted as an observable.
*
* @param options.emitErrors In case the promise errors, the observable will
* error out (throw and abort) if emitErrors is `true`; otherwise the error will
* just be logged.
*
* @ignore
*/
exports.switchMapPromise = function (promise, options) {
if (options === void 0) { options = { emitErrors: false }; }
return operators_1.switchMap(function () {
return rxjs_1.from(promise().then(function (result) {
// The result can sometimes be {id: 2, jsonrpc: "2.0", error: {...}}
if (result.error) {
return Promise.reject(result);
}
return Promise.resolve(result);
})).pipe(operators_1.catchError(function (err) {
console.group();
console.error({ call: promise.toString(), err: err });
console.error(new Error('Error while executing API call, see error log above for more information.'));
console.groupEnd();
if (options.emitErrors) {
return rxjs_1.throwError(err);
}
else {
return rxjs_1.empty();
}
}));
});
};
;