Api & RxApi now created via new (#153)
* Api & RxApi now created via new * Bump version
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -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 & {
|
||||
|
||||
Reference in New Issue
Block a user