Allow handling of RPC errors, promise resolve/reject (#572)

* Allow handling of RPC errors, promise resolve/reject

* Revert doing too much, focus on api-promise only

* tap instead of map

* Bump version, CHANGELOG
This commit is contained in:
Jaco Greeff
2019-01-11 16:46:01 +01:00
committed by GitHub
parent 4352f73c4c
commit 54eada5d87
14 changed files with 74 additions and 56 deletions
+4
View File
@@ -1,3 +1,7 @@
# 0.39.1
- The Promise API now returns a `Promise<UnsubFunction>` instead of `UnsubFunction` when making subscriptions.
# 0.38.1
Substrate has been updated with a breaking new transaction format where the Index/Nonce is now encoded as a Compact. This change is being rolled out to both Alexander (Polkadot testnet) as well as Charred Cherry (Substrate testnet) - transactions between old and new are not compatible.
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.38.16"
"version": "0.39.0"
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-derive",
"version": "0.38.16",
"version": "0.39.0",
"description": "Common functions used across Polkadot, derived from RPC calls and storage queries.",
"main": "index.js",
"keywords": [
@@ -31,8 +31,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/api": "^0.38.16",
"@polkadot/types": "^0.38.16",
"@polkadot/api": "^0.39.0",
"@polkadot/types": "^0.39.0",
"@types/memoizee": "^0.4.2",
"@types/rx": "^4.1.1",
"memoizee": "^0.4.14",
+7 -7
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "0.38.16",
"version": "0.39.0",
"description": "An RxJS wrapper around the Polkadot JS RPC",
"main": "index.js",
"keywords": [
@@ -30,12 +30,12 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/api-derive": "^0.38.16",
"@polkadot/extrinsics": "^0.38.16",
"@polkadot/rpc-provider": "^0.38.16",
"@polkadot/rpc-rx": "^0.38.16",
"@polkadot/storage": "^0.38.16",
"@polkadot/types": "^0.38.16",
"@polkadot/api-derive": "^0.39.0",
"@polkadot/extrinsics": "^0.39.0",
"@polkadot/rpc-provider": "^0.39.0",
"@polkadot/rpc-rx": "^0.39.0",
"@polkadot/storage": "^0.39.0",
"@polkadot/types": "^0.39.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.3"
}
@@ -41,7 +41,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
};
}
send (statusCb?: (result: SubmittableSendResult) => any): Promise<Hash> | UnsubFunction {
send (statusCb?: (result: SubmittableSendResult) => any): Promise<Hash | UnsubFunction> {
if (!statusCb || !this._api.hasSubscriptions) {
return this._api.rpc.author.submitExtrinsic(this);
}
@@ -55,24 +55,13 @@ export default class SubmittableExtrinsic extends Extrinsic {
return this;
}
signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableSendResult) => any): Promise<Hash> | UnsubFunction {
async signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableSendResult) => any): Promise<Hash | UnsubFunction> {
assert(this._api.hasSubscriptions, 'Api does not support subscriptions');
let unsubscribe: UnsubFunction | undefined;
const signerNonce = await this._api.query.system.accountNonce(signerPair.address());
this._api.query.system
.accountNonce(signerPair.address())
.then((nonce) => {
unsubscribe = this.sign(signerPair, nonce as Index).send(statusCb) as UnsubFunction;
})
.catch(console.error);
return (): void => {
if (unsubscribe) {
unsubscribe();
}
unsubscribe = undefined;
};
return this
.sign(signerPair, signerNonce as Index)
.send(statusCb);
}
}
+33 -8
View File
@@ -7,6 +7,8 @@ import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { ApiOptions } from '../types';
import { ApiPromiseInterface, DecoratedRpc, DecoratedRpc$Method, DecoratedRpc$Section, QueryableStorageFunction, QueryableModuleStorage, QueryableStorage, SubmittableExtrinsics, SubmittableModuleExtrinsics, SubmittableExtrinsicFunction, UnsubFunction } from './types';
import { EMPTY } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import Rpc from '@polkadot/rpc-core/index';
import RpcRx from '@polkadot/rpc-rx/index';
import { Storage } from '@polkadot/storage/types';
@@ -195,17 +197,40 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
rxfn(...params).toPromise();
}
return (..._params: Array<any>): UnsubFunction => {
return (..._params: Array<any>): Promise<UnsubFunction> => {
const cb = _params[_params.length - 1];
assert(isFunction(cb), 'Expected callback as last paramater for subscription');
const params = _params.slice(0, _params.length - 1);
const subscription = rxfn(...params).subscribe(cb);
return new Promise((resolve, reject) => {
let isCompleted = false;
const params = _params.slice(0, _params.length - 1);
const subscription = rxfn(...params)
.pipe(
// if we find an error (invalid params, etc), reject the promise
catchError((error) => {
console.error(error);
return (): void => {
subscription.unsubscribe();
};
if (!isCompleted) {
isCompleted = true;
reject(error);
}
// we don't want to continue, so empty observable it is
return EMPTY;
}),
// upon the first result, resolve the with the unsub function
tap(() => {
if (!isCompleted) {
isCompleted = true;
resolve(subscription.unsubscribe);
}
})
)
.subscribe(cb);
});
};
}
@@ -273,7 +298,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
}
private decorateStorageEntry (method: StorageFunction): QueryableStorageFunction {
const decorated: any = (...args: Array<any>): Promise<Codec | null | undefined> | UnsubFunction => {
const decorated: any = (...args: Array<any>): Promise<Codec | null | undefined> | Promise<UnsubFunction> => {
const cb = args[args.length - 1];
if (args.length === 0 || !isFunction(cb)) {
@@ -288,7 +313,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
[[method, args.length === 1 ? undefined : args[0]]],
(result: Array<Codec | null | undefined> = []) =>
cb(result[0])
) as UnsubFunction;
) as Promise<UnsubFunction>;
};
decorated.at = (hash: Hash, arg?: any): Promise<Codec | null | undefined> =>
+5 -5
View File
@@ -12,7 +12,7 @@ import SubmittableExtrinsic from './SubmittableExtrinsic';
export type UnsubFunction = () => void;
export type DecoratedRpc$Method = (...params: Array<any>) => Promise<any> | UnsubFunction;
export type DecoratedRpc$Method = (...params: Array<any>) => Promise<any> | Promise<UnsubFunction>;
export interface DecoratedRpc$Section {
[index: string]: DecoratedRpc$Method;
@@ -26,13 +26,13 @@ export interface DecoratedRpc {
}
export interface QueryableStorageFunction$Subscribe {
(arg: any, cb: (value?: any | null) => any): UnsubFunction;
(cb: (value?: any | null) => any): UnsubFunction;
(arg: any, cb: (value?: any | null) => any): Promise<UnsubFunction>;
(cb: (value?: any | null) => any): Promise<UnsubFunction>;
}
export interface QueryableStorageFunction extends StorageFunction {
(cb: (value?: any | null) => any): UnsubFunction;
(arg: any, cb: (value?: any | null) => any): UnsubFunction;
(cb: (value?: any | null) => any): Promise<UnsubFunction>;
(arg: any, cb: (value?: any | null) => any): Promise<UnsubFunction>;
(arg?: any): Promise<Codec | null | undefined>;
at: (hash: Hash, arg?: any) => Promise<Codec | null | undefined>;
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-core",
"version": "0.38.16",
"version": "0.39.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
@@ -30,9 +30,9 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-core#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/jsonrpc": "^0.38.16",
"@polkadot/rpc-provider": "^0.38.16",
"@polkadot/types": "^0.38.16",
"@polkadot/jsonrpc": "^0.39.0",
"@polkadot/rpc-provider": "^0.39.0",
"@polkadot/types": "^0.39.0",
"@polkadot/util": "^0.33.33"
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-provider",
"version": "0.38.16",
"version": "0.39.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
@@ -31,7 +31,7 @@
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/keyring": "^0.33.33",
"@polkadot/storage": "^0.38.16",
"@polkadot/storage": "^0.39.0",
"@polkadot/util": "^0.33.33",
"@polkadot/util-crypto": "^0.33.33",
"@types/nock": "^9.3.0",
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-rx",
"version": "0.38.16",
"version": "0.39.0",
"description": "An RxJs wrapper around the Polkadot JS API",
"main": "index.js",
"keywords": [
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-rx#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/rpc-core": "^0.38.16",
"@polkadot/rpc-provider": "^0.38.16",
"@polkadot/rpc-core": "^0.39.0",
"@polkadot/rpc-provider": "^0.39.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.3"
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/extrinsics",
"version": "0.38.16",
"version": "0.39.0",
"main": "index.js",
"repository": "https://github.com/polkadot-js/api.git",
"author": "Jaco Greeff <jacogr@gmail.com>",
@@ -11,7 +11,7 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/types": "^0.38.16",
"@polkadot/types": "^0.39.0",
"@polkadot/util": "^0.33.33"
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/jsonrpc",
"version": "0.38.16",
"version": "0.39.0",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/storage",
"version": "0.38.16",
"version": "0.39.0",
"description": "Generic wrapper wrapper around state storage",
"main": "index.js",
"engines": {
@@ -31,7 +31,7 @@
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/keyring": "^0.33.33",
"@polkadot/types": "^0.38.16",
"@polkadot/types": "^0.39.0",
"@polkadot/util": "^0.33.33",
"@polkadot/util-crypto": "^0.33.33"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/types",
"version": "0.38.16",
"version": "0.39.0",
"description": "Implementation of the Parity codec",
"main": "index.js",
"keywords": [