Skip to content

Commit b16c242

Browse files
author
Fabien
authored
Merge pull request #180 from steemit/ws-client-refactoring
ws client refactoring
2 parents e46ec11 + 2204f6e commit b16c242

2 files changed

Lines changed: 63 additions & 58 deletions

File tree

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
# Change these settings to your own preference
10+
indent_style = space
11+
indent_size = 2
12+
13+
# We recommend you to keep these unchanged
14+
end_of_line = lf
15+
charset = utf-8
16+
trim_trailing_whitespace = true
17+
insert_final_newline = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

src/api/index.js

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Steem extends EventEmitter {
4747
this.apiIds = this.options.apiIds;
4848
this.isOpen = false;
4949
this.releases = [];
50-
this.requestsTime = {};
50+
this.requests = {};
5151

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

105106
this.releases = this.releases.concat([
@@ -170,21 +171,35 @@ class Steem extends EventEmitter {
170171
return Promise.props(this.apiIdsP);
171172
}
172173

173-
// waitForSlot() {
174-
// if (this.inFlight < 10) {
175-
// debugEmitters('Less than 10 in-flight messages, moving on');
176-
// return null;
177-
// }
178-
//
179-
// debugEmitters('More than 10 in-flight messages, waiting');
180-
// return Promise.delay(100).then(() => {
181-
// if (this.inFlight < 10) {
182-
// debugEmitters('Less than 10 in-flight messages, moving on');
183-
// return null;
184-
// }
185-
// return this.waitForSlot();
186-
// });
187-
// }
174+
175+
onMessage(message, request) {
176+
const {api, data, resolve, reject, start_time} = request;
177+
console.log('-- Steem.onMessage -->', message.id);
178+
const errorCause = message.error;
179+
if (errorCause) {
180+
const err = new Error(
181+
// eslint-disable-next-line prefer-template
182+
(errorCause.message || 'Failed to complete operation') +
183+
' (see err.payload for the full error payload)'
184+
);
185+
err.payload = message;
186+
reject(err);
187+
return;
188+
}
189+
190+
if (api === 'login_api' && data.method === 'login') {
191+
debugApiIds(
192+
'network_broadcast_api API ID depends on the WS\' session. ' +
193+
'Triggering a refresh...'
194+
);
195+
this.getApiIds('network_broadcast_api', true);
196+
}
197+
198+
debugProtocol('Resolved', api, data, '->', message);
199+
this.emit('track-performance', data.method, Date.now() - start_time);
200+
delete this.requests[message.id];
201+
resolve(message.result);
202+
}
188203

189204
send(api, data, callback) {
190205
debugSetup('Steem::send', api, data);
@@ -220,44 +235,14 @@ class Steem extends EventEmitter {
220235
],
221236
});
222237

223-
const release = this.listenTo(this, 'message', (message, time_taken) => {
224-
// We're still seeing old messages
225-
if (message.id !== id) {
226-
debugProtocol('Different message was dropped', message);
227-
return;
228-
}
229-
230-
// this.inFlight -= 1;
231-
release();
232-
233-
// Our message's response came back
234-
const errorCause = message.error;
235-
if (errorCause) {
236-
const err = new Error(
237-
// eslint-disable-next-line prefer-template
238-
(errorCause.message || 'Failed to complete operation') +
239-
' (see err.payload for the full error payload)'
240-
);
241-
err.payload = message;
242-
reject(err);
243-
return;
244-
}
245-
246-
if (api === 'login_api' && data.method === 'login') {
247-
debugApiIds(
248-
'network_broadcast_api API ID depends on the WS\' session. ' +
249-
'Triggering a refresh...'
250-
);
251-
this.getApiIds('network_broadcast_api', true);
252-
}
253-
254-
debugProtocol('Resolved', api, data, '->', message);
255-
this.emit('track-performance', data.method, time_taken);
256-
resolve(message.result);
257-
});
258-
259238
debugWs('Sending message', payload);
260-
this.requestsTime[id] = Date.now();
239+
this.requests[id] = {
240+
api,
241+
data,
242+
resolve,
243+
reject,
244+
start_time: Date.now()
245+
};
261246

262247
// this.inFlight += 1;
263248
this.ws.send(payload);

0 commit comments

Comments
 (0)