Created
September 27, 2019 04:42
-
-
Save raphapassini/c033a65b222f72e31da561a7c8a72c51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from 'react' | |
export const AppContext = React.createContext() | |
export const WS = new WebSocket("ws://localhost:8000/subscribe=accounts,funds,orders,quotes&refresh=2") | |
export const AppContextProvider = (props) => { | |
let [loading, setLoading] = useState(true) | |
let [accounts, setAccounts] = useState({}) | |
let [funds, setFunds] = useState({}) | |
let [orders, setOrders] = useState([]) | |
let [quotes, setQuotes] = useState({}) | |
WS.onopen = () => { | |
setLoading(false) | |
} | |
WS.onmessage = evt => { | |
let data = JSON.parse(evt.data) | |
setAccounts(data.accounts) | |
setQuotes(data.quotes) | |
setFunds(data.funds) | |
setOrders(data.orders) | |
} | |
return( | |
<AppContext.Provider value={{accounts, funds, orders, quotes | |
}}> | |
{props.children} | |
</AppContext.Provider> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment