Skip to content

Commit

Permalink
Merge b027ffc into 9b448cb
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Jan 21, 2019
2 parents 9b448cb + b027ffc commit ccb058a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 15 deletions.
85 changes: 74 additions & 11 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as _methods from "const/method";
import { requestTimeout } from 'const/error';
import { RPCrequest, RPCresponse, Methods } from "const/type";
import txBlock from './txBlock';
import ledger from './ledger';

export default class client {
_provider: any
isConnected: Boolean
buildinTxBlock: txBlock
buildinLedger: ledger
private requestList: Array<any>

wallet: _methods.walletFunc
net: _methods.netFunc
Expand All @@ -25,23 +28,48 @@ export default class client {
this.buildinTxBlock = new txBlock(this);
this.buildinLedger = new ledger(this);

firstConnect && this.connectedOnce(firstConnect);
this._setMethodsName();
this.isConnected = false;
this.connectedOnce(firstConnect);
this.requestList = [];
}

setProvider(provider, firstConnect, abort) {
abort && this._provider.abort(abort);
this._provider = provider;

this.isConnected = false;
this.connectedOnce(firstConnect);

let providerType = this._provider.type || 'http';
if (providerType.toLowerCase !== 'ipc' || this.wallet) {
return;
}

this._setMethodsName();
}

connectedOnce(cb) {
if (this._provider.type === 'http' || this._provider.connectStatus) {
let connectedCB = () => {
this.isConnected = true;
this.requestList.forEach((_q) => {
_q && _q();
});
cb && cb(this);
}

if (this._provider.type === 'http' || this._provider.connectStatus) {
connectedCB();
return;
}

this._provider.on('connect', () => {
cb && cb(this);
connectedCB();
this._provider.remove('connect');
});
}

_setMethodsName() {
private _setMethodsName() {
let providerType = (this._provider.type || 'http').toLowerCase();
for (let namespace in _methods) {
if (providerType === 'ipc' && namespace === 'wallet') {
Expand All @@ -65,18 +93,45 @@ export default class client {
}
}

setProvider(provider, abort) {
abort && this._provider.abort(abort);
this._provider = provider;

let providerType = this._provider.type || 'http';
if (providerType.toLowerCase !== 'ipc' || this.wallet) {
private _offReq(_q) {
let i;
for (i=0; i<this.requestList.length; i++) {
if (this.requestList[i] === _q) {
break;
}
}
if (i === this.requestList.length) {
return;
}
this._setMethodsName();
this.requestList.splice(i, 1);
}

private _onReq(type, methods, ...args) {
return new Promise((res, rej) => {
let _q = () => {
this[type](methods, ...args).then((data) => {
clearTimeout(_timeout);
res(data);
}).catch((err) => {
clearTimeout(_timeout);
rej(err);
});
};

this.requestList.push(_q);

let _timeout = setTimeout(() => {
this._offReq(_q);
rej(requestTimeout);
}, this._provider._timeout || 30000);
});
}

async request(methods: Methods, ...args: any[]) {
if (!this.isConnected) {
return this._onReq('request', methods, ...args);
}

const rep: RPCresponse = await this._provider.request(methods, args);
if (rep.error) {
throw rep.error
Expand All @@ -85,10 +140,18 @@ export default class client {
}

async notification(methods: Methods, ...args: any[]) {
if (!this.isConnected) {
return this._onReq('notification', methods, ...args);
}

return this._provider.notification(methods, args);
}

async batch(reqs: RPCrequest[]) {
if (!this.isConnected) {
return this._onReq('batch', reqs);
}

reqs.forEach(v => {
v.type = v.type || 'request'
});
Expand Down
6 changes: 3 additions & 3 deletions src/client/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export default class ledger {
return Promise.reject(err);
}

const data:RPCresponse[] = await this._client.batch([{
const data = await this._client.batch([{
methodName: _ledger.getAccountByAccAddr,
params: [addr]
}, {
methodName: onroad.getAccountOnroadInfo,
params: [addr]
}]);

if (!data || +data.length < 2) {
if (!data || (data instanceof Array && data.length < 2)) {
return null;
}

Expand Down Expand Up @@ -72,7 +72,7 @@ export default class ledger {
})
}

const data:RPCresponse[] = await this._client.batch(requests);
const data = await this._client.batch(requests);

let rawList;
requests.forEach((_r, i) => {
Expand Down
2 changes: 1 addition & 1 deletion src/client/txBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class tx {
return reject(no);
}

let req: RPCresponse[] = await this._client.batch(requests);
let req = await this._client.batch(requests);
let latestBlock;

requests.forEach((_r, index) =>{
Expand Down
2 changes: 2 additions & 0 deletions src/const/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const Default_Gid = '00000000000000000001';
// snapshot = "00000000000000000001"
// case delegate = "00000000000000000002"
export const Default_Hash = '0000000000000000000000000000000000000000000000000000000000000000'; // A total of 64 0
export const Quota_Addr = 'vite_000000000000000000000000000000000000000309508ba646';
export const Vote_Addr = 'vite_000000000000000000000000000000000000000270a48cc491';
Expand Down
5 changes: 5 additions & 0 deletions src/const/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ export const addressMissing = {
code: '200002',
message: 'Address does not exist'
}

export const requestTimeout = {
code: '300001',
message: 'Request timeout'
}

0 comments on commit ccb058a

Please sign in to comment.