You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This can't be used reliably without waiting for initialization. Therefore, it should include an init() function that provides that boilerplate once, rather than leaving it up to every downstream dev to implement it, possibly incorrectly / missing edge cases.
(in fact, there have already been bugs in dashcore-lib related to this problem)
Example Usage
letrpc=newRpcClient(rpcConfig);awaitrpc.init({retry: 5000}).catch(function(err){throwerr;});// (if using the shim below)rpc.onconnected=rpcConfig.onconnected;
Example Shim
dashd-rpc-shim.js:
'use strict';letRpcClient=require('@dashevo/dashd-rpc');RpcClient.E_IN_WARMUP=-28;RpcClient.prototype.init=asyncfunction(opts){letrpc=this;rpc._connected=false;letretry=opts?.retry||5000;letheight=0;for(;;){height=awaitRpcClient._getHeight(rpc);if(height){break;}awaitsleep(retry);}returnheight;};functionsleep(ms){returnnewPromise(function(resolve){setTimeout(resolve,ms);});}RpcClient._getHeight=asyncfunction(rpc){letwarn=null;lettip=awaitrpc.getChainTips()//.getBestBlockHash().then(function(result){// { id, error, result }if(result.error){// impossible, but we'll check anywaythrownewError(result.error);}if(!result.result?.[0].height){// also impossible, and we still check anywaythrownewError('Sanity Fail: missing tip');}returnresult.result[0].height;}).catch(function(e){if(e.code===RpcClient.E_IN_WARMUP){warn=e;return0;}throwe;});if(!rpc._connected){rpc._connected=true;letonconnected=rpc.onconnected||RpcClient._onconnected;voidonconnected.call(rpc,warn);}returntip;};RpcClient._onconnected=function(){letrpc=this;console.info(`[dashd-rpc] client connected to ${rpc.host}:${rpc.port}`);};
Update This is in #72, and readily available in https://github.com/dashhive/DashRPC.js
This can't be used reliably without waiting for initialization. Therefore, it should include an
init()
function that provides that boilerplate once, rather than leaving it up to every downstream dev to implement it, possibly incorrectly / missing edge cases.(in fact, there have already been bugs in dashcore-lib related to this problem)
Example Usage
Example Shim
dashd-rpc-shim.js
:Example Internal changes
The text was updated successfully, but these errors were encountered: