@polkadot/rpc-core
Advanced tools
Comparing version 0.31.20 to 0.31.21
@@ -55,3 +55,3 @@ // Copyright 2017-2018 @polkadot/rpc-core authors & contributors | ||
api.state | ||
.storage( | ||
.subscribeStorage( | ||
[ | ||
@@ -58,0 +58,0 @@ [storage.balances.freeBalance, ADDR_ONE], |
24
index.js
@@ -10,2 +10,4 @@ "use strict"; | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
require("core-js/modules/web.dom.iterable"); | ||
@@ -105,5 +107,4 @@ | ||
return Object.keys(methods).reduce((exposed, method) => { | ||
const rpcName = `${section}_${method}`; | ||
const def = methods[method]; | ||
exposed[method] = def.isSubscription ? this.createMethodSubscribe(rpcName, def) : this.createMethodSend(rpcName, def); | ||
exposed[method] = def.isSubscription ? this.createMethodSubscribe(def) : this.createMethodSend(def); | ||
return exposed; | ||
@@ -113,3 +114,5 @@ }, {}); | ||
createMethodSend(rpcName, method) { | ||
createMethodSend(method) { | ||
const rpcName = `${method.section}_${method.method}`; | ||
const call = async (...values) => { | ||
@@ -132,5 +135,14 @@ // TODO Warn on deprecated methods | ||
createMethodSubscribe(rpcName, method) { | ||
const unsubscribe = subscriptionId => this._provider.unsubscribe(rpcName, method.subscribe[1], subscriptionId); | ||
createMethodSubscribe(method) { | ||
const _method$pubsub = (0, _slicedToArray2.default)(method.pubsub, 3), | ||
updateType = _method$pubsub[0], | ||
subMethod = _method$pubsub[1], | ||
unsubMethod = _method$pubsub[2]; | ||
const subName = `${method.section}_${subMethod}`; | ||
const unsubName = `${method.section}_${unsubMethod}`; | ||
const subscriptionType = `${method.section}_${updateType}`; | ||
const unsubscribe = subscriptionId => this._provider.unsubscribe(subscriptionType, unsubName, subscriptionId); | ||
const _call = async (...values) => { | ||
@@ -152,3 +164,3 @@ try { | ||
return this._provider.subscribe(rpcName, method.subscribe[0], paramsJson, update); | ||
return this._provider.subscribe(subscriptionType, subName, paramsJson, update); | ||
} catch (error) { | ||
@@ -155,0 +167,0 @@ const message = `${Rpc.signature(method)}:: ${error.message}`; |
21
index.ts
@@ -90,8 +90,8 @@ // Copyright 2017-2018 @polkadot/rpc-core authors & contributors | ||
.reduce((exposed, method) => { | ||
const rpcName = `${section}_${method}`; | ||
const def = methods[method]; | ||
exposed[method] = def.isSubscription | ||
? this.createMethodSubscribe(rpcName, def) | ||
: this.createMethodSend(rpcName, def); | ||
? this.createMethodSubscribe(def) | ||
: this.createMethodSend(def); | ||
@@ -102,3 +102,5 @@ return exposed; | ||
private createMethodSend (rpcName: string, method: RpcMethod): RpcInterface$Section$Method { | ||
private createMethodSend (method: RpcMethod): RpcInterface$Section$Method { | ||
const rpcName = `${method.section}_${method.method}`; | ||
const call = async (...values: Array<any>): Promise<any> => { | ||
@@ -124,5 +126,10 @@ // TODO Warn on deprecated methods | ||
private createMethodSubscribe (rpcName: string, method: RpcMethod): RpcInterface$Section$Method { | ||
private createMethodSubscribe (method: RpcMethod): RpcInterface$Section$Method { | ||
const [updateType, subMethod, unsubMethod] = method.pubsub; | ||
const subName = `${method.section}_${subMethod}`; | ||
const unsubName = `${method.section}_${unsubMethod}`; | ||
const subscriptionType = `${method.section}_${updateType}`; | ||
const unsubscribe = (subscriptionId: any): Promise<any> => | ||
this._provider.unsubscribe(rpcName, method.subscribe[1], subscriptionId); | ||
this._provider.unsubscribe(subscriptionType, unsubName, subscriptionId); | ||
const _call = async (...values: Array<any>): Promise<any> => { | ||
@@ -145,3 +152,3 @@ try { | ||
return this._provider.subscribe(rpcName, method.subscribe[0], paramsJson, update); | ||
return this._provider.subscribe(subscriptionType, subName, paramsJson, update); | ||
} catch (error) { | ||
@@ -148,0 +155,0 @@ const message = `${Rpc.signature(method)}:: ${error.message}`; |
@@ -19,2 +19,3 @@ // Copyright 2017-2018 @polkadot/rpc-core authors & contributors | ||
], | ||
section: 'test', | ||
type: 'Bytes' | ||
@@ -25,2 +26,3 @@ }, | ||
params: [], | ||
section: 'test', | ||
type: 'Bytes' | ||
@@ -40,3 +42,3 @@ } | ||
it('wraps errors with the call signature', () => { | ||
const method = api.createMethodSend('test_blah', methods.blah); | ||
const method = api.createMethodSend(methods.blah); | ||
@@ -49,3 +51,3 @@ return method().catch((error) => { | ||
it('checks for mismatched parameters', () => { | ||
const method = api.createMethodSend('test_bleh', methods.bleh); | ||
const method = api.createMethodSend(methods.bleh); | ||
@@ -58,3 +60,3 @@ return method(1).catch((error) => { | ||
it('calls the provider with the correct parameters', () => { | ||
const method = api.createMethodSend('test_blah', methods.blah); | ||
const method = api.createMethodSend(methods.blah); | ||
@@ -61,0 +63,0 @@ // Args are length-prefixed, because it's a Bytes |
{ | ||
"name": "@polkadot/rpc-core", | ||
"version": "0.31.20", | ||
"version": "0.31.21", | ||
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface", | ||
@@ -33,6 +33,6 @@ "main": "index.js", | ||
"@babel/runtime": "^7.0.0", | ||
"@polkadot/jsonrpc": "^0.31.20", | ||
"@polkadot/rpc-provider": "^0.31.20", | ||
"@polkadot/jsonrpc": "^0.31.21", | ||
"@polkadot/rpc-provider": "^0.31.21", | ||
"@polkadot/util": "^0.30.7" | ||
} | ||
} |
@@ -56,8 +56,4 @@ # @polkadot/rpc-core | ||
api.chain | ||
.newHead((error, header) => { | ||
if (error) { | ||
console.error('error:', error); | ||
} | ||
console.log(`best #${header.blockNumber.toString()}`); | ||
.subscribeNewHead((header) => { | ||
console.log(`best #${header.blockNumber}`); | ||
}) | ||
@@ -67,3 +63,3 @@ .then((subscriptionId) => { | ||
// id for the subscription, can unsubscribe via | ||
// api.chain.newHead.unsubscribe(subscriptionId); | ||
// api.chain.subscribeNewHead.unsubscribe(subscriptionId); | ||
}) | ||
@@ -70,0 +66,0 @@ .catch((error) => { |
26270
602
68