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.
Leave bitshares-ws 的 API 配置疑惑 to:
Read more #cn-dev posts
Best Posts From ETY001
We have not curated any of ety001's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ETY001
- 解决CDN缓存301的问题
- Hivians, PLEASE BLOCK ME. Thank you!
- Hive chain coming is a SHAME TIME for Steem.
- 达则兼济天下
- 中国大陆地区第一台不稳定Steem全节点正式上线
- Code Server使用小技巧--禁用浏览器标签页关闭
- 区块链公司该如何保护自己的资产
- 我不站队Sun,但是我为个人资产站队
- 由《Vue.js: The Documentary》想起的一件往事
- 从帮助400+用户7天重温了9000+个书签说起
- 先问是不是,再问为什么
- 解决每次zip压缩后的md5不同的问题
- 这并不是远程工作的元年
- Google Analytics Api 使用
- 《爱情公寓》的剧终
- 最近的三两事
- 全新版本的Chrome扩展“温故知新”已通过审核上线
- 细节描写要适可而止
- 蓄势待发
- 用 js 控制 manifest.json 的 chrome_url_overrides