Allow subscription id & subscription result out of order (#765)

* Bump dependencies

* Allow waiting for out-of-order replies (due to network)

* Add comments around logic

* subId naming

* Bumps (re-apply)
This commit is contained in:
Jaco Greeff
2019-03-08 11:31:21 +01:00
committed by GitHub
parent ea4b9daa3f
commit 7c4371bfa0
8 changed files with 52 additions and 33 deletions
+1 -1
View File
@@ -36,6 +36,6 @@
"@polkadot/rpc-rx": "^0.46.8",
"@polkadot/storage": "^0.46.8",
"@polkadot/types": "^0.46.8",
"@polkadot/util-crypto": "^0.35.6"
"@polkadot/util-crypto": "^0.35.7"
}
}
+1 -1
View File
@@ -33,6 +33,6 @@
"@polkadot/jsonrpc": "^0.46.8",
"@polkadot/rpc-provider": "^0.46.8",
"@polkadot/types": "^0.46.8",
"@polkadot/util": "^0.35.6"
"@polkadot/util": "^0.35.7"
}
}
+3 -3
View File
@@ -30,10 +30,10 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-provider#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@polkadot/keyring": "^0.35.6",
"@polkadot/keyring": "^0.35.7",
"@polkadot/storage": "^0.46.8",
"@polkadot/util": "^0.35.6",
"@polkadot/util-crypto": "^0.35.6",
"@polkadot/util": "^0.35.7",
"@polkadot/util-crypto": "^0.35.7",
"@types/nock": "^9.3.1",
"eventemitter3": "^3.1.0",
"isomorphic-fetch": "^2.2.1",
+26 -7
View File
@@ -72,6 +72,9 @@ export default class WsProvider implements WSProviderInterface {
private subscriptions: {
[index: string]: WsState$Subscription
};
private waitingForId: {
[index: string]: JsonRpcResponse
};
private websocket: WebSocket | null;
/**
@@ -88,6 +91,7 @@ export default class WsProvider implements WSProviderInterface {
this.handlers = {};
this.queued = {};
this.subscriptions = {};
this.waitingForId = {};
this.websocket = null;
if (autoConnect) {
@@ -297,15 +301,24 @@ export default class WsProvider implements WSProviderInterface {
const { method, params, subscription } = handler;
const result = this.coder.decodeResponse(response);
// first send the result - in case of subs, we may have an update
// immediately if we have some queued results already
handler.callback(null, result);
if (subscription) {
this.subscriptions[`${subscription.type}::${result}`] = {
const subId = `${subscription.type}::${result}`;
this.subscriptions[subId] = {
...subscription,
method,
params
};
}
handler.callback(null, result);
// if we have a result waiting for this subscription already
if (this.waitingForId[subId]) {
this.onSocketMessageSubscribe(this.waitingForId[subId]);
}
}
} catch (error) {
handler.callback(error, undefined);
}
@@ -314,17 +327,23 @@ export default class WsProvider implements WSProviderInterface {
}
private onSocketMessageSubscribe = (response: JsonRpcResponse): void => {
const subscription = `${response.method}::${response.params.subscription}`;
const subId = `${response.method}::${response.params.subscription}`;
l.debug(() => ['handling: response =', response, 'subscription =', subscription]);
l.debug(() => ['handling: response =', response, 'subscription =', subId]);
const handler = this.subscriptions[subscription];
const handler = this.subscriptions[subId];
if (!handler) {
l.debug(() => `Unable to find handler for subscription=${subscription}`);
// store the JSON, we could have out-of-order subid coming in
this.waitingForId[subId] = response;
l.debug(() => `Unable to find handler for subscription=${subId}`);
return;
}
// housekeeping
delete this.waitingForId[subId];
try {
const result = this.coder.decodeResponse(response);
+1 -1
View File
@@ -12,6 +12,6 @@
"dependencies": {
"@babel/runtime": "^7.3.4",
"@polkadot/types": "^0.46.8",
"@polkadot/util": "^0.35.6"
"@polkadot/util": "^0.35.7"
}
}
+3 -3
View File
@@ -30,9 +30,9 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-storage#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@polkadot/keyring": "^0.35.6",
"@polkadot/keyring": "^0.35.7",
"@polkadot/types": "^0.46.8",
"@polkadot/util": "^0.35.6",
"@polkadot/util-crypto": "^0.35.6"
"@polkadot/util": "^0.35.7",
"@polkadot/util-crypto": "^0.35.7"
}
}
+2 -2
View File
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@polkadot/keyring": "^0.35.6",
"@polkadot/util": "^0.35.6",
"@polkadot/keyring": "^0.35.7",
"@polkadot/util": "^0.35.7",
"core-js": "^2.6.5"
}
}