From 8587d7a693c59a7c02d5a0c32f1974d2955a76de Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Mon, 20 Aug 2018 15:12:38 -0700 Subject: [PATCH] Create rpc-client --- src/program.js | 33 ++++----------------------------- src/rpc-client.js | 29 +++++++++++++++++++++++++++++ src/web3-sol.js | 3 +++ 3 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 src/rpc-client.js diff --git a/src/program.js b/src/program.js index 46314c9..104a416 100644 --- a/src/program.js +++ b/src/program.js @@ -1,36 +1,11 @@ import EventEmitter from 'event-emitter'; -import jayson from 'jayson/lib/client/browser'; -import fetch from 'node-fetch'; -import promisify from 'promisify'; - -const promisify_jayson = promisify.object({ - request: promisify.cb_func(), -}); - -const rpcClient = promisify_jayson(jayson( - async (request, callback) => { - const options = { - method: 'POST', - body: request, - headers: { - 'Content-Type': 'application/json', - } - }; - - try { - const res = await fetch(window.location.origin, options); - const text = await res.text(); - callback(null, text); - } catch (err) { - callback(err); - } - } -)); +import {createRpcClient} from './rpc-client'; export class Program { modified = false; _ee = new EventEmitter(); + _rpcClient = createRpcClient(window.location.origin); constructor() { Object.assign(this, { @@ -47,7 +22,7 @@ export class Program { this.uri = uri; try { - const res = await rpcClient.request('load', [uri]); + const res = await this._rpcClient.request('load', [uri]); console.log('load result', res); if (res.error) { @@ -75,7 +50,7 @@ export class Program { name: this.name, source: this.source, }; - const res = await rpcClient.request('save', [program]); + const res = await this._rpcClient.request('save', [program]); console.log('save result', res); if (res.error) { throw new Error(res.error.message); diff --git a/src/rpc-client.js b/src/rpc-client.js new file mode 100644 index 0000000..b570105 --- /dev/null +++ b/src/rpc-client.js @@ -0,0 +1,29 @@ +import jayson from 'jayson/lib/client/browser'; +import fetch from 'node-fetch'; +import promisify from 'promisify'; + +const promisify_jayson = promisify.object({ + request: promisify.cb_func(), +}); + +export function createRpcClient(uri) { + return promisify_jayson(jayson( + async (request, callback) => { + const options = { + method: 'POST', + body: request, + headers: { + 'Content-Type': 'application/json', + } + }; + + try { + const res = await fetch(uri, options); + const text = await res.text(); + callback(null, text); + } catch (err) { + callback(err); + } + } + )); +} diff --git a/src/web3-sol.js b/src/web3-sol.js index ba4b894..8bfbfd7 100644 --- a/src/web3-sol.js +++ b/src/web3-sol.js @@ -6,6 +6,8 @@ import nacl from 'tweetnacl'; import bs58 from 'bs58'; +import {createRpcClient} from './rpc-client'; + function sleep(duration: number = 0): Promise { return new Promise((accept) => { setTimeout(accept, duration); @@ -19,6 +21,7 @@ export class Web3Sol { balance: 0, endpoint, keypair, + rpcClient: createRpcClient(endpoint), }); }