Skip to content

Commit

Permalink
Use socket API (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds authored and sindresorhus committed Mar 15, 2018
1 parent e6235b0 commit d1cd3a4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/renderer/api.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import {sha256} from 'crypto-hash';
import PQueue from 'p-queue';
import electrumServers from './electrum-servers';
import MarketmakerSocket from './marketmaker-socket';

export default class Api {
constructor({endpoint, seedPhrase, concurrency = 1}) {
constructor({endpoint, seedPhrase, concurrency = Infinity}) {
if (!(endpoint && seedPhrase)) {
throw new Error('The `endpoint` and `seedPhrase` options are required');
}

this.endpoint = endpoint;
this.token = sha256(seedPhrase);
this.socket = false;
this.currentQueued = 0;

this.queue = new PQueue({concurrency});
}

async _request(data) {
const queueId = this.socket ? ++this.currentQueued : 0;

const response = await this.queue.add(() => fetch(this.endpoint, {
method: 'post',
body: JSON.stringify(data),
body: JSON.stringify({
...{queueid: queueId},
...data},
),
}));

return response.json();
return this.socket ? this.socket.getResponse(queueId) : response.json();
}

async enableSocket() {
const {endpoint} = await this.request({method: 'getendpoint'});
const socket = new MarketmakerSocket(endpoint);
await this.socket.connected;
this.socket = socket;

return this.socket;
}

async request(data) {
Expand Down
1 change: 1 addition & 0 deletions app/renderer/containers/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class LoginContainer extends Container {
// TODO: Show some loading here as it takes some time to decrypt the password and then start marketmaker
const seedPhrase = await decryptSeedPhrase(portfolio.encryptedSeedPhrase, password);
const api = await initApi(seedPhrase);
await api.enableSocket();

if (is.development) {
// Expose the API for debugging in DevTools
Expand Down
35 changes: 35 additions & 0 deletions app/renderer/marketmaker-socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Emittery from 'emittery';
import pEvent from 'p-event';
import readBlob from 'read-blob';

class MarketmakerSocket {
constructor(endpoint) {
this._ws = new WebSocket(endpoint, ['pair.sp.nanomsg.org']);
this.connected = pEvent(this._ws, 'open');

const ee = new Emittery();
this._ee = ee;
this.on = ee.on.bind(ee);
this.off = ee.off.bind(ee);
this.once = ee.once.bind(ee);

this._ws.addEventListener('message', this._handleMessage);
}

_handleMessage = async event => {
const json = await readBlob.text(event.data);
const data = JSON.parse(json);
const queueId = data.queueid;
const message = data.result;

if (queueId > 0) {
this._ee.emit(`id_${queueId}`, message);
}

this._ee.emit('message', message);
}

getResponse = queueId => this._ee.once(`id_${queueId}`);
}

export default MarketmakerSocket;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
"dependencies": {
"bip39": "^2.5.0",
"crypto-hash": "^0.1.0",
"emittery": "^0.3.0",
"lodash": "^4.17.4",
"modern-normalize": "^0.4.0",
"p-event": "^1.3.0",
"p-queue": "^2.3.1",
"randoma": "^1.2.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-extras": "^0.5.0",
"react-select": "^1.2.1",
"read-blob": "^1.1.0",
"sass-extras": "^0.3.0",
"unstated": "^1.0.3"
},
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,10 @@ elliptic@^6.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.0"

emittery@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/emittery/-/emittery-0.3.0.tgz#e6dcedabae804b5478c760335ecbbaf159da645c"

emojis-list@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
Expand Down Expand Up @@ -6171,6 +6175,12 @@ p-each-series@^1.0.0:
dependencies:
p-reduce "^1.0.0"

p-event@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/p-event/-/p-event-1.3.0.tgz#8e6b4f4f65c72bc5b6fe28b75eda874f96a4a085"
dependencies:
p-timeout "^1.1.1"

p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"
Expand Down Expand Up @@ -7092,6 +7102,10 @@ react@^16.2.0:
object-assign "^4.1.1"
prop-types "^15.6.0"

read-blob@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/read-blob/-/read-blob-1.1.0.tgz#66792f7bdd0cca00850faccdf11ec6bf2061a791"

read-chunk@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/read-chunk/-/read-chunk-2.1.0.tgz#6a04c0928005ed9d42e1a6ac5600e19cbc7ff655"
Expand Down

0 comments on commit d1cd3a4

Please sign in to comment.