bitshares-ws 的 API 配置疑惑
目前正在开发针对 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 。
总结下就是在同个项目下, import
同一个库只会执行一次,且返回结果是引用。
ET碎碎念,每周一,晚六点一刻更新,欢迎订阅
欢迎使用 SteemEditor 来编写文章,获取 @steemeditor.bot 的点赞!
感谢你的阅读,我是中文区见证人之一,欢迎通过 SteemConnect 来给我投票,或者打开 https://steemitwallet.com/~witnesses 页面,输入 ety001 进行投票。
中文区的见证人目前有:
支持一下他们(按字母顺序),一人可以有30票:
Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by SteemConnect. Or open https://steemitwallet.com/~witnesses page, input ety001 to vote.
你那里天气如何?新人吗?《steemit指南》拿一份吧,以免迷路; 另外一定要去 @team-cn 的新手村看看,超级热闹的大家庭。倘若你想让我隐形,请回复“取消”。
感谢代理200.26SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 12% 点赞!(如果不想看到这个回复,请回复“取消”)
@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.