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"
}
}
+15 -15
View File
@@ -1429,14 +1429,14 @@
typescript "^3.3.3333"
vuepress "^1.0.0-alpha.40"
"@polkadot/keyring@^0.35.6":
version "0.35.6"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.35.6.tgz#6b8f5d8f9fadfa99b0171659d024b4cb04fa5bb7"
integrity sha512-/M7aQZX0HN2R+mryGoAu+xW3NufdnXZiPqTJ4kXaUo2zkOBseEA9duztwHP3PJaXSVklob3qRYM5YTK2zvnHpw==
"@polkadot/keyring@^0.35.7":
version "0.35.7"
resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.35.7.tgz#f1f9775ef473e2b0a41478499a71ef7f35e0b834"
integrity sha512-Rg1d1xCOVuxyruqFXJZefxJZZ2jmmpRUZq6NeCEtxrKPchfatcyxt5cEDZoZ9Kc7JZkmMal7FahLvPe2ArjdSA==
dependencies:
"@babel/runtime" "^7.3.4"
"@polkadot/util" "^0.35.6"
"@polkadot/util-crypto" "^0.35.6"
"@polkadot/util" "^0.35.7"
"@polkadot/util-crypto" "^0.35.7"
"@types/bs58" "^4.0.0"
bs58 "^4.0.1"
@@ -1450,14 +1450,14 @@
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
integrity sha512-wnt4zXxZXyz6WaubTO/I+nUElwV2DogFzdl6CrKfVn2PTWp8uHN06W9s40FH57ORtmQfDr9rLRP8Nq+oIyElbg==
"@polkadot/util-crypto@^0.35.6":
version "0.35.6"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.35.6.tgz#23e15da6a09d8201bbb1c4bbce7a37cbf7ba476a"
integrity sha512-LaT1KZ/BXVVKoZ0Yd0pj3Aqxs3pqwWh+0/uuCQyGT+a0a3cZ5QHcksWeCV1wjPGqNKEEhNUhNc6CXO99Qpt2wA==
"@polkadot/util-crypto@^0.35.7":
version "0.35.7"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.35.7.tgz#c7eae4a4bf3a033eb72128a10126227be7722d09"
integrity sha512-w44O9t41emL96yBy1670oHz3635R1OPsPA6SWeohL4CTYatzRVs5kn3dGGSAokMzuRbiaGTlWSCzQLbE6NJ2bw==
dependencies:
"@babel/runtime" "^7.3.4"
"@polkadot/schnorrkel-js" "^0.1.2-3"
"@polkadot/util" "^0.35.6"
"@polkadot/util" "^0.35.7"
"@types/bip39" "^2.4.2"
"@types/pbkdf2" "^3.0.0"
"@types/secp256k1" "^3.5.0"
@@ -1470,10 +1470,10 @@
tweetnacl "^1.0.1"
xxhashjs "^0.2.2"
"@polkadot/util@^0.35.6":
version "0.35.6"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.35.6.tgz#6f82a80ee33d79f8006c3ca5f36def840dfa700b"
integrity sha512-UkXftcPjx99QnP3L2hXcjmyejUm4wiSrzD3k4OM+iVkBTlg7DztQm2Eb9uTbCwii4i1SScudIogbjbOjeX1e7g==
"@polkadot/util@^0.35.7":
version "0.35.7"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.35.7.tgz#b560a38aba332792457b4b0799634bb818dc4d09"
integrity sha512-baKr2OawDhuAhlo/CkgNDfaz+r5z12T0Hk4+y85h7lV8HuG+5GV1u9YHP4mHoX95BweEfvfQuGES3ErIBlGyOg==
dependencies:
"@babel/runtime" "^7.3.4"
"@types/bn.js" "^4.11.4"