Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit 45f7de7

Browse files
authored
Merge pull request #5 from paritytech/am-setapi
Add setApi and setProvider
2 parents c75381e + d02a6c8 commit 45f7de7

23 files changed

+102
-158
lines changed

packages/light.js/docs/api/README.md

Lines changed: 9 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
[![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)
7+
[![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)
88

99
Getting Started
1010
---------------
@@ -19,7 +19,9 @@ Usage
1919
Reactively observe JSONRPC methods:
2020

2121
```javascript
22-
import { defaultAccount$ } from '@parity/light.js';
22+
import light, { defaultAccount$ } from '@parity/light.js';
23+
24+
light.setProvider(/* put your ethereum provider here */);
2325

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

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

7173
Usage with React
7274
----------------
7375

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

7678
```javascript
77-
import light from '???'; // ??? to be decided
78-
import { syncing$ } from '@parity/light.js';
79+
import light from 'parity/ligth.js-react';
80+
import { syncStatus$ } from '@parity/light.js';
7981

8082
@light({
81-
syncingVariable: syncing$
83+
mySyncVariable: syncStatus$
8284
})
8385
class MyClass extends React.Component {
8486
render() {
85-
return <div>{JSON.stringify(this.props.syncingVariable)}</div>;
87+
return <div>{JSON.stringify(this.props.mySyncVariable)}</div>;
8688
}
8789
}
8890
```
@@ -154,99 +156,8 @@ The keys are the Observables you are using in your dapp, each containing an obje
154156

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

157-
Notes about Implementation
158-
--------------------------
159-
160-
### Observables are cold
161-
162-
The underlying JSONRPC method is only called if there's at least one subscriber.
163-
164-
```javascript
165-
import { balanceOf$ } from '@parity/light.js';
166-
167-
const myObs$ = balanceOf$('0x123');
168-
// Observable created, but `eth_getBalance` not called yet
169-
const subscription = myObs$.subscribe(console.log);
170-
// `eth_getBalance` called for the 1st time
171-
172-
// Some other code...
173-
174-
subscription.unsubscribe();
175-
// `eth_getBalance` stops being called
176-
```
177-
178-
### Observables are PublishReplay(1)
179-
180-
Let's take `blockNumber()$` which fires blocks 7, 8 and 9, and has 3 subscribers that don't subscribe at the same time.
181-
182-
We have the following marble diagram (`^` denotes when the subscriber subscribes).
183-
184-
```
185-
blockNumber$(): -----7----------8------9-----|
186-
subscriber1: -^---7----------8------9-----|
187-
subscriber2: ------------^7--8------9-----|
188-
subscriber3: --------------------------^9-|
189-
```
190-
191-
Note: the default behavior for Observables is without PublishReplay, i.e.
192-
193-
```
194-
blockNumber$(): -----7----------8------9-----|
195-
subscriber1: -^---7----------8------9-----|
196-
subscriber2: ------------^---8------9-----|
197-
subscriber3: --------------------------^--|
198-
```
199-
200-
But Observables in this library are PublishReplay(1). [Read more](https://blog.angularindepth.com/rxjs-how-to-use-refcount-73a0c6619a4e) about PublishReplay.
201-
202-
### Observables are memoized
203-
204-
```javascript
205-
const obs1$ = balanceOf$('0x123');
206-
const obs2$ = balanceOf$('0x123');
207-
console.log(obs1$ === obs2$); // true
208-
209-
const obs3$ = balanceOf$('0x456');
210-
console.log(obs1$ === obs3$); // false
211-
```
212-
213-
### Underlying API calls are not unnessarily repeated
214-
215-
```javascript
216-
const obs1$ = balanceOf$('0x123');
217-
const obs2$ = balanceOf$('0x123');
218-
219-
obs1$.subscribe(console.log);
220-
obs1$.subscribe(console.log);
221-
obs2$.subscribe(console.log);
222-
// Logs 3 times the balance
223-
// But only one call to `eth_getBalance` has been made
224-
225-
const obs3$ = balanceOf$('0x456');
226-
// Logs a new balance, another call to `eth_getBalance` is made
227-
```
228-
229-
### Underlying PubSub subscriptions are dropped when there's no subscriber
230-
231-
```javascript
232-
import { blockNumber$ } from '@parity/light.js';
233-
234-
const myObs$ = blockNumber$();
235-
console.log(blockNumber$.frequency); // [onEveryBlock$]
236-
// Note: onEveryBlock$ creates a pubsub on `eth_blockNumber`
237-
238-
const subscription = myObs$.subscribe(console.log);
239-
// Creates a pubsub subscription
240-
241-
// Some other code...
242-
243-
subscription.unsubscribe();
244-
// Drops the pubsub subscription
245-
```
246-
247159
TODO
248160
----
249161

250-
* Switch to TypeScript.
251162
* Have 100% test coverage.
252163

packages/light.js/docs/api/interfaces/_types_.frequencyobservable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ___
5757

5858
**● metadata**: *`object`*
5959

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

6262
#### Type declaration
6363

packages/light.js/docs/api/interfaces/_types_.metadata.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
**● calledWithArgs**: *`object`*
1717

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

2020
#### Type declaration
2121

22-
[key: `string`]: `any`
22+
[key: `string`]: `ReplaySubject`<`Out`>
2323

2424
___
2525
<a id="calls"></a>
@@ -28,7 +28,7 @@ ___
2828

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

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

3333
___
3434
<a id="dependson"></a>
@@ -37,7 +37,7 @@ ___
3737

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

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

4242
___
4343
<a id="frequency"></a>
@@ -46,7 +46,7 @@ ___
4646

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

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

5151
___
5252
<a id="name"></a>
@@ -55,7 +55,7 @@ ___
5555

5656
**● name**: *`string`*
5757

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

6060
___
6161
<a id="pipes"></a>
@@ -64,7 +64,7 @@ ___
6464

6565
**● pipes**: *`function`*
6666

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

6969
#### Type declaration
7070
▸(...args: *`any`[]*): `OperatorFunction`<`Source`, `Out`>[]

packages/light.js/docs/api/interfaces/_types_.rpcobservable.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Callable
1111
**__call**(...args: *`any`[]*): `Observable`<`Out`>
1212

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

1515
**Parameters:**
1616

@@ -28,7 +28,7 @@
2828

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

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

3333
___
3434

@@ -38,15 +38,15 @@ ___
3838

3939
## `<Optional>` setFrequency
4040

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

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

4545
**Parameters:**
4646

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

5151
**Returns:** `void`
5252

packages/light.js/docs/api/interfaces/_types_.txstatus.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
**● confirmed**: *`any`*
1414

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

1717
___
1818
<a id="estimated"></a>
@@ -21,7 +21,7 @@ ___
2121

2222
**● estimated**: *`BigNumber`*
2323

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

2626
___
2727
<a id="estimating"></a>
@@ -30,7 +30,7 @@ ___
3030

3131
**● estimating**: *`boolean`*
3232

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

3535
___
3636
<a id="failed"></a>
@@ -39,7 +39,7 @@ ___
3939

4040
**● failed**: *`Error`*
4141

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

4444
___
4545
<a id="requested"></a>
@@ -48,7 +48,7 @@ ___
4848

4949
**● requested**: *`string`*
5050

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

5353
___
5454
<a id="schedule"></a>
@@ -57,7 +57,7 @@ ___
5757

5858
**● schedule**: *`any`*
5959

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

6262
___
6363
<a id="signed"></a>
@@ -66,7 +66,7 @@ ___
6666

6767
**● signed**: *`string`*
6868

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

7171
___
7272

packages/light.js/docs/api/modules/_api_.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
**getApi**(): `any`
1010

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

1313
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.
1414

@@ -22,15 +22,34 @@ ___
2222

2323
**setApi**(newApi: *`any`*): `void`
2424

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

27-
Sets an Api object.
27+
Sets a new Api object.
2828

2929
**Parameters:**
3030

3131
| Param | Type | Description |
3232
| ------ | ------ | ------ |
33-
| newApi | `any` | The Api object. |
33+
| newApi | `any` | An Api object. |
34+
35+
**Returns:** `void`
36+
37+
___
38+
<a id="setprovider"></a>
39+
40+
## `<Const>` setProvider
41+
42+
**setProvider**(provider: *`any`*): `void`
43+
44+
*Defined in [api.ts:46](https://github.com/paritytech/js-libs/blob/c75381e/packages/light.js/src/api.ts#L46)*
45+
46+
Sets a new Ethereum provider object.
47+
48+
**Parameters:**
49+
50+
| Param | Type | Description |
51+
| ------ | ------ | ------ |
52+
| provider | `any` | An Ethereum provider object. |
3453

3554
**Returns:** `void`
3655

packages/light.js/docs/api/modules/_frequency_accounts_.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
api
1212
)
1313

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

1616
Observable that emits each time the default account changes
1717

@@ -25,7 +25,7 @@ ___
2525
api
2626
)
2727

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

3030
Observable that emits each time the default account changes
3131

0 commit comments

Comments
 (0)