(state, action)
| 4 | export const SocketContext = createContext(); |
| 5 | |
| 6 | const socketReducer = (state, action) => { |
| 7 | switch (action.type) { |
| 8 | case "initSocket": { |
| 9 | return { |
| 10 | ...state, |
| 11 | socket: new WebSocket(action.url), |
| 12 | url: action.url, |
| 13 | }; |
| 14 | } |
| 15 | case "renewSocket": { |
| 16 | let timeout = state.wsTimeoutDuration * 2; |
| 17 | if (timeout > 10000) { |
| 18 | timeout = 10000; |
| 19 | } |
| 20 | return { |
| 21 | ...state, |
| 22 | socket: new WebSocket(state.url), |
| 23 | wsTimeoutDuration: timeout, |
| 24 | }; |
| 25 | } |
| 26 | case "updateTimeout": { |
| 27 | return { ...state, connectTimeout: action.timeout }; |
| 28 | } |
| 29 | case "clearTimeout": { |
| 30 | clearTimeout(state.connectTimeout); |
| 31 | return { ...state }; |
| 32 | } |
| 33 | case "resetTimeoutDuration": { |
| 34 | return { ...state, wsTimeoutDuration: 250 }; |
| 35 | } |
| 36 | default: { |
| 37 | return { ...state }; |
| 38 | } |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | const initialState = { |
| 43 | url: "", |
nothing calls this directly
no outgoing calls
no test coverage detected