Skip to content
This repository has been archived by the owner on Nov 28, 2020. It is now read-only.

Commit

Permalink
Create rpc-client
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Aug 20, 2018
1 parent e956f86 commit 8587d7a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
33 changes: 4 additions & 29 deletions src/program.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions src/rpc-client.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
));
}
3 changes: 3 additions & 0 deletions src/web3-sol.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import nacl from 'tweetnacl';
import bs58 from 'bs58';

import {createRpcClient} from './rpc-client';

function sleep(duration: number = 0): Promise<void> {
return new Promise((accept) => {
setTimeout(accept, duration);
Expand All @@ -19,6 +21,7 @@ export class Web3Sol {
balance: 0,
endpoint,
keypair,
rpcClient: createRpcClient(endpoint),
});
}

Expand Down

0 comments on commit 8587d7a

Please sign in to comment.