forked from EcoG-io/react-wot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.2 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
// @ts-check
import React from 'react';
// WoT imports, needed for several components
import * as Core from '@node-wot/core';
import * as Http from '@node-wot/binding-http';
import * as WebSocket from '@node-wot/binding-websockets';
export const WoT = {
Core,
Http,
WebSocket
};
export class WoTProvider extends React.Component {
state = {
wot: {}
};
componentDidMount = () => {
const servient = new WoT.Core.Servient();
servient.addClientFactory(new WoT.Http.HttpClientFactory());
servient.addClientFactory(new WoT.WebSocket.WebSocketClientFactory());
servient.addClientFactory(new WoT.WebSocket.WebSocketSecureClientFactory());
servient.start().then(wot => {
this.setState({ wot });
});
};
render() {
return (
<WoTContext.Provider value={{ wot: this.state.wot }}>
{this.props.children}
</WoTContext.Provider>
);
}
}
export const WoTContext = React.createContext({ wot: {} });
export const withWoT = ComponentToWrap => {
class WithWoT extends React.Component {
render() {
let WoT = this.context.wot;
return <ComponentToWrap {...this.props} WoT={WoT} />;
}
}
WithWoT.contextType = WoTContext;
return WithWoT;
};