Api & RxApi now created via new (#153)

* Api & RxApi now created via new

* Bump version
This commit is contained in:
Jaco Greeff
2018-09-14 17:32:58 +02:00
committed by GitHub
parent d9ab009917
commit 35b4f9a095
23 changed files with 106 additions and 102 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-rx",
"version": "0.28.35",
"version": "0.29.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/api-rx#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/api": "^0.28.35",
"@polkadot/api-provider": "^0.28.35",
"@polkadot/api": "^0.29.0",
"@polkadot/api-provider": "^0.29.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.2"
}
-16
View File
@@ -1,16 +0,0 @@
// Copyright 2017-2018 @polkadot/api-rx authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface } from '@polkadot/api-provider/types';
import { BehaviorSubject } from 'rxjs';
export default function connected (provider: ProviderInterface): BehaviorSubject<boolean> {
const subject = new BehaviorSubject(provider.isConnected());
provider.on('connected', () => subject.next(true));
provider.on('disconnected', () => subject.next(false));
return subject;
}
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2017-2018 @polkadot/api-rx authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface } from '@polkadot/api-provider/types';
import { RxApiInterface } from '../types';
import { BehaviorSubject } from 'rxjs';
import createConnected from './connected';
export default function exposed (provider: ProviderInterface): RxApiInterface {
const connected = createConnected(provider);
// @ts-ignore as in types.d.ts, not sure what to do here
return {
isConnected: (): BehaviorSubject<boolean> =>
connected
};
}
+22 -4
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import isFunction from '@polkadot/util/is/function';
jest.mock('@polkadot/api-provider/ws', () => class {
isConnected = () => true;
on = () => true;
@@ -9,14 +11,30 @@ jest.mock('@polkadot/api-provider/ws', () => class {
});
jest.mock('./interface', () => (api, sectionName) => sectionName);
const createApi = require('./index').default;
const Api = require('./index').default;
describe('Api', () => {
let api;
beforeEach(() => {
api = new Api();
});
describe('createApi', () => {
it('creates an instance with all sections', () => {
expect(
Object.keys(createApi())
Object
.keys(api)
.filter((key) =>
!key.startsWith('_')
)
).toEqual([
'isConnected', 'author', 'chain', 'state', 'system'
'author', 'chain', 'state', 'system'
]);
});
it('has isConnected', () => {
expect(
isFunction(api.isConnected)
).toBe(true);
});
});
+26 -11
View File
@@ -2,24 +2,39 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Interface$Sections } from '@polkadot/jsonrpc/types';
import { ApiInterface } from '@polkadot/api/types';
import { ProviderInterface } from '@polkadot/api-provider/types';
import { RxApiInterface } from './types';
import { RxApiInterface, RxApiInterface$Section } from './types';
import createApi from '@polkadot/api/index';
import interfaces from '@polkadot/jsonrpc/index';
import { BehaviorSubject } from 'rxjs';
import Api from '@polkadot/api/index';
import Ws from '@polkadot/api-provider/ws';
import createExposed from './api';
import defaults from './defaults';
import createInterface from './interface';
export default function rxApi (provider: ProviderInterface = new Ws(defaults.WS_URL)): RxApiInterface {
const api = createApi(provider);
export default class RxApi implements RxApiInterface {
private _api: ApiInterface;
private _isConnected: BehaviorSubject<boolean>;
readonly author: RxApiInterface$Section;
readonly chain: RxApiInterface$Section;
readonly state: RxApiInterface$Section;
readonly system: RxApiInterface$Section;
return Object.keys(interfaces).reduce((result, type) => {
result[type as Interface$Sections] = createInterface(api, type as Interface$Sections);
constructor (provider: ProviderInterface = new Ws(defaults.WS_URL)) {
this._api = new Api(provider);
this._isConnected = new BehaviorSubject(provider.isConnected());
return result;
}, createExposed(provider));
provider.on('connected', () => this._isConnected.next(true));
provider.on('disconnected', () => this._isConnected.next(false));
this.author = createInterface(this._api, 'author');
this.chain = createInterface(this._api, 'chain');
this.state = createInterface(this._api, 'state');
this.system = createInterface(this._api, 'system');
}
isConnected (): BehaviorSubject<boolean> {
return this._isConnected;
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ export type RxApiInterface$Section = {
};
type RxApiInterface$Keys = {
[key in keyof Interfaces]: RxApiInterface$Section;
readonly [key in keyof Interfaces]: RxApiInterface$Section;
};
export type RxApiInterface = RxApiInterface$Keys & {