-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (97 loc) · 2.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView, ActivityIndicator } from 'react-native';
import { dataSources, getClient } from '../../libs/ocap';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: dataSources[1],
message: null,
timestamp: null,
subscribed: false,
};
}
async componentDidMount() {
const client = getClient(this.state.dataSource.name);
const subscription = await client.newBlockMined();
subscription
.on('data', data => {
this.setState({
message: data,
timestamp: new Date(),
});
setTimeout(() => {
this.setState({ message: null });
}, 5000);
})
.on('error', err => {
this.setState({ message: err });
});
this.setState({ subscribed: true });
}
render() {
const { subscribed, message, timestamp, dataSource } = this.state;
return (
<ScrollView contentContainerStyle={styles.container}>
<Text style={styles.header}>Subscription Demo</Text>
{subscribed || (
<Text style={{ marginBottom: 15 }}>
Try to subscribe to {dataSource.name.toUpperCase()}
.newBlockMined
</Text>
)}
{subscribed && (
<Text style={{ marginBottom: 15 }}>
{dataSource.name.toUpperCase()}
.newBlockMined subscription success
</Text>
)}
{subscribed &&
!message && (
<View>
<Text style={{ marginBottom: 15 }}>Waiting for data</Text>
<ActivityIndicator size="large" color="#0000ff" />
</View>
)}
{message && (
<View style={{flex: 1}}>
<Text style={{ marginBottom: 15 }}>
New {dataSource.name.toUpperCase()} blocked mined at {timestamp.toString()}:
</Text>
<Text style={styles.code}>{JSON.stringify(message, true, ' ')}</Text>
</View>
)}
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 10,
paddingTop: 30,
justifyContent: 'flex-start',
alignItems: 'flex-start',
backgroundColor: '#F5FCFF',
},
header: {
fontSize: 20,
marginBottom: 10,
},
code: {
backgroundColor: '#EEEE',
padding: 10,
marginTop: 10,
borderRadius: 5,
borderWidth: 1,
borderStyle: 'solid',
borderColor: '#AAAA',
},
});