-
Notifications
You must be signed in to change notification settings - Fork 11
/
BindToChainState.js
371 lines (346 loc) · 17.5 KB
/
BindToChainState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import React from "react";
import {curry, flow, reject, clone, pairs, omit, get, pick} from "lodash";
import {ChainStore} from "bitsharesjs";
import ChainTypes from "./ChainTypes";
import utils from "../../../common/utils";
import Loading from "../Loading";
/**
* @brief 提供链数据的自动提取和更新
*
* After applying this decorator to component any property of a type from ChainTypes
* specified in component's propTypes will be automatically converted from object or account id
* into a state object that is either undefined, null or an Immutable object. The
* Immutable object will automatically be updated anytime it changes on the
* blockchain.
*
* Example:
*
* class Balance {
* static propTypes = {
* balance: ChainTypes.ChainObject.isRequired
* }
* render() {
* let amount = Number(this.props.balance.get('balance'));
* let type = this.props.balance.get('asset_type');
* return (<FormattedAsset amount={amount} asset={type}/>);
* }
* }
* Balance = BindToChainState(Balance);
*
* <Balance balance="1.5.3"/>
*/
const arrayElement = (element_number, array) => array[element_number];
const firstEl = curry(arrayElement)(0);
const secondEl = curry(arrayElement)(1);
const checkChainType = curry( (chain_type, t) => t === chain_type || t === chain_type.isRequired );
const isObjectType = checkChainType(ChainTypes.ChainObject);
const isAccountType = checkChainType(ChainTypes.ChainAccount);
const isKeyRefsType = checkChainType(ChainTypes.ChainKeyRefs);
const isAddressBalancesType = checkChainType(ChainTypes.ChainAddressBalances);
const isAssetType = checkChainType(ChainTypes.ChainAsset);
const isObjectsListType = checkChainType(ChainTypes.ChainObjectsList);
const isAccountsListType = checkChainType(ChainTypes.ChainAccountsList);
const isAssetsListType = checkChainType(ChainTypes.ChainAssetsList);
function checkIfRequired(t) {
for(let k in ChainTypes) {
let v = ChainTypes[k];
if(t === v.isRequired) return true;
}
return false;
}
function BindToChainState(Component, options = {}) {
return class Wrapper extends React.Component {
constructor(props) {
super(props);
let prop_types_array = pairs(Component.propTypes);
if(options && options.all_props) {
this.chain_objects = reject(Object.keys(this.props), (e) => e === "children" || e === "keep_updating" || e === "show_loader");
this.chain_accounts = [];
this.chain_key_refs = [];
this.chain_address_balances = [];
this.chain_assets = [];
this.chain_objects_list = [];
this.chain_accounts_list = [];
this.chain_assets_list = [];
this.required_props = [];
this.all_chain_props = this.chain_objects;
} else {
this.chain_objects = prop_types_array.filter(flow(secondEl, isObjectType)).map(firstEl);
this.chain_accounts = prop_types_array.filter(flow(secondEl, isAccountType)).map(firstEl);
this.chain_key_refs = prop_types_array.filter(flow(secondEl, isKeyRefsType)).map(firstEl);
this.chain_address_balances = prop_types_array.filter(flow(secondEl, isAddressBalancesType)).map(firstEl);
this.chain_assets = prop_types_array.filter(flow(secondEl, isAssetType)).map(firstEl);
this.chain_objects_list = prop_types_array.filter(flow(secondEl, isObjectsListType)).map(firstEl);
this.chain_accounts_list = prop_types_array.filter(flow(secondEl, isAccountsListType)).map(firstEl);
this.chain_assets_list = prop_types_array.filter(flow(secondEl, isAssetsListType)).map(firstEl);
this.required_props = prop_types_array.filter(flow(secondEl, checkIfRequired)).map(firstEl);
this.all_chain_props = [...this.chain_objects,
...this.chain_accounts,
...this.chain_key_refs,
...this.chain_address_balances,
...this.chain_assets,
...this.chain_objects_list];
}
if(options && options.require_all_props){
this.required_props = this.all_chain_props;
}
this.dynamic_props = {};
this.default_props = clone(Component.defaultProps) || {};
for (let key in this.default_props) {
let value = this.default_props[key];
if (typeof(value) === "string" && value.indexOf("props.") === 0) {
this.dynamic_props[key] = get(this, value);
}
}
this.tempComponent = Component.defaultProps ? Component.defaultProps.tempComponent || null : null;
//console.log("----- Wrapper constructor ----->", this.all_chain_props);
this.update = this.update.bind(this);
this.state = { resolved: false };
}
shouldComponentUpdate(nextProps, nextState){
return !utils.are_equal_shallow(this.props, nextProps) ||
!utils.are_equal_shallow(this.state, nextState);
}
componentWillMount() {
ChainStore.subscribe(this.update);
this.update();
}
componentWillUnmount() {
ChainStore.unsubscribe(this.update);
}
componentWillReceiveProps(next_props) {
if(options && options.all_props) {
this.chain_objects = reject(Object.keys(next_props), (e) => e === "children" || e === "keep_updating" || e === "show_loader");
this.all_chain_props = this.chain_objects;
this.state = pick(this.state, this.chain_objects);
}
let props_obj = null;
for(let k in this.dynamic_props) {
let selector = this.default_props[k];
if(!props_obj) props_obj = {props: next_props};
let cur_value = get(this, selector);
let next_value = get(props_obj, selector);
if (next_value && next_value !== cur_value) {
this.dynamic_props[k] = get(props_obj, selector);
}
}
this.update(next_props);
}
update(next_props = null)
{
//let keep_updating = (options && options.keep_updating) || this.props.keep_updating;
//if(!next_props && !keep_updating && this.state.resolved) return;
let props = next_props || this.props;
let new_state = {};
let all_objects_counter = 0;
let resolved_objects_counter = 0;
for( let key of this.chain_objects )
{
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let new_obj = ChainStore.getObject(prop);
if (new_obj === undefined && this.required_props.indexOf(key) === -1 && new_obj !== this.state[key]) new_state[key] = new_obj;
else if (new_obj && new_obj !== this.state[key]) new_state[key] = new_obj;
++all_objects_counter;
if (new_obj !== undefined) ++resolved_objects_counter;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_accounts )
{
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
if(prop[0] === "#" && Number.parseInt(prop.substring(1)))
prop = "1.2." + prop.substring(1);
let new_obj = ChainStore.getAccount(prop);
if (new_obj === undefined && this.required_props.indexOf(key) === -1 && new_obj !== this.state[key]) new_state[key] = new_obj;
else if (new_obj && new_obj !== this.state[key]) new_state[key] = new_obj;
++all_objects_counter;
if (new_obj !== undefined) ++resolved_objects_counter;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_key_refs )
{
let prop = props[key] || this.dynamic_prop[key] || this.default_props[key];
if(prop) {
let new_obj = ChainStore.getAccountRefsOfKey(prop);
if (new_obj === undefined && this.required_props.indexOf(key) === -1 && new_obj !== this.state[key]) new_state[key] = new_obj;
else if (new_obj && new_obj !== this.state[key]) new_state[key] = new_obj;
++all_objects_counter;
if (new_obj !== undefined) ++resolved_objects_counter;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_address_balances )
{
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let new_obj = ChainStore.getBalanceObjects(prop);
if (new_obj === undefined && this.required_props.indexOf(key) === -1 && new_obj !== this.state[key]) new_state[key] = new_obj;
else if (new_obj && new_obj !== this.state[key]) new_state[key] = new_obj;
++all_objects_counter;
if (new_obj !== undefined) ++resolved_objects_counter;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_assets )
{
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let new_obj = ChainStore.getAsset(prop);
if (new_obj === undefined && this.required_props.indexOf(key) === -1 && new_obj !== this.state[key]) new_state[key] = new_obj;
else if (new_obj && new_obj !== this.state[key]) new_state[key] = new_obj;
++all_objects_counter;
if (new_obj !== undefined) ++resolved_objects_counter;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_objects_list )
{
//console.log("-- Wrapper.update -->", this.chain_objects_list);
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let prop_prev_state = this.state[key];
let prop_new_state = [];
let changes = false;
if(!prop_prev_state || prop_prev_state.length !== prop.size) {
prop_prev_state = [];
changes = true;
}
let index = 0;
prop.forEach( obj_id => {
++index;
//console.log("-- Wrapper.chain_objects_list item -->", obj_id, index);
if(obj_id) {
let new_obj = ChainStore.getObject(obj_id);
if(new_obj) ++resolved_objects_counter;
if(prop_prev_state[index] !== new_obj) {
changes = true;
prop_new_state[index] = new_obj;
} else {
prop_new_state[index] = prop_prev_state[index];
}
}
++all_objects_counter;
});
//console.log("-- Wrapper.chain_objects_list: ", prop_new_state);
if(changes) new_state[key] = prop_new_state;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_accounts_list )
{
//console.log("-- Wrapper.update -->", this.chain_accounts_list);
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let prop_prev_state = this.state[key];
let prop_new_state = [];
let changes = false;
if(!prop_prev_state || prop_prev_state.length !== prop.size) {
prop_prev_state = [];
changes = true;
}
let index = 0;
prop.forEach( obj_id => {
//console.log("-- Wrapper.chain_accounts_list item -->", obj_id, index);
if(obj_id) {
let new_obj = ChainStore.getAccount(obj_id);
// console.info('new_obj222222222222222',obj_id,JSON.parse(JSON.stringify(new_obj)));
if(new_obj) ++resolved_objects_counter;
if(prop_prev_state[index] !== new_obj) {
changes = true;
prop_new_state[index] = new_obj;
} else {
prop_new_state[index] = prop_prev_state[index];
}
}
++index;
++all_objects_counter;
});
// console.log("-- Wrapper.chain_accounts_list: ",JSON.parse(JSON.stringify(prop_new_state)));
if(changes) new_state[key] = prop_new_state;
} else {
if(this.state[key]) new_state[key] = null;
}
}
for( let key of this.chain_assets_list )
{
//console.log("-- Wrapper.update -->", this.chain_assets_list);
let prop = props[key] || this.dynamic_props[key] || this.default_props[key];
if(prop) {
let prop_prev_state = this.state[key];
let prop_new_state = [];
let changes = false;
if(!prop_prev_state || prop_prev_state.length !== prop.size) {
prop_prev_state = [];
changes = true;
}
let index = 0;
prop.forEach( obj_id => {
++index;
//console.log("-- Wrapper.chain_assets_list item -->", obj_id, index);
if(obj_id) {
let new_obj = ChainStore.getAsset(obj_id);
if(new_obj) ++resolved_objects_counter;
if(prop_prev_state[index] !== new_obj) {
changes = true;
prop_new_state[index] = new_obj;
} else {
prop_new_state[index] = prop_prev_state[index];
}
}
++all_objects_counter;
});
//console.log("-- Wrapper.chain_assets_list: ", prop_new_state);
if(changes) new_state[key] = prop_new_state;
} else {
if(this.state[key]) new_state[key] = null;
}
}
//console.log("----- Wrapper update ----->", this.all_chain_props, this.all_chain_props.length, all_objects_counter, resolved_objects_counter);
if(all_objects_counter <= resolved_objects_counter) new_state.resolved = true;
this.setState( new_state );
}
componentName() {
let cf = Component.toString();
return cf.substr(9, cf.indexOf("(") - 9);
}
render() {
const props = omit(this.props, this.all_chain_props);
//console.log("----- Wrapper render ----->", this.componentName(), this.props, this.state);
// console.info('this.required_props',this.required_props);
for (let prop of this.required_props) {
if(!this.state[prop]) {
// console.info('options',options);
if (typeof options !== "undefined" && options.show_loader) {
return <Loading />;
} else {
// returning a temp component of the desired type prevents invariant violation errors, notably when rendering tr components
// to use, specicy a defaultProps field of tempComponent: "tr" (or "div", "td", etc as desired)
// console.info('this.tempComponent1111',this.tempComponent);
return this.tempComponent ? React.createElement(this.tempComponent) : <span></span>;
}
}
}
//return <span className={this.state.resolved ? "resolved":"notresolved"}><Component {...props} {...this.state}/></span>;
return <Component {...props} {...this.state}/>;
}
};
}
class Wrapper extends React.Component {
render() {
return <span className="wrapper">
{this.props.children(this.props)}
</span>;
}
}
Wrapper = BindToChainState(Wrapper, {all_props: true, require_all_props: true});
BindToChainState.Wrapper = Wrapper;
export default BindToChainState;