UNPKG

@parity/light.js

Version:

A high-level reactive JS library optimized for light clients

85 lines (84 loc) 3.41 kB
"use strict"; // Copyright 2015-2019 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: MIT Object.defineProperty(exports, "__esModule", { value: true }); var rxjs_1 = require("rxjs"); var types_1 = require("@parity/api/lib/util/types"); // @ts-ignore Unfortunately no types for memoizee/weak. var memoizeeWeak = require("memoizee/weak"); var api_1 = require("../../api"); var operators_1 = require("../../utils/operators"); // Unsubscribe to the frequency/dependsOn observable after 2s with no subscriber exports.UNSUB_DELAY = 2000; /** * Add metadata to an RpcObservable, and transform it into a ReplaySubject(1). * It's a currified function. * Pure function version of {@link createRpc}. * * @ignore * @param metadata - The metadata to add. * @example * createRpc(metadata)(options) returns a RpcObservable. * createRpc(metadata)(options)(someArgs) returns an Observable. */ var createRpcWithApi = memoizeeWeak(function (api, metadata) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } if (!metadata.dependsOn && !metadata.frequency) { throw new Error("Rpc$ '" + metadata.name + "' needs either a 'dependsOn' or a 'frequency' field."); } // The source Observable can either be another RpcObservable (in the // `dependsOn` field), or an Observable built by merging all the // FrequencyObservables var source$ = metadata.dependsOn ? metadata.dependsOn.apply(metadata, args.concat([{ provider: api.provider }])) : rxjs_1.merge.apply(void 0, metadata.frequency.map(function (f) { return f({ provider: api.provider }); })); // The pipes to add var pipes = []; if (metadata.pipes && types_1.isFunction(metadata.pipes)) { pipes.push.apply(pipes, metadata.pipes(api)); } pipes.push(operators_1.distinctReplayRefCountDelay(exports.UNSUB_DELAY)); return source$.pipe.apply(source$, pipes); }, { length: false, normalizer: function (_, otherArgs) { var _a = Array.from(otherArgs), metadata = _a[0], args = _a.slice(1); // Custom memoization function. The first argument (_, which is `api`), // is memoized by reference. For the rest of the arguments, we create an // unique id based on serialization. // https://github.com/medikoo/memoizee/issues/99#issuecomment-422155924 return "" + metadata.name + JSON.stringify(args); } }); /** * Add metadata to an RpcObservable, and transform it into a ReplaySubject(1). * It's a currified function. * * @ignore * @param metadata - The metadata to add. * @example * createRpc(metadata)(options) returns a RpcObservable. * createRpc(metadata)(options)(someArgs) returns an Observable. */ var createRpc = function (metadata) { return function (options) { if (options === void 0) { options = {}; } return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } // Evaluate api only once we subscribe return rxjs_1.defer(function () { var provider = options.provider; var api = provider ? api_1.createApiFromProvider(provider) : api_1.getApi(); return createRpcWithApi.apply(void 0, [api, metadata].concat(args)); }); }; }; }; exports.default = createRpc;