Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 9 additions & 98 deletions packages/light.js/docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

A high-level reactive JS library optimized for light clients.

[![Build Status](https://travis-ci.org/paritytech/js-libs.svg?branch=master)](https://travis-ci.org/paritytech/js-libs) [![npm (scoped)](https://img.shields.io/npm/v/@parity/light.js.svg)](https://www.npmjs.com/package/@parity/light.js) [![dependencies Status](https://david-dm.org/paritytech/js-libs/status.svg?path=packages/light.js)](https://david-dm.org/paritytech/js-libs?path=packages/light.js)
[![Build Status](https://travis-ci.org/paritytech/js-libs.svg?branch=master)](https://travis-ci.org/paritytech/js-libs) [![npm (scoped)](https://img.shields.io/npm/v/@parity/light.js.svg)](https://www.npmjs.com/package/@parity/light.js) [![npm](https://img.shields.io/npm/dw/@parity/light.js.svg)](https://www.npmjs.com/package/@parity/light.js) [![dependencies Status](https://david-dm.org/paritytech/js-libs/status.svg?path=packages/light.js)](https://david-dm.org/paritytech/js-libs?path=packages/light.js)

Getting Started
---------------
Expand All @@ -19,7 +19,9 @@ Usage
Reactively observe JSONRPC methods:

```javascript
import { defaultAccount$ } from '@parity/light.js';
import light, { defaultAccount$ } from '@parity/light.js';

light.setProvider(/* put your ethereum provider here */);

defaultAccount$().subscribe(publicAddress => console.log(publicAddress));
// Outputs your public address 0x...
Expand Down Expand Up @@ -66,23 +68,23 @@ defaultAccount$()
.subscribe(console.log); // Will log the result, and everytime the result changes
```

All available methods are documented [in the docs](https://parity-js.github.io/light.js/).
All available methods are documented [in the docs](TODO).

Usage with React
----------------

The libray provides a higher-order component to use these Observables easily with React apps.

```javascript
import light from '???'; // ??? to be decided
import { syncing$ } from '@parity/light.js';
import light from 'parity/ligth.js-react';
import { syncStatus$ } from '@parity/light.js';

@light({
syncingVariable: syncing$
mySyncVariable: syncStatus$
})
class MyClass extends React.Component {
render() {
return <div>{JSON.stringify(this.props.syncingVariable)}</div>;
return <div>{JSON.stringify(this.props.mySyncVariable)}</div>;
}
}
```
Expand Down Expand Up @@ -154,99 +156,8 @@ The keys are the Observables you are using in your dapp, each containing an obje

This output can of course be different on different pages of your dapp, if they use different Observables.

Notes about Implementation
--------------------------

### Observables are cold

The underlying JSONRPC method is only called if there's at least one subscriber.

```javascript
import { balanceOf$ } from '@parity/light.js';

const myObs$ = balanceOf$('0x123');
// Observable created, but `eth_getBalance` not called yet
const subscription = myObs$.subscribe(console.log);
// `eth_getBalance` called for the 1st time

// Some other code...

subscription.unsubscribe();
// `eth_getBalance` stops being called
```

### Observables are PublishReplay(1)

Let's take `blockNumber()$` which fires blocks 7, 8 and 9, and has 3 subscribers that don't subscribe at the same time.

We have the following marble diagram (`^` denotes when the subscriber subscribes).

```
blockNumber$(): -----7----------8------9-----|
subscriber1: -^---7----------8------9-----|
subscriber2: ------------^7--8------9-----|
subscriber3: --------------------------^9-|
```

Note: the default behavior for Observables is without PublishReplay, i.e.

```
blockNumber$(): -----7----------8------9-----|
subscriber1: -^---7----------8------9-----|
subscriber2: ------------^---8------9-----|
subscriber3: --------------------------^--|
```

But Observables in this library are PublishReplay(1). [Read more](https://blog.angularindepth.com/rxjs-how-to-use-refcount-73a0c6619a4e) about PublishReplay.

### Observables are memoized

```javascript
const obs1$ = balanceOf$('0x123');
const obs2$ = balanceOf$('0x123');
console.log(obs1$ === obs2$); // true

const obs3$ = balanceOf$('0x456');
console.log(obs1$ === obs3$); // false
```

### Underlying API calls are not unnessarily repeated

```javascript
const obs1$ = balanceOf$('0x123');
const obs2$ = balanceOf$('0x123');

obs1$.subscribe(console.log);
obs1$.subscribe(console.log);
obs2$.subscribe(console.log);
// Logs 3 times the balance
// But only one call to `eth_getBalance` has been made

const obs3$ = balanceOf$('0x456');
// Logs a new balance, another call to `eth_getBalance` is made
```

### Underlying PubSub subscriptions are dropped when there's no subscriber

```javascript
import { blockNumber$ } from '@parity/light.js';

const myObs$ = blockNumber$();
console.log(blockNumber$.frequency); // [onEveryBlock$]
// Note: onEveryBlock$ creates a pubsub on `eth_blockNumber`

const subscription = myObs$.subscribe(console.log);
// Creates a pubsub subscription

// Some other code...

subscription.unsubscribe();
// Drops the pubsub subscription
```

TODO
----

* Switch to TypeScript.
* Have 100% test coverage.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ___

**● metadata**: *`object`*

*Defined in [types.ts:38](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L38)*
*Defined in [types.ts:38](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L38)*

#### Type declaration

Expand Down
14 changes: 7 additions & 7 deletions packages/light.js/docs/api/interfaces/_types_.metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

**● calledWithArgs**: *`object`*

*Defined in [types.ts:27](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L27)*
*Defined in [types.ts:27](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L27)*

#### Type declaration

[key: `string`]: `any`
[key: `string`]: `ReplaySubject`<`Out`>

___
<a id="calls"></a>
Expand All @@ -28,7 +28,7 @@ ___

**● calls**: *`string`[]*

*Defined in [types.ts:30](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L30)*
*Defined in [types.ts:30](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L30)*

___
<a id="dependson"></a>
Expand All @@ -37,7 +37,7 @@ ___

**● dependsOn**: *[RpcObservable](_types_.rpcobservable.md)<`any`, `Source`>*

*Defined in [types.ts:31](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L31)*
*Defined in [types.ts:31](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L31)*

___
<a id="frequency"></a>
Expand All @@ -46,7 +46,7 @@ ___

**● frequency**: *[FrequencyObservable](_types_.frequencyobservable.md)<`Source`>[]*

*Defined in [types.ts:32](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L32)*
*Defined in [types.ts:32](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L32)*

___
<a id="name"></a>
Expand All @@ -55,7 +55,7 @@ ___

**● name**: *`string`*

*Defined in [types.ts:33](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L33)*
*Defined in [types.ts:33](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L33)*

___
<a id="pipes"></a>
Expand All @@ -64,7 +64,7 @@ ___

**● pipes**: *`function`*

*Defined in [types.ts:34](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L34)*
*Defined in [types.ts:34](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L34)*

#### Type declaration
▸(...args: *`any`[]*): `OperatorFunction`<`Source`, `Out`>[]
Expand Down
10 changes: 5 additions & 5 deletions packages/light.js/docs/api/interfaces/_types_.rpcobservable.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Callable
▸ **__call**(...args: *`any`[]*): `Observable`<`Out`>

*Defined in [types.ts:41](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L41)*
*Defined in [types.ts:41](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L41)*

**Parameters:**

Expand All @@ -28,7 +28,7 @@

**● metadata**: *[Metadata](_types_.metadata.md)<`Source`, `Out`>*

*Defined in [types.ts:43](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L43)*
*Defined in [types.ts:43](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L43)*

___

Expand All @@ -38,15 +38,15 @@ ___

## `<Optional>` setFrequency

▸ **setFrequency**(frequency: *`Observable`<[ApiValue](../modules/_types_.md#apivalue)>[]*): `void`
▸ **setFrequency**(frequency: *[FrequencyObservable](_types_.frequencyobservable.md)<`Source`>[]*): `void`

*Defined in [types.ts:44](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L44)*
*Defined in [types.ts:44](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L44)*

**Parameters:**

| Param | Type |
| ------ | ------ |
| frequency | `Observable`<[ApiValue](../modules/_types_.md#apivalue)>[] |
| frequency | [FrequencyObservable](_types_.frequencyobservable.md)<`Source`>[] |

**Returns:** `void`

Expand Down
14 changes: 7 additions & 7 deletions packages/light.js/docs/api/interfaces/_types_.txstatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

**● confirmed**: *`any`*

*Defined in [types.ts:55](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L55)*
*Defined in [types.ts:55](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L55)*

___
<a id="estimated"></a>
Expand All @@ -21,7 +21,7 @@ ___

**● estimated**: *`BigNumber`*

*Defined in [types.ts:57](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L57)*
*Defined in [types.ts:57](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L57)*

___
<a id="estimating"></a>
Expand All @@ -30,7 +30,7 @@ ___

**● estimating**: *`boolean`*

*Defined in [types.ts:56](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L56)*
*Defined in [types.ts:56](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L56)*

___
<a id="failed"></a>
Expand All @@ -39,7 +39,7 @@ ___

**● failed**: *`Error`*

*Defined in [types.ts:58](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L58)*
*Defined in [types.ts:58](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L58)*

___
<a id="requested"></a>
Expand All @@ -48,7 +48,7 @@ ___

**● requested**: *`string`*

*Defined in [types.ts:59](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L59)*
*Defined in [types.ts:59](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L59)*

___
<a id="schedule"></a>
Expand All @@ -57,7 +57,7 @@ ___

**● schedule**: *`any`*

*Defined in [types.ts:60](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L60)*
*Defined in [types.ts:60](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L60)*

___
<a id="signed"></a>
Expand All @@ -66,7 +66,7 @@ ___

**● signed**: *`string`*

*Defined in [types.ts:61](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/types.ts#L61)*
*Defined in [types.ts:61](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/types.ts#L61)*

___

27 changes: 23 additions & 4 deletions packages/light.js/docs/api/modules/_api_.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

▸ **getApi**(): `any`

*Defined in [api.ts:48](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/api.ts#L48)*
*Defined in [api.ts:62](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/api.ts#L62)*

We only ever use api() at call-time of functions; this allows the options (particularly the transport option) to be changed dynamically and the data structure to be reused.

Expand All @@ -22,15 +22,34 @@ ___

▸ **setApi**(newApi: *`any`*): `void`

*Defined in [api.ts:32](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/api.ts#L32)*
*Defined in [api.ts:32](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/api.ts#L32)*

Sets an Api object.
Sets a new Api object.

**Parameters:**

| Param | Type | Description |
| ------ | ------ | ------ |
| newApi | `any` | The Api object. |
| newApi | `any` | An Api object. |

**Returns:** `void`

___
<a id="setprovider"></a>

## `<Const>` setProvider

▸ **setProvider**(provider: *`any`*): `void`

*Defined in [api.ts:46](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/api.ts#L46)*

Sets a new Ethereum provider object.

**Parameters:**

| Param | Type | Description |
| ------ | ------ | ------ |
| provider | `any` | An Ethereum provider object. |

**Returns:** `void`

Expand Down
4 changes: 2 additions & 2 deletions packages/light.js/docs/api/modules/_frequency_accounts_.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
api
)

*Defined in [frequency/accounts.ts:13](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/frequency/accounts.ts#L13)*
*Defined in [frequency/accounts.ts:13](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/frequency/accounts.ts#L13)*

Observable that emits each time the default account changes

Expand All @@ -25,7 +25,7 @@ ___
api
)

*Defined in [frequency/accounts.ts:22](https://github.com/paritytech/js-libs/blob/a8a861f/packages/light.js/src/frequency/accounts.ts#L22)*
*Defined in [frequency/accounts.ts:22](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/frequency/accounts.ts#L22)*

Observable that emits each time the default account changes

Expand Down
Loading