Skip to content

Commit 118bc57

Browse files
committedMar 9, 2020
Update docs to reflect latest subspace changes
1 parent 866619d commit 118bc57

File tree

9 files changed

+185
-95
lines changed

9 files changed

+185
-95
lines changed
 

‎packages/core/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ await subspace.init();
3737

3838
In addition to the web3 object, `Subspace` also accepts an `options` object with settings that can change its behavior:
3939
- `dbFilename` - Name of the database where the information will be stored (default `subspace.db`)
40-
- `callInterval` - Interval of time in milliseconds to query a contract/address to determine changes in state or balance (default: `undefined`. Obtain data every block).
40+
- `callInterval` - Interval of time in milliseconds to query a contract/address to determine changes in state or balance. It's only used with HttpProviders (default: `undefined`. Obtains data every block using the average block time as an interval).
4141
- `refreshLastNBlocks` - Ignores last N blocks (from current block), stored in the local db and refresh them via a web3 subscription. Useful for possible reorgs (default: `12`),
4242
- `disableSubscriptions` - Subspace by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an HttpProvider. This functionality can be disabled by passing true to this option. (default: `undefined`)
4343

‎packages/docs/api.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
## General
44

5-
### `new Subspace(web3Provider [, options])`
5+
### `new Subspace(web3 [, options])`
66
Constructor.
77

88
**Parameters**
9-
1. `web3Provider` - `Object`: a valid web3 provider.
9+
1. `web3` - `Object`: a `web3.js` object.
1010
2. `options` - `Object` (optional): Options used to initialize Subspace
1111
- `dbFilename` - `String` (optional): Name of the database where the information will be stored (default `'subspace.db'`)
12-
- `callInterval` - `Number` (optional): - Interval of time in milliseconds to poll a contract/address to determine changes in state or balance (default: `undefined`. Obtains data every block. If using a HttpProvider, the default is: `1000`)
12+
- `callInterval` - Interval of time in milliseconds to query a contract/address to determine changes in state or balance. It's only used with HttpProviders (default: `undefined`. Obtains data every block using the average block time as an interval).
1313
- `refreshLastNBlocks` - Ignores last N blocks (from current block), stored in the local db and refresh them via a web3 subscription. Useful for possible reorgs (default: 12),
1414
- `disableSubscriptions` - Subspace by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an HttpProvider. This functionality can be disabled by passing true to this option. (default: undefined)
1515

‎packages/docs/getting-started.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ const Subspace = require('@embarklabs/subspace');
2222

2323

2424
## Connecting to a web3 provider
25-
To interact with the EVM, **Subspace** requires a valid Web3 provider.
25+
To interact with the EVM, **Subspace** requires a valid Web3 object, connected to a provider
2626

2727
```js
28-
const subspace = new Subspace(web3.currentProvider);
28+
const subspace = new Subspace(web3);
2929
await subspace.init();
3030
```
3131

3232
In addition to the provider, `Subspace` also accepts an `options` object with settings that can change its behavior:
3333
- `dbFilename` - Name of the database where the information will be stored (default `'subspace.db'`)
34-
- `callInterval` - Interval of time in milliseconds to query a contract/address to determine changes in state or balance (default: `undefined`. Obtains data every block).
34+
- `callInterval` - Interval of time in milliseconds to query a contract/address to determine changes in state or balance. It's only used with HttpProviders (default: `undefined`. Obtains data every block using the average block time as an interval).
3535
- `refreshLastNBlocks` - Ignores last N blocks (from current block), stored in the local db and refresh them via a web3 subscription. Useful for possible reorgs (default: 12),
36-
- `disableSubscriptions` - Subspace by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an HttpProvider. This functionality can be disabled by passing true to this option. (default: undefined)
36+
- `disableSubscriptions` - Subspace by default will attempt to use websocket subscriptions if the current provider supports them, otherwise it will use polling because it asumes the provider is an HttpProvider. This functionality can be disabled by passing true to this option. (default: `undefined`)
3737

3838

3939
## Enhancing your contract objects
@@ -88,13 +88,13 @@ The subscription will be triggered whenever the title changes
8888
## Tracking events
8989
You can track events and react to their returned values.
9090
```js
91-
const eventObservable$ = Contract.event.eventName().track();
91+
const eventObservable$ = Contract.event.eventName.track();
9292
```
9393

9494
Example:
9595

9696
```js
97-
const rating$ = Product.events.Rating().track().map("rating")).pipe(map(x => parseInt(x)));
97+
const rating$ = Product.events.Rating.track().map("rating")).pipe(map(x => parseInt(x)));
9898
rating$.subscribe((rating) => console.log("rating received: " + rating));
9999

100100

@@ -112,7 +112,7 @@ For e.g: if you needed to get the average rating of the last 5 events:
112112
```js
113113
import { $average, $latest } from "@embarklabs/subspace";
114114

115-
const rating$ = Product.events.Rating().track().map("rating")).pipe(map(x => parseInt(x)));
115+
const rating$ = Product.events.Rating.track().map("rating")).pipe(map(x => parseInt(x)));
116116

117117
rating$.pipe($latest(5), $average()).subscribe((rating) => {
118118
console.log("average rating of the last 5 events is " + rating)
@@ -212,6 +212,6 @@ If **Subspace** is not needed anymore, you need can invoke `close()` to dispose
212212
subspace.close();
213213
```
214214
::: warning What about subscriptions created with our observables?
215-
Any subscription created via the tracking methods must be unsubscribed manually (in the current version).
215+
`close()` will dispose any web3 subscription created when using a Subspace tracking method, however any subscription to an observable must still be unsubscribed manually. The npm package `subsink` can be used to clear all the observables' subscriptions at once.
216216
:::
217217

‎packages/docs/integrations.md

Whitespace-only changes.

‎packages/docs/react.md

+78-76
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,111 @@
11
# React
2-
We provide a higher-order component to connect to enhance presentational components to react to any observable (not limited to those generated by **Subspace**).
2+
Subspace also provides a set of components that simplifies its usage within React projects through the `@embarklabs/subspace-react` package.
3+
4+
### Install
5+
You can install it through npm or yarn:
6+
```
7+
npm install --save @embarklabs/subspace-react web3 rxjs # RxJS and Web3.js are needed peer-dependencies
8+
```
39

410
### Usage
5-
```js
6-
import { observe } from '@embarklabs/subspace/react';
711

8-
const ObserverComponent = observe(WrappedComponent);
9-
```
12+
#### SubspaceProvider
13+
To use most of the `subspace-react` components, you need to wrap your app with the `<SubspaceProvider web3={web3} />` component. This will make Subspace available to any nested components that accesses it via the `useSubspace` hook or has been wrapped in the `withSubspace` higher order component. Any React component might use Subspace so it makes sense to add the provider near the top level of your dApp. The `SubspaceProvider` requires a web3 object
1014

11-
This enhanced component will subscribe to any observable property it receives when the component is mounted and automatically unsubscribe when the component is unmounted.
15+
```js
16+
// index.js
17+
import React from 'react'
18+
import ReactDOM from 'react-dom'
19+
import MyApp from './MyApp'
20+
import { SubspaceProvider } from '@embarklabs/subspace-react';
1221

13-
### Example
22+
const web3 = new Web3("ws://localhost:8545");
1423

15-
::: tip
16-
This example is available in [Github](https://github.com/embark-framework/subspace/tree/master/examples/react-example1)
17-
:::
24+
const rootElement = document.getElementById('root')
25+
ReactDOM.render(
26+
<SubspaceProvider web3={web3}>
27+
<MyApp />
28+
</SubspaceProvider>,
29+
rootElement
30+
);
31+
```
1832

1933

20-
#### MyComponentObserver.js
34+
#### useSubspace
35+
Rather than relying on global variables or passing Subspace through props, The easiest way to access Subspace features is via the `useSubspace` hook. Be sure that your entire dApp is wrapped with a `<SubspaceProvider />` to have it available througout the component tree.
2136
```js
22-
import React from "react";
23-
import ReactDOM from 'react-dom';
24-
import {observe} from "@embarklabs/subspace/react";
37+
// index.js
38+
import React from 'react'
39+
import { useSubspace } from '@embarklabs/subspace-react';
2540

26-
const MyComponent = ({eventData}) => {
27-
// Handle initial state when no data is available
28-
if (!eventData) {
29-
return <p>No data</p>;
30-
}
31-
32-
return <p>{eventData.someReturnedValue}</p>
33-
};
41+
const MyComponent = () => {
42+
const subspace = useSubspace();
3443

35-
// MyComponent will now observe any observable prop it receives
36-
// and update its state whenever the observable emits an event
37-
export default observe(MyComponent);
38-
```
44+
// do something....
45+
// subspace.trackBalance(web3.eth.defaultAccount);
3946

40-
#### App.js
41-
```js
42-
import React, {Component} from 'react';
43-
import ReactDOM from 'react-dom';
44-
import Subspace from '@embarklabs/subspace';
47+
return ...;
48+
}
4549

46-
import MyComponentObserver from './MyComponentObserver';
50+
export default MyComponent
51+
```
4752

48-
class App extends Component {
49-
state = {
50-
myEventObservable$: null
51-
}
5253

53-
async componentDidMount() {
54-
const MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance
54+
#### withSubspace
55+
This higher order component is provided as an alternative to the `useSubspace` hook. This injects the `subspace` property with an already initialized Subspace instance. Just like with the hook, your entire dApp needs to be wrapped with a `<SubspaceProvider />`.
5556

56-
const subspace = new Subspace("wss://localhost:8545"); // Use a valid provider (geth, parity, infura...)
57-
await subspace.init()
58-
59-
const myEventObservable$ = subspace.trackEvent(MyContractInstance, "MyEvent", {filter: {}, fromBlock: 1 });
60-
this.setState({ myEventObservable$ });
61-
}
57+
```js
58+
// index.js
59+
import React from 'react'
60+
import { withSubspace } from '@embarklabs/subspace-react';
6261

63-
render() {
64-
return <MyComponentObserver eventData={this.state.myEventObservable$} />;
65-
}
62+
const MyComponent = (props) => {
63+
// do something....
64+
// props.subspace.trackBalance(web3.eth.defaultAccount);
65+
66+
return ...;
6667
}
6768

68-
export default App;
69+
export default withSubspace(MyComponent);
6970
```
7071

71-
::: warning Handling Contract Objects
72-
The variable `MyContractInstance` is a `web3.eth.Contract` object pointing to a deployed contract address. You can use a DApp framework like [Embark](https://embark.status.im/docs/contracts_javascript.html) to easily import that contract instance: `import { MyContract } from './embarkArtifacts/contracts';`, or use web3.js directly (just like in the example [source code](https://github.com/embarklabs/subspace/blob/master/examples/react/src/MyContract.js#L36-L42))
73-
:::
7472

75-
#### index.js
73+
#### observe
74+
75+
Useful to make your component subscribe to any observable props it receives when the component is mounted and automatically unsubscribes when the component is unmounted. It can be used with any kind of observables.
76+
77+
7678
```js
77-
import React from 'react';
78-
import ReactDOM from 'react-dom';
79-
import App from './App';
79+
import { observe } from '@embarklabs/subspace-react';
8080

81-
ReactDOM.render(<App />, document.getElementById('root'));
81+
const ObserverComponent = observe(WrappedComponent);
8282
```
8383

84-
84+
##### Example usage:
8585
```js
86-
import { observe } from "@embarklabs/subspace/react";
87-
88-
const ProductComponent = ({ maxRating, minRating, averageRating }) => {
89-
return <ul>
90-
<li><b>minimum rating: </b> {minRating}</li>
91-
<li><b>maximum rating: </b> {maxRating}</li>
92-
<li><b>average rating: </b> {averageRating}</li>
93-
</ul>;
86+
const MyComponent = ({eventData}) => {
87+
// Handle initial state when no data is available
88+
if (!eventData) {
89+
return <p>No data</p>;
90+
}
91+
92+
return <p>Value: {eventData.someReturnValue}</p>
9493
};
9594

96-
const ReactiveProductComponent = observe(ProductComponent);
9795

98-
const Product = subspace.contract({abi, address});
99-
const rating$ = Product.events.Rating.track().map("rating").pipe(map(x => parseInt(x)));
96+
const MyEnhancedComponent = observe(MyComponent);
10097

101-
ReactDOM.render(
102-
<ReactiveProductComponent
103-
maxRating={rating$.pipe($max())}
104-
minRating={rating$.pipe($min())}
105-
averageRating={rating$.pipe($average())}
106-
/>,
107-
document.getElementById('hello-example')
108-
);
98+
99+
const SomeOtherComponent = () => {
100+
const myObservable$ = MyContractInstance.events.MyEvent.track({fromBlock: 1});
101+
return <MyEnhancedComponent myProp={myObservable$} />;
102+
}
109103
```
104+
105+
::: warning Handling Contract Objects
106+
The variable `MyContractInstance` is a `web3.eth.Contract` object pointing to a deployed contract address that has been enhanced with `subspace.contract()`. You can use a DApp framework like [Embark](https://embark.status.im/docs/contracts_javascript.html) to easily import that contract instance: `import { MyContract } from './embarkArtifacts/contracts';`.
107+
:::
108+
109+
::: tip
110+
To learn more about how to use `subspace-react`, there are full working examples available in [Github](https://github.com/embark-framework/subspace/tree/master/examples/)
111+
:::

‎packages/docs/reactive-graphql.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const gql = require("graphql-tag");
1414
const { graphql } = require("reactive-graphql");
1515

1616
const run = async () => {
17-
const subspace = new Subspace(web3.currentProvider); // Use a valid provider (geth, parity, infura...)
17+
const subspace = new Subspace(web3);
1818
await subspace.init();
1919

2020
const MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance
@@ -31,9 +31,7 @@ const run = async () => {
3131

3232
const resolvers = {
3333
Query: {
34-
myEvents: () => {
35-
return subspace.trackEvent(MyContractInstance, 'MyEvent', { filter: {}, fromBlock: 1 })
36-
}
34+
myEvents: () => subspace.trackEvent(MyContractInstance, 'MyEvent', { filter: {}, fromBlock: 1 })
3735
}
3836
};
3937

‎packages/docs/redux.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { myAction } from './actions';
1919
const run = async () => {
2020
const MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance
2121

22-
const subspace = new Subspace("ws://localhost:8545"); // Use a valid provider (geth, parity, infura...)
22+
const subspace = new Subspace(web3);
2323
await subspace.init();
2424

2525
subspace.trackEvent(MyContractInstance, "MyEvent", {filter: {}, fromBlock: 1 })

‎packages/docs/vue.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default {
5656
created: async function(){
5757
this.MyContractInstance = ...; // TODO: obtain a web3.eth.contract instance
5858

59-
const subspace = new Subspace("wss://localhost:8545"); // Use a valid provider (geth, parity, infura...)
59+
const subspace = new Subspace(web3);
6060
await subspace.init();
6161

6262
this.myEventObservable$ = subspace.trackEvent(this.MyContractInstance, "MyEvent", {filter: {}, fromBlock: 1 });

0 commit comments

Comments
 (0)