Skip to content

Commit

Permalink
Move Rx API wrapper into api
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed May 7, 2018
1 parent 2c6a853 commit 793cb5f
Show file tree
Hide file tree
Showing 20 changed files with 536 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .flowconfig
Expand Up @@ -12,4 +12,5 @@ all=warn

[options]
include_warnings=true
module.name_mapper='^@polkadot/api-\(format\|jsonrpc\|provider\)\(.*\)$' -> '<PROJECT_ROOT>/packages/api-\1/src\2'
module.name_mapper='^@polkadot/api-\(format\|jsonrpc\|provider\|rx\)\(.*\)$' -> '<PROJECT_ROOT>/packages/api-\1/src\2'
module.name_mapper='^@polkadot/api\(.*\)$' -> '<PROJECT_ROOT>/packages/api/src\1'
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -15,7 +15,8 @@ This library provides a clean wrapper around all the methods exposed by a Polkad

The API is split up into a number of internal packages -

- [@polkadot/api](packages/api/) The API library
- [@polkadot/api](packages/api/) The low-level base API library
- [@polkadot/api-rx](packages/api-rx/) A RxJs Observable wrapper around the API
- [@polkadot/api-format](packages/api-format/) Input and output formatters
- [@polkadot/api-jsonrpc](packages/api-jsonrpc/) Interface definitions for RPC
- [@polkadot/api-provider](packages/api-provider/) Providers for connecting
3 changes: 2 additions & 1 deletion jest.config.js
Expand Up @@ -2,6 +2,7 @@ const config = require('@polkadot/dev/config/jest');

module.exports = Object.assign({}, config, {
moduleNameMapper: {
'@polkadot/api-(format|jsonrpc|provider)(.*)$': '<rootDir>/packages/api-$1/src/$2'
'@polkadot/api-(format|jsonrpc|provider|rx)(.*)$': '<rootDir>/packages/api-$1/src/$2',
'@polkadot/api(.*)$': '<rootDir>/packages/api/src/$1'
}
});
15 changes: 15 additions & 0 deletions packages/api-rx/LICENSE
@@ -0,0 +1,15 @@
ISC License (ISC)

Copyright 2017-2018 Jaco Greeff <jacogr@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
13 changes: 13 additions & 0 deletions packages/api-rx/README.md
@@ -0,0 +1,13 @@
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org)
![isc](https://img.shields.io/badge/license-ISC-lightgrey.svg?style=flat-square)
[![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![npm](https://img.shields.io/npm/v/@polkadot/api-rx.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/api-rx)
[![travis](https://img.shields.io/travis/polkadot-js/api.svg?style=flat-square)](https://travis-ci.org/polkadot-js/api)
[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api/maintainability)
[![coverage](https://img.shields.io/coveralls/polkadot-js/api.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/api?branch=master)
[![dependency](https://david-dm.org/polkadot-js/api.svg?style=flat-square&path=packages/api-rx)](https://david-dm.org/polkadot-js/api?path=packages/api-rx)
[![devDependency](https://david-dm.org/polkadot-js/api/dev-status.svg?style=flat-square&path=packages/api-rx)](https://david-dm.org/polkadot-js/api?path=packages/api-rx#info=devDependencies)

# @polkadot/api-rx

An RxJs wrapper around the [@polkadot/api](../api).
37 changes: 37 additions & 0 deletions packages/api-rx/package.json
@@ -0,0 +1,37 @@
{
"name": "@polkadot/api-rx",
"version": "0.6.1",
"description": "An RxJs wrapper around the Polkadot JS API",
"main": "index.js",
"keywords": [
"Polkadot",
"RxJs"
],
"author": "Jaco Greeff <jacogr@gmail.com>",
"license": "ISC",
"engines": {
"node": ">=8.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/polkadot-js/api.git"
},
"bugs": {
"url": "https://github.com/polkadot-js/api/issues"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-rx#readme",
"scripts": {
"build": "polkadot-dev-build-babel",
"check": "eslint src && flow check",
"test": "echo \"Tests only available from root wrapper\""
},
"dependencies": {
"@polkadot/api": "^0.9.16",
"@polkadot/api-provider": "^0.9.16",
"rxjs": "^5.5.10"
}
}
17 changes: 17 additions & 0 deletions packages/api-rx/src/api/connected.js
@@ -0,0 +1,17 @@
// 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 } from '@polkadot/api-provider/types';

const { BehaviorSubject } = require('rxjs/BehaviorSubject');

module.exports = function connected (provider: ProviderInterface): rxjs$BehaviorSubject<boolean> {
const subject = new BehaviorSubject(provider.isConnected());

provider.on('connected', () => subject.next(true));
provider.on('disconnected', () => subject.next(false));

return subject;
};
18 changes: 18 additions & 0 deletions packages/api-rx/src/api/index.js
@@ -0,0 +1,18 @@
// 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 } from '@polkadot/api-provider/types';
import type { RxApiInterface } from '../types';

const createConnected = require('./connected');

module.exports = function exposed (provider: ProviderInterface): RxApiInterface {
const connected = createConnected(provider);

return {
isConnected: (): rxjs$BehaviorSubject<boolean> =>
connected
};
};
10 changes: 10 additions & 0 deletions packages/api-rx/src/defaults.js
@@ -0,0 +1,10 @@
// 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

const WS_URL = 'ws://127.0.0.1:9944';

module.exports = {
WS_URL
};
29 changes: 29 additions & 0 deletions packages/api-rx/src/index.js
@@ -0,0 +1,29 @@
// 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 } from '@polkadot/api-provider/types';
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { RxApiInterface } from './types';

const createApi = require('@polkadot/api');
const interfaces = require('@polkadot/api-jsonrpc');
const createWs = require('@polkadot/api-provider/ws');

const createExposed = require('./api');
const defaults = require('./defaults');
const createInterface = require('./interface');

module.exports = function rxApi (provider?: ProviderInterface = createWs(defaults.WS_URL)): RxApiInterface {
const api = createApi(provider);
const exposed = createExposed(provider);

return Object
.keys(interfaces)
.reduce((result, type: InterfaceTypes) => {
result[type] = createInterface(api, type);

return result;
}, exposed);
};
22 changes: 22 additions & 0 deletions packages/api-rx/src/index.spec.js
@@ -0,0 +1,22 @@
// 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.

jest.mock('@polkadot/api-provider/ws', () => () => ({
isConnected: () => true,
on: () => true,
send: () => true
}));
jest.mock('./interface', () => (api, sectionName) => sectionName);

const createApi = require('./index');

describe('createApi', () => {
it('creates an instance with all sections', () => {
expect(
Object.keys(createApi())
).toEqual([
'isConnected', 'author', 'chain', 'state'
]);
});
});
23 changes: 23 additions & 0 deletions packages/api-rx/src/interface.js
@@ -0,0 +1,23 @@
// 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 { ApiInterface, ApiInterface$Section } from '@polkadot/api/types';
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { RxApiInterface$Section } from './types';

const observable = require('./observable');

module.exports = function createInterface (api: ApiInterface, sectionName: InterfaceTypes): RxApiInterface$Section {
const section: ApiInterface$Section = api[sectionName];

return Object
.keys(section)
.filter((name) => !['subscribe', 'unsubscribe'].includes(name))
.reduce((observables, name) => {
observables[name] = observable(`${sectionName}_${name}`, name, section);

return observables;
}, ({}: $Shape<RxApiInterface$Section>));
};
37 changes: 37 additions & 0 deletions packages/api-rx/src/interface.spec.js
@@ -0,0 +1,37 @@
// 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.

jest.mock('./observable', () => (subName, name, section) => `${subName}_${name}_${Object.keys(section).join(':')}`);

const createInterface = require('./interface');

describe('createInterface', () => {
let api;
let int;

beforeEach(() => {
api = {
chain: {
foo: 'test',
bar: 'test',
subscribe: 'noInclude',
unsubscribe: 'noInclude'
},
state: {
baz: 'test'
}
};

int = createInterface(api, 'chain');
});

it('creates observables for all relevant methods', () => {
expect(
int
).toEqual({
foo: 'chain_foo_foo_foo:bar:subscribe:unsubscribe',
bar: 'chain_bar_bar_foo:bar:subscribe:unsubscribe'
});
});
});
35 changes: 35 additions & 0 deletions packages/api-rx/src/observable/cached.js
@@ -0,0 +1,35 @@
// 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 { ApiInterface$Section } from '@polkadot/api/types';

type Cached$Name = string;
type Cached$ParamJson = string;

type CachedMap = {
[Cached$Name]: {
[Cached$ParamJson]: rxjs$BehaviorSubject<*>
}
};

const subject = require('./subject');

const cacheMap: CachedMap = {};

module.exports = function cached (subName: string, name: string, section: ApiInterface$Section): (...params: Array<mixed>) => rxjs$BehaviorSubject<*> {
if (!cacheMap[subName]) {
cacheMap[subName] = {};
}

return (...params: Array<mixed>): rxjs$BehaviorSubject<*> => {
const paramStr = JSON.stringify(params);

if (!cacheMap[subName][paramStr]) {
cacheMap[subName][paramStr] = subject(name, params, section);
}

return cacheMap[subName][paramStr];
};
};
80 changes: 80 additions & 0 deletions packages/api-rx/src/observable/cached.spec.js
@@ -0,0 +1,80 @@
// 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.

const cachedSubscription = require('./cached');

describe('cached', () => {
let creator;
let section;

beforeEach(() => {
const subMethod = jest.fn((name, ...params) => {
return Promise.resolve(12345);
});

subMethod.unsubscribe = jest.fn(() => {
return Promise.resolve(true);
});

const resMethod = jest.fn((name, ...params) => {
return Promise.resolve(12345);
});

section = {
resMethod,
subMethod
};

creator = cachedSubscription('test', 'subMethod', section);
});

it('creates a single observable', () => {
creator(123).subscribe((value) => {});

expect(
section.subMethod
).toHaveBeenCalledWith(123, expect.anything());
});

it('creates a single observable (multiple calls)', () => {
const observable1 = creator(123);

observable1.subscribe((value) => {});

const observable2 = creator(123);

observable2.subscribe((value) => {});

expect(
observable2
).toEqual(observable1);
});

it('creates multiple observers for different values', () => {
const observable1 = creator(123);

observable1.subscribe((value) => {});

const observable2 = creator(456);

observable2.subscribe((value) => {});

expect(
observable2
).not.toEqual(observable1);
});

it('functions as an subject', (done) => {
const subject = creator(123);

subject.subscribe((value) => {
if (value) {
expect(value).toEqual('test');
done();
}
});

subject.next('test');
});
});
22 changes: 22 additions & 0 deletions packages/api-rx/src/observable/index.js
@@ -0,0 +1,22 @@
// 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 { ApiInterface$Section } from '@polkadot/api/types';

const { fromPromise } = require('rxjs/observable/fromPromise');
const isFunction = require('@polkadot/util/is/function');

const cached = require('./cached');

module.exports = function observable (subName: string, name: string, section: ApiInterface$Section): (...params: Array<mixed>) => rxjs$Observable<*> | rxjs$BehaviorSubject<*> {
if (isFunction(section[name].unsubscribe)) {
return cached(subName, name, section);
}

return (...params: Array<mixed>): rxjs$Observable<*> =>
fromPromise(
section[name].apply(null, params)
);
};

0 comments on commit 793cb5f

Please sign in to comment.