![](https://steemeditor.com/storage/images/adtqN21Adq2ecBCXpFvGd8m76yvNEpqndNaDLYYC.png) 目前正在开发针对 `Bitshares` 的测试工具,一开始就让我遇到了疑惑,先来看代码: ``` import {Apis} from "bitsharesjs-ws"; var {ChainStore} = require("bitsharesjs"); Apis.instance("wss://eu.nodes.bitshares.ws", true).init_promise.then((res) => { console.log("connected to:", res[0].network); ChainStore.init().then(() => { ChainStore.subscribe(updateState); }); }); let dynamicGlobal = null; function updateState(object) { dynamicGlobal = ChainStore.getObject("2.1.0"); console.log("ChainStore object update\n", dynamicGlobal ? dynamicGlobal.toJS() : dynamicGlobal); } ``` 上面这段代码是来自 `bitshares-js` 的官方文档的示例代码,其中我有两个疑惑,一个是 `API` 地址的设置,一个是 `subscribe` 是在什么时候调用的,`ChainStore` 到底该如何使用。 这篇文章是来写第一个疑惑的。 通过翻代码,可以看到在 `ChainStore.init()` 中也有调用 `Apis.instance()`,但是却没有方法指定 `API` 的 `URL`。于是我很好奇这个到底是怎么确定 `ChainStore` 在使用哪个节点呢? 去看 `bitshares-ws` 中关于 `Apis.instance()` 的代码,发现有其中一段代码 ``` var Apis = null; export const instance = ( cs = "ws://localhost:8090", connect, connectTimeout = 4000, optionalApis, closeCb ) => { if (!Apis) { Apis = newApis(); Apis.setRpcConnectionStatusCallback(statusCb); } if (Apis && connect) { Apis.connect(cs, connectTimeout, optionalApis); } if (closeCb) Apis.closeCb = closeCb; return Apis; }; ``` 这里我们可以看到在 `Apis.instance()` 这个库代码里,有一个变量 `Apis`,对于我这种 `js` 半路出家的人来说,并不理解这个 `Apis` 到底是在哪个局部生效的。 也就是说,我在同一个项目的不同位置,`import` 同一个库的时候,库里的变量到底是指向两个内存地址,还是指向了同一个内存地址。按照目前 `bitshares-js` 给的示例代码来猜测,是指向了同一个内存地址。 于是我自己写了个简单的例子,来测试了一下。 ``` -- main.js -- import t from './test2'; import init from './test1'; t(); const anotherGVal = init(); console.log('another g val:', anotherGVal); -- test1.js -- var g = null; export const init = () => { if (!g) { console.log('not defined'); g = 1; } console.log('has defined'); return g; } -- test2.js -- import init from './test1'; export const t = () => { const gVal = init(); console.log('gVal is:', gVal); } ``` 最终指向结果就是,只打印了一次 `not defined`,也就说明了,在我的测试代码中,第二次调用 `test1.init()` 的时候,变量 `g` 其实已经存在了。 这也就说明了 `ChainStore.init()` 中再次调用 `Apis.instance()` 的时候,由于之前已经调用过,所以 `Apis` 已经存在了。 再后来我又搜索了关于 `javascript` 中 `import` 相关的文章,发现了这篇文章 [https://zhuanlan.zhihu.com/p/33843378](https://zhuanlan.zhihu.com/p/33843378) 。 **总结下就是在同个项目下, `import` 同一个库只会执行一次,且返回结果是引用。** --- #### ET碎碎念,每周一,晚六点一刻更新,欢迎订阅 ![](https://steemeditor.com/storage/images/IucEYuEtIxqdcAGJ9NxOVuZTLhd2cyvBKliqds2g.jpeg) --- ### 欢迎使用 [SteemEditor](https://steemeditor.com) 来编写文章,[获取 @steemeditor.bot 的点赞!](https://steemit.com/cn/@steemeditor.bot/steemeditor) --- **感谢你的阅读,我是中文区见证人之一,欢迎通过 [SteemConnect](https://v2.steemconnect.com/sign/account-witness-vote?witness=ety001&approve=1) 来给我投票,或者打开 [https://steemitwallet.com/~witnesses](https://steemitwallet.com/~witnesses) 页面,输入 *ety001* 进行投票。** ![2.gif](https://steemitimages.com/DQmZfJo3F8NXpFx7nenQA3zTU9jg6YoRQvfyeeHgSYpdqr4/2.gif) **中文区的见证人目前有:** 支持一下他们(按字母顺序),*一人可以有30票*: - @abit | [投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=abit) - @bobdos | [投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=bobdos) - @ety001| [投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=ety001) - @justyy |[投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=justyy) - @oflyhigh | [投票](https://steemconnect.com/sign/account_witness_vote?approve=1&witness=oflyhigh) --- **Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by [SteemConnect](https://v2.steemconnect.com/sign/account-witness-vote?witness=ety001&approve=1). Or open [https://steemitwallet.com/~witnesses](https://steemitwallet.com/~witnesses) page, input *ety001* to vote.** ![2.gif](https://steemitimages.com/DQmZfJo3F8NXpFx7nenQA3zTU9jg6YoRQvfyeeHgSYpdqr4/2.gif)
post_id | 82,482,874 | ||||||
---|---|---|---|---|---|---|---|
author | ety001 | ||||||
permlink | bitshares-ws-api | ||||||
category | cn | ||||||
json_metadata | {"tags":["cn-dev"],"app":"steemit\/0.1","format":"markdown","app_url":"https:\/\/steemeditor.com","users":["abit","bobdos","ety001","justyy","oflyhigh"],"image":["https:\/\/steemeditor.com\/storage\/images\/adtqN21Adq2ecBCXpFvGd8m76yvNEpqndNaDLYYC.png","https:\/\/steemeditor.com\/storage\/images\/IucEYuEtIxqdcAGJ9NxOVuZTLhd2cyvBKliqds2g.jpeg","https:\/\/steemitimages.com\/DQmZfJo3F8NXpFx7nenQA3zTU9jg6YoRQvfyeeHgSYpdqr4\/2.gif"],"links":["https:\/\/zhuanlan.zhihu.com\/p\/33843378","https:\/\/steemeditor.com","https:\/\/steemit.com\/cn\/@steemeditor.bot\/steemeditor","https:\/\/v2.steemconnect.com\/sign\/account-witness-vote?witness=ety001&approve=1","https:\/\/steemitwallet.com\/~witnesses","https:\/\/steemconnect.com\/sign\/account_witness_vote?approve=1&witness=abit","https:\/\/steemconnect.com\/sign\/account_witness_vote?approve=1&witness=bobdos","https:\/\/steemconnect.com\/sign\/account_witness_vote?approve=1&witness=ety001","https:\/\/steemconnect.com\/sign\/account_witness_vote?approve=1&witness=justyy","https:\/\/steemconnect.com\/sign\/account_witness_vote?approve=1&witness=oflyhigh"]} | ||||||
created | 2019-12-12 01:16:45 | ||||||
last_update | 2019-12-13 14:12:45 | ||||||
depth | 0 | ||||||
children | 3 | ||||||
net_rshares | 9,810,949,042,101 | ||||||
last_payout | 2019-12-19 01:16:45 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 1.182 SBD | ||||||
curator_payout_value | 1.174 SBD | ||||||
pending_payout_value | 0.000 SBD | ||||||
promoted | 0.000 SBD | ||||||
body_length | 3,875 | ||||||
author_reputation | 190,546,071,796,324 | ||||||
root_title | "bitshares-ws 的 API 配置疑惑" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 SBD | ||||||
percent_steem_dollars | 10,000 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
abit | 0 | 1,254,195,465,321 | 100% | ||
jademont | 0 | 426,729,367,609 | 100% | ||
oflyhigh | 0 | 3,827,075,917,461 | 100% | ||
tumutanzi | 0 | 9,504,739,699 | 14.37% | ||
justyy | 0 | 637,600,391,097 | 22.54% | ||
simonjay | 0 | 99,284,286,166 | 41% | ||
shenchensucc | 0 | 47,953,309,598 | 8.62% | ||
ety001 | 0 | 27,213,407,080 | 100% | ||
idx | 0 | 21,764,228,502 | 100% | ||
liumei | 0 | 555,379,053 | 6% | ||
davidke20 | 0 | 8,020,443,043 | 4% | ||
namchau | 0 | 2,967,609,362 | 70% | ||
cnbuddy | 0 | 506,995,594,079 | 28.75% | ||
lebin | 0 | 262,674,564,355 | 100% | ||
youandme | 0 | 712,426,077 | 100% | ||
nostalgic1212 | 0 | 36,663,594,386 | 25% | ||
andrewma | 0 | 2,749,472,828 | 14.37% | ||
springfall | 0 | 684,013,034 | 100% | ||
fishlucy | 0 | 13,377,171,379 | 50% | ||
tydebbie | 0 | 852,950,339 | 6% | ||
moneybaby | 0 | 1,216,084,499 | 2.5% | ||
ybeyond | 0 | 590,258,481 | 12% | ||
team-cn | 0 | 100,820,187,348 | 12% | ||
coder-bts | 0 | 1,168,568,391 | 14.37% | ||
steemeditor.bot | 0 | 4,420,878,079 | 100% | ||
wanggang | 0 | 43,276,187,710 | 5.75% | ||
botreporter | 0 | 8,943,474,648 | 100% | ||
yuwen | 0 | 543,771,701 | 100% | ||
shuxue | 0 | 543,771,647 | 100% | ||
shengwu | 0 | 541,587,715 | 100% | ||
verifyme | 0 | 692,835,633 | 10% | ||
sunai | 0 | 22,631,870,945 | 100% | ||
melaniewang | 0 | 2,275,342,003 | 14.37% | ||
changxiu | 0 | 1,206,247,980 | 14.37% | ||
cherryzz | 0 | 3,605,105,409 | 14.37% | ||
sasaadrian | 0 | 18,496,950,912 | 20% | ||
wherein | 0 | 1,576,265,883,106 | 35% | ||
tina3721 | 0 | 1,357,742,120 | 14.37% | ||
nympheas | 0 | 1,483,370,837 | 9.6% | ||
cnvote | 0 | 581,486,351,158 | 100% | ||
cnbuddy-reward | 0 | 249,102,348,672 | 28.75% | ||
real3earch | 0 | 2,715,062,964 | 28.75% | ||
grenilcu | 0 | -9,170,325 | -10% |
你那里天气如何?新人吗?[《steemit指南》](http://steemh.org/)拿一份吧,以免迷路; 另外一定要去 @team-cn 的新手村看看,超级热闹的大家庭。倘若你想让我隐形,请回复“取消”。
post_id | 82,483,194 |
---|---|
author | cnbuddy |
permlink | re-ety001-bitshares-ws-api-20191212t013259786z |
category | cn |
json_metadata | {} |
created | 2019-12-12 01:33:00 |
last_update | 2019-12-12 01:33:00 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-12-19 01:33:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 105 |
author_reputation | -1,405,328,253,928 |
root_title | "bitshares-ws 的 API 配置疑惑" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
感谢代理200.26SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 12% 点赞!(如果不想看到这个回复,请回复“取消”)
post_id | 82,483,334 |
---|---|
author | team-cn |
permlink | re-ety001-bitshares-ws-api-20191212t014015949z |
category | cn |
json_metadata | "{"app":"NBC bot"}" |
created | 2019-12-12 01:40:18 |
last_update | 2019-12-12 01:40:18 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-12-19 01:40:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 89 |
author_reputation | 41,051,906,712,730 |
root_title | "bitshares-ws 的 API 配置疑惑" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
@ety001 You have received a 100% upvote from @botreporter because this post did not use any bidbots and you have not used bidbots in the last 30 days! Upvoting this comment will help keep this service running.
post_id | 82,528,696 |
---|---|
author | verifyme |
permlink | re-bitshares-ws-api-ety001-verifyme-randomvote |
category | cn |
json_metadata | {"tags":["verifyme","random-upvote"],"users":["ety001","botreporter"],"app":"null\/null","format":"markdown"} |
created | 2019-12-13 18:01:21 |
last_update | 2019-12-13 18:01:21 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2019-12-20 18:01:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 212 |
author_reputation | -704,332,579,924 |
root_title | "bitshares-ws 的 API 配置疑惑" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |