UNPKG

@parity/light.js-react

Version:

A HOC to easily use @parity/light.js with React.

56 lines (55 loc) 2.09 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: MIT var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var recompose_1 = require("recompose"); var operators_1 = require("rxjs/operators"); /** * HOC which listens to one Observable, and update the React wrapped component * every time the Observable fires. * * @param key - The key to add the value in `this.props`, so that the value * will be accessible via `this.props[key]`. * @param rpc$ - The RpcObservable to listen to. */ exports.withOneObservable = function (key, rpc$) { return recompose_1.mapPropsStreamWithConfig({ // Converts a plain ES observable to an RxJS 6 observable fromESObservable: rxjs_1.from, toESObservable: function (stream$) { return stream$; } })(function (props$) { return rxjs_1.combineLatest(props$, props$.pipe(operators_1.switchMap(rpc$))).pipe(operators_1.map(function (_a) { var _b; var props = _a[0], value = _a[1]; return (__assign({}, props, (_b = {}, _b[key] = value, _b))); })); }); }; /** * HOC which listens to multiple Observables, and injects those emitted values * into `this.props`. * * @param observables - An object where the keys will be injected into * `this.props`, and the value of each key will be the value emitted by the * corresponding Observable. */ var light = function (observables) { return recompose_1.compose.apply(void 0, Object.keys(observables).map(function (key) { return exports.withOneObservable(key, observables[key]); })); }; exports.default = light;