1 |
|
2 |
|
3 | import { lazyMethod, logger, objectClear } from '@polkadot/util';
|
4 | const l = logger('api/augment');
|
5 |
|
6 | function logLength(type, values, and = []) {
|
7 | return values.length ? ` ${values.length} ${type}${and.length ? ' and' : ''}` : '';
|
8 | }
|
9 |
|
10 | function logValues(type, values) {
|
11 | return values.length ? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}` : '';
|
12 | }
|
13 |
|
14 |
|
15 | function warn(prefix, type, [added, removed]) {
|
16 | if (added.length || removed.length) {
|
17 | l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
|
18 | }
|
19 | }
|
20 |
|
21 | function findSectionExcludes(a, b) {
|
22 | return a.filter(s => !b.includes(s));
|
23 | }
|
24 |
|
25 | function findSectionIncludes(a, b) {
|
26 | return a.filter(s => b.includes(s));
|
27 | }
|
28 |
|
29 | function extractSections(src, dst) {
|
30 | const srcSections = Object.keys(src);
|
31 | const dstSections = Object.keys(dst);
|
32 | return [findSectionExcludes(srcSections, dstSections), findSectionExcludes(dstSections, srcSections)];
|
33 | }
|
34 |
|
35 | function findMethodExcludes(src, dst) {
|
36 | const srcSections = Object.keys(src);
|
37 | const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
|
38 | const excludes = [];
|
39 |
|
40 | for (let s = 0; s < dstSections.length; s++) {
|
41 | const section = dstSections[s];
|
42 | const srcMethods = Object.keys(src[section]);
|
43 | const dstMethods = Object.keys(dst[section]);
|
44 | excludes.push(...dstMethods.filter(m => !srcMethods.includes(m)).map(m => `${section}.${m}`));
|
45 | }
|
46 |
|
47 | return excludes;
|
48 | }
|
49 |
|
50 | function extractMethods(src, dst) {
|
51 | return [findMethodExcludes(dst, src), findMethodExcludes(src, dst)];
|
52 | }
|
53 |
|
54 | function lazySection(src, dst) {
|
55 | const creator = m => src[m];
|
56 |
|
57 | const methods = Object.keys(src);
|
58 |
|
59 | for (let i = 0; i < methods.length; i++) {
|
60 | const method = methods[i];
|
61 |
|
62 |
|
63 |
|
64 | if (!Object.prototype.hasOwnProperty.call(dst, method)) {
|
65 | lazyMethod(dst, method, creator);
|
66 | }
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | export function augmentObject(prefix, src, dst, fromEmpty = false) {
|
77 | fromEmpty && objectClear(dst);
|
78 |
|
79 |
|
80 |
|
81 | if (prefix && Object.keys(dst).length) {
|
82 | warn(prefix, 'modules', extractSections(src, dst));
|
83 | warn(prefix, 'calls', extractMethods(src, dst));
|
84 | }
|
85 |
|
86 | const sections = Object.keys(src);
|
87 |
|
88 | for (let i = 0; i < sections.length; i++) {
|
89 | const section = sections[i];
|
90 |
|
91 |
|
92 | if (!dst[section]) {
|
93 | dst[section] = {};
|
94 | }
|
95 |
|
96 | lazySection(src[section], dst[section]);
|
97 | }
|
98 |
|
99 | return dst;
|
100 | } |
\ | No newline at end of file |