Skip to content

Instantly share code, notes, and snippets.

@raphapassini
Created September 27, 2019 04:42
Show Gist options
  • Save raphapassini/c033a65b222f72e31da561a7c8a72c51 to your computer and use it in GitHub Desktop.
Save raphapassini/c033a65b222f72e31da561a7c8a72c51 to your computer and use it in GitHub Desktop.
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