Skip to content

Commit

Permalink
ws client refactoring: make it more simple by calling onMessage funct…
Browse files Browse the repository at this point in the history
…ion from ws.message handler
  • Loading branch information
valzav committed Jun 22, 2017
1 parent e46ec11 commit 2204f6e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 58 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
101 changes: 43 additions & 58 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Steem extends EventEmitter {
this.apiIds = this.options.apiIds;
this.isOpen = false;
this.releases = [];
this.requestsTime = {};
this.requests = {};

// A Map of api name to a promise to it's API ID refresh call
this.apiIdsP = {};
Expand Down Expand Up @@ -94,12 +94,13 @@ class Steem extends EventEmitter {
debugWs('Received message', message.data);
const data = JSON.parse(message.data);
const id = data.id;
const msToRespond = Date.now() - this.requestsTime[id];
delete this.requestsTime[id];
if (msToRespond > expectedResponseMs) {
debugWs(`Message received in ${msToRespond}ms, it's over the expected response time of ${expectedResponseMs}ms`, message.data);
const request = this.requests[id];
if (!request) {
console.error('Steem.onMessage error: unknown request ', id);
return;
}
this.emit('message', data, msToRespond);
delete this.requests[id];
this.onMessage(data, request);
});

this.releases = this.releases.concat([
Expand Down Expand Up @@ -170,21 +171,35 @@ class Steem extends EventEmitter {
return Promise.props(this.apiIdsP);
}

// waitForSlot() {
// if (this.inFlight < 10) {
// debugEmitters('Less than 10 in-flight messages, moving on');
// return null;
// }
//
// debugEmitters('More than 10 in-flight messages, waiting');
// return Promise.delay(100).then(() => {
// if (this.inFlight < 10) {
// debugEmitters('Less than 10 in-flight messages, moving on');
// return null;
// }
// return this.waitForSlot();
// });
// }

onMessage(message, request) {
const {api, data, resolve, reject, start_time} = request;
console.log('-- Steem.onMessage -->', message.id);
const errorCause = message.error;
if (errorCause) {
const err = new Error(
// eslint-disable-next-line prefer-template
(errorCause.message || 'Failed to complete operation') +
' (see err.payload for the full error payload)'
);
err.payload = message;
reject(err);
return;
}

if (api === 'login_api' && data.method === 'login') {
debugApiIds(
'network_broadcast_api API ID depends on the WS\' session. ' +
'Triggering a refresh...'
);
this.getApiIds('network_broadcast_api', true);
}

debugProtocol('Resolved', api, data, '->', message);
this.emit('track-performance', data.method, Date.now() - start_time);
delete this.requests[message.id];
resolve(message.result);
}

send(api, data, callback) {
debugSetup('Steem::send', api, data);
Expand Down Expand Up @@ -220,44 +235,14 @@ class Steem extends EventEmitter {
],
});

const release = this.listenTo(this, 'message', (message, time_taken) => {
// We're still seeing old messages
if (message.id !== id) {
debugProtocol('Different message was dropped', message);
return;
}

// this.inFlight -= 1;
release();

// Our message's response came back
const errorCause = message.error;
if (errorCause) {
const err = new Error(
// eslint-disable-next-line prefer-template
(errorCause.message || 'Failed to complete operation') +
' (see err.payload for the full error payload)'
);
err.payload = message;
reject(err);
return;
}

if (api === 'login_api' && data.method === 'login') {
debugApiIds(
'network_broadcast_api API ID depends on the WS\' session. ' +
'Triggering a refresh...'
);
this.getApiIds('network_broadcast_api', true);
}

debugProtocol('Resolved', api, data, '->', message);
this.emit('track-performance', data.method, time_taken);
resolve(message.result);
});

debugWs('Sending message', payload);
this.requestsTime[id] = Date.now();
this.requests[id] = {
api,
data,
resolve,
reject,
start_time: Date.now()
};

// this.inFlight += 1;
this.ws.send(payload);
Expand Down

0 comments on commit 2204f6e

Please sign in to comment.