Provider emits connected/disconnected (#76)
* Add provider.on stub (mock/http) * Emit connected/disconnected from ws * Update dependencies * Expose .on on api
This commit is contained in:
+1
-1
@@ -10,5 +10,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "0.8.18"
|
||||
"version": "0.9.0"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-format",
|
||||
"version": "0.8.18",
|
||||
"version": "0.9.0",
|
||||
"description": "Input/Output formatters for JsonRPC exchange",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -30,7 +30,7 @@
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/api-jsonrpc": "^0.8.18"
|
||||
"@polkadot/api-jsonrpc": "^0.9.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/primitives-json": "^0.10.3",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-jsonrpc",
|
||||
"version": "0.8.18",
|
||||
"version": "0.9.0",
|
||||
"description": "Method definitions for the Polkadot RPC layer",
|
||||
"main": "index.js",
|
||||
"engines": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api-provider",
|
||||
"version": "0.8.18",
|
||||
"version": "0.9.0",
|
||||
"description": "Transport providers for the API",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -38,6 +38,7 @@
|
||||
"@polkadot/util-crypto": "^0.19.2",
|
||||
"@polkadot/util-keyring": "^0.19.2",
|
||||
"babel-runtime": "^6.26.0",
|
||||
"eventemitter3": "^2.0.3",
|
||||
"isomorphic-fetch": "^2.2.1",
|
||||
"websocket": "^1.0.25"
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface, ProviderInterface$Callback } from '../types';
|
||||
import type { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
|
||||
require('./polyfill');
|
||||
|
||||
const on = require('./on');
|
||||
const send = require('./send');
|
||||
const subscribe = require('./subscribe');
|
||||
const state = require('./state');
|
||||
@@ -18,6 +19,8 @@ module.exports = function httpProvider (endpoint: string): ProviderInterface {
|
||||
return {
|
||||
isConnected: (): boolean =>
|
||||
true,
|
||||
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
|
||||
on(self, type, sub),
|
||||
send: (method: string, params: Array<mixed>): Promise<mixed> =>
|
||||
send(self, method, params),
|
||||
subscribe: (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number> =>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
import type { HttpState } from './types';
|
||||
|
||||
module.exports = function on (self: HttpState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
|
||||
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
|
||||
};
|
||||
@@ -3,9 +3,10 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface } from '../types';
|
||||
import type { ProviderInterface, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
|
||||
const mocks = require('./mocks');
|
||||
const on = require('./on');
|
||||
const send = require('./send');
|
||||
const state = require('./state');
|
||||
const subscribe = require('./subscribe');
|
||||
@@ -19,6 +20,8 @@ module.exports = function mockProvider (): ProviderInterface {
|
||||
return {
|
||||
isConnected: (): boolean =>
|
||||
true,
|
||||
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
|
||||
on(self, type, sub),
|
||||
send: (method: string, params: Array<mixed>): Promise<mixed> =>
|
||||
send(self, method, params),
|
||||
subscribe: (method: string, ...params: Array<mixed>): Promise<number> =>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
import type { MockState } from './types';
|
||||
|
||||
module.exports = function on (self: MockState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
|
||||
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
|
||||
};
|
||||
@@ -38,8 +38,14 @@ export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase;
|
||||
|
||||
export type ProviderInterface$Callback = (error: ?Error, result: mixed) => void;
|
||||
|
||||
export type ProviderInterface$Emitted = 'connected' | 'disconnected';
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
export type ProviderInterface$EmitCb = (value?: any) => any;
|
||||
|
||||
export type ProviderInterface = {
|
||||
isConnected (): boolean,
|
||||
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void,
|
||||
send (method: string, params: Array<mixed>): Promise<mixed>,
|
||||
subscribe (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number>,
|
||||
unsubscribe (method: string, id: number): Promise<boolean>
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface, ProviderInterface$Callback } from '../types';
|
||||
import type { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
|
||||
require('./polyfill');
|
||||
|
||||
const connect = require('./connect');
|
||||
const on = require('./on');
|
||||
const send = require('./send');
|
||||
const state = require('./state');
|
||||
const subscribe = require('./subscribe');
|
||||
@@ -25,6 +26,8 @@ module.exports = function wsProvider (endpoint: string, autoConnect: boolean = t
|
||||
connect(self),
|
||||
isConnected: (): boolean =>
|
||||
self.isConnected,
|
||||
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
|
||||
on(self, type, sub),
|
||||
send: (method: string, params: Array<mixed>): Promise<mixed> =>
|
||||
send(self, method, params),
|
||||
subscribe: (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number> =>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
|
||||
import type { WsState } from './types';
|
||||
|
||||
module.exports = function on (self: WsState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
|
||||
self.emitter.on(type, sub);
|
||||
};
|
||||
@@ -10,6 +10,7 @@ module.exports = function onClose (self: WsState): () => void {
|
||||
self.l.debug(() => ['disconnected from', self.endpoint]);
|
||||
|
||||
self.isConnected = false;
|
||||
self.emitter.emit('disconnected');
|
||||
|
||||
if (self.autoConnect) {
|
||||
setTimeout(() => {
|
||||
|
||||
@@ -10,6 +10,7 @@ module.exports = function onOpen (self: WsState): () => boolean {
|
||||
self.l.debug(() => ['connected to', self.endpoint]);
|
||||
|
||||
self.isConnected = true;
|
||||
self.emitter.emit('connected');
|
||||
|
||||
Object.keys(self.queued).forEach((id) => {
|
||||
try {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import type { WsState } from './types';
|
||||
|
||||
const EventEmitter = require('eventemitter3');
|
||||
const assert = require('@polkadot/util/assert');
|
||||
const l = require('@polkadot/util/logger')('api-ws');
|
||||
|
||||
@@ -16,6 +17,7 @@ module.exports = function state (endpoint: string, autoConnect: boolean): $Shape
|
||||
return {
|
||||
autoConnect,
|
||||
coder: coder(),
|
||||
emitter: new EventEmitter(),
|
||||
endpoint,
|
||||
handlers: {},
|
||||
isConnected: false,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type EventEmitter from 'eventemitter3';
|
||||
import type { Logger } from '@polkadot/util/types';
|
||||
import type { RpcCoder } from '../coder/json/types';
|
||||
import type { ProviderInterface$Callback } from '../types';
|
||||
@@ -20,6 +21,7 @@ export type WsState = {
|
||||
autoConnect: boolean,
|
||||
coder: RpcCoder,
|
||||
endpoint: string,
|
||||
emitter: EventEmitter,
|
||||
handlers: { [number]: WsState$Awaiting },
|
||||
isConnected: boolean,
|
||||
l: Logger,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/api",
|
||||
"version": "0.8.18",
|
||||
"version": "0.9.0",
|
||||
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -30,9 +30,9 @@
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api-format": "^0.8.18",
|
||||
"@polkadot/api-jsonrpc": "^0.8.18",
|
||||
"@polkadot/api-provider": "^0.8.18",
|
||||
"@polkadot/api-format": "^0.9.0",
|
||||
"@polkadot/api-jsonrpc": "^0.9.0",
|
||||
"@polkadot/api-provider": "^0.9.0",
|
||||
"@polkadot/util": "^0.19.2",
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
|
||||
@@ -16,11 +16,15 @@ const createInterface = require('./create/interface');
|
||||
module.exports = function api (provider: ProviderInterface): ApiInterface {
|
||||
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
|
||||
|
||||
const exposed: ApiInterface = {
|
||||
on: provider.on
|
||||
};
|
||||
|
||||
return Object
|
||||
.keys(interfaces)
|
||||
.reduce((result, type: InterfaceTypes) => {
|
||||
result[type] = createInterface(provider, type);
|
||||
|
||||
return result;
|
||||
}, ({}: $Shape<ApiInterface>));
|
||||
}, exposed);
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// @flow
|
||||
|
||||
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
|
||||
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '@polkadot/api-provider/types';
|
||||
|
||||
export type ApiInterface$Section$Method = {
|
||||
(...params: Array<mixed>): Promise<mixed>;
|
||||
@@ -15,5 +16,6 @@ export type ApiInterface$Section = {
|
||||
};
|
||||
|
||||
export type ApiInterface = {
|
||||
[InterfaceTypes]: ApiInterface$Section
|
||||
[InterfaceTypes]: ApiInterface$Section,
|
||||
on (type: ProviderInterface$Emitted, callback: ProviderInterface$EmitCb): void
|
||||
}
|
||||
|
||||
@@ -2244,6 +2244,10 @@ esutils@^2.0.2:
|
||||
version "2.0.2"
|
||||
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
|
||||
|
||||
eventemitter3@^2.0.3:
|
||||
version "2.0.3"
|
||||
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-2.0.3.tgz#b5e1079b59fb5e1ba2771c0a993be060a58c99ba"
|
||||
|
||||
exec-sh@^0.2.0:
|
||||
version "0.2.1"
|
||||
resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.2.1.tgz#163b98a6e89e6b65b47c2a28d215bc1f63989c38"
|
||||
|
||||
Reference in New Issue
Block a user