-
Notifications
You must be signed in to change notification settings - Fork 31
/
Client.jsx
134 lines (114 loc) · 3.6 KB
/
Client.jsx
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Provides a shared streamr-client instance.
*/
/* eslint-disable react/no-unused-state */
import React, { useState, useEffect, useLayoutEffect, useCallback, useMemo } from 'react'
import { connect } from 'react-redux'
import t from 'prop-types'
import StreamrClient from 'streamr-client'
import { selectAuthApiKeyId } from '$shared/modules/resourceKey/selectors'
import { getMyResourceKeys } from '$shared/modules/resourceKey/actions'
import useIsMountedRef from '$shared/hooks/useIsMountedRef'
import * as services from '../services'
export const ClientContext = React.createContext()
export function createClient(apiKey) {
return new StreamrClient({
url: process.env.STREAMR_WS_URL,
restUrl: process.env.STREAMR_API_URL,
auth: {
apiKey, // assume this won't change for now
},
autoConnect: true,
autoDisconnect: false,
})
}
function useClientProvider({ apiKey }) {
const [client, setClient] = useState()
const isMountedRef = useIsMountedRef()
const hasClient = !!client
const reset = useCallback(() => {
if (!client) { return }
// clean up listeners
client.connection.off('disconnecting', reset)
client.connection.off('disconnected', reset)
client.off('error', reset)
if (!isMountedRef.current) { return }
// reset client unless already changed
setClient((currentClient) => {
if (currentClient !== client) { return currentClient }
return undefined
})
}, [client, setClient, isMountedRef])
// listen for state changes which should trigger reset
useEffect(() => {
if (!client) { return }
client.connection.once('disconnecting', reset)
client.connection.once('disconnected', reset)
client.once('error', reset)
return reset // reset to cleanup
}, [reset, client, apiKey])
// (re)create client if none
useLayoutEffect(() => {
if (!apiKey || hasClient) { return }
setClient(createClient(apiKey))
}, [hasClient, setClient, apiKey])
// disconnect on unmount/client change
useEffect(() => {
if (!client) { return }
return () => {
client.ensureDisconnected()
}
}, [client, setClient])
const send = useCallback(async (rest) => (
services.send({
apiKey,
...rest,
})
), [apiKey])
return useMemo(() => ({
client,
send,
}), [client, send])
}
export function ClientProviderComponent({ children, apiKey }) {
return (
<ClientContext.Provider value={useClientProvider({ apiKey })}>
{children || null}
</ClientContext.Provider>
)
}
ClientProviderComponent.propTypes = {
apiKey: t.string,
}
const withAuthApiKey = connect((state) => ({
apiKey: selectAuthApiKeyId(state),
}), {
loadKeys: getMyResourceKeys,
})
export const ClientProvider = withAuthApiKey(class ClientProvider extends React.Component {
state = {
isLoading: false,
}
async loadIfNoKey() {
if (this.state.isLoading || this.props.apiKey) { return }
this.setState({ isLoading: true })
try {
await this.props.loadKeys()
} finally {
this.setState({ isLoading: false })
}
}
componentDidUpdate() {
return this.loadIfNoKey()
}
componentDidMount() {
return this.loadIfNoKey()
}
render() {
const { loadKey, ...props } = this.props
// new client if apiKey changes
return (
<ClientProviderComponent {...props} />
)
}
})