UNPKG

3.58 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.augmentObject = augmentObject;
7
8var _util = require("@polkadot/util");
9
10// Copyright 2017-2022 @polkadot/api authors & contributors
11// SPDX-License-Identifier: Apache-2.0
12const l = (0, _util.logger)('api/augment');
13
14function logLength(type, values) {
15 let and = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
16 return values.length ? ` ${values.length} ${type}${and.length ? ' and' : ''}` : '';
17}
18
19function logValues(type, values) {
20 return values.length ? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}` : '';
21} // log details to console
22
23
24function warn(prefix, type, _ref) {
25 let [added, removed] = _ref;
26
27 if (added.length || removed.length) {
28 l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
29 }
30}
31
32function findSectionExcludes(a, b) {
33 return a.filter(s => !b.includes(s));
34}
35
36function findSectionIncludes(a, b) {
37 return a.filter(s => b.includes(s));
38}
39
40function extractSections(src, dst) {
41 const srcSections = Object.keys(src);
42 const dstSections = Object.keys(dst);
43 return [findSectionExcludes(srcSections, dstSections), findSectionExcludes(dstSections, srcSections)];
44}
45
46function findMethodExcludes(src, dst) {
47 const srcSections = Object.keys(src);
48 const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
49 const excludes = [];
50
51 for (let s = 0; s < dstSections.length; s++) {
52 const section = dstSections[s];
53 const srcMethods = Object.keys(src[section]);
54 const dstMethods = Object.keys(dst[section]);
55 excludes.push(...dstMethods.filter(m => !srcMethods.includes(m)).map(m => `${section}.${m}`));
56 }
57
58 return excludes;
59}
60
61function extractMethods(src, dst) {
62 return [findMethodExcludes(dst, src), findMethodExcludes(src, dst)];
63}
64
65function lazySection(src, dst) {
66 const creator = m => src[m];
67
68 const methods = Object.keys(src);
69
70 for (let i = 0; i < methods.length; i++) {
71 const method = methods[i]; // We use hasOwnproperty here to only check for the existence of the key,
72 // instead of reading dst[section][method] which will evaluate when already
73 // set as a lazy value previously
74
75 if (!Object.prototype.hasOwnProperty.call(dst, method)) {
76 (0, _util.lazyMethod)(dst, method, creator);
77 }
78 }
79}
80/**
81 * @description Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is
82 * already available, but rather just adds new missing items into the result object.
83 * @internal
84 */
85
86
87function augmentObject(prefix, src, dst) {
88 let fromEmpty = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
89 fromEmpty && (0, _util.objectClear)(dst); // NOTE: This part is slightly problematic since it will get the
90 // values for at least all the sections and the names of the methods
91 // (Since methods won't be decorated before lazy, this _may_ be ok)
92
93 if (prefix && Object.keys(dst).length) {
94 warn(prefix, 'modules', extractSections(src, dst));
95 warn(prefix, 'calls', extractMethods(src, dst));
96 }
97
98 const sections = Object.keys(src);
99
100 for (let i = 0; i < sections.length; i++) {
101 const section = sections[i]; // We don't set here with a lazy interface, we decorate based
102 // on the top-level structure (this bypasses adding lazy onto lazy)
103
104 if (!dst[section]) {
105 dst[section] = {};
106 }
107
108 lazySection(src[section], dst[section]);
109 }
110
111 return dst;
112}
\No newline at end of file