Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
536 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); | ||
}; |
Oops, something went wrong.