bitshares-ws 的 API 配置疑惑 by ety001

View this thread on steempeak.com
· @ety001 · (edited)
$2.36
bitshares-ws 的 API 配置疑惑
![](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)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
👎  
properties (23)
post_id82,482,874
authorety001
permlinkbitshares-ws-api
categorycn
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"]}
created2019-12-12 01:16:45
last_update2019-12-13 14:12:45
depth0
children3
net_rshares9,810,949,042,101
last_payout2019-12-19 01:16:45
cashout_time1969-12-31 23:59:59
total_payout_value1.182 SBD
curator_payout_value1.174 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,875
author_reputation190,546,071,796,324
root_title"bitshares-ws 的 API 配置疑惑"
beneficiaries
0.
accountsteemeditor.bot
weight500
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (43)
@cnbuddy ·
你那里天气如何?新人吗?[《steemit指南》](http://steemh.org/)拿一份吧,以免迷路; 另外一定要去 @team-cn 的新手村看看,超级热闹的大家庭。倘若你想让我隐形,请回复“取消”。
properties (22)
post_id82,483,194
authorcnbuddy
permlinkre-ety001-bitshares-ws-api-20191212t013259786z
categorycn
json_metadata{}
created2019-12-12 01:33:00
last_update2019-12-12 01:33:00
depth1
children0
net_rshares0
last_payout2019-12-19 01:33:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length105
author_reputation-1,405,328,253,928
root_title"bitshares-ws 的 API 配置疑惑"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@team-cn ·
感谢代理200.26SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 12% 点赞!(如果不想看到这个回复,请回复“取消”)
properties (22)
post_id82,483,334
authorteam-cn
permlinkre-ety001-bitshares-ws-api-20191212t014015949z
categorycn
json_metadata"{"app":"NBC bot"}"
created2019-12-12 01:40:18
last_update2019-12-12 01:40:18
depth1
children0
net_rshares0
last_payout2019-12-19 01:40:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length89
author_reputation41,051,906,712,730
root_title"bitshares-ws 的 API 配置疑惑"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@verifyme ·
@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.
properties (22)
post_id82,528,696
authorverifyme
permlinkre-bitshares-ws-api-ety001-verifyme-randomvote
categorycn
json_metadata{"tags":["verifyme","random-upvote"],"users":["ety001","botreporter"],"app":"null\/null","format":"markdown"}
created2019-12-13 18:01:21
last_update2019-12-13 18:01:21
depth1
children0
net_rshares0
last_payout2019-12-20 18:01:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length212
author_reputation-704,332,579,924
root_title"bitshares-ws 的 API 配置疑惑"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000