-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6191cf
commit 163b715
Showing
7 changed files
with
61 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,57 @@ | ||
import { Action, ActionType } from '../actions'; | ||
import { PayloadAction, createSlice } from '@reduxjs/toolkit'; | ||
import z from 'zod'; | ||
|
||
import { createWebsocketResponseSchema } from '../websocketActions'; | ||
|
||
export type State = { | ||
connected: boolean; | ||
error?: string; | ||
featureFlagEnabled: boolean; | ||
}; | ||
|
||
const DEFAULT: State = { | ||
const initialState: State = { | ||
connected: false, | ||
featureFlagEnabled: false, | ||
}; | ||
|
||
export default function websocket(state = DEFAULT, action: Action): State { | ||
switch (action.type) { | ||
case ActionType.WebSocketConnected: | ||
return { ...state, connected: true, error: undefined }; | ||
const websocketErrorPayloadSchema = z.object({ | ||
error: z.string(), | ||
}); | ||
type websocketErrorPayload = z.infer<typeof websocketErrorPayloadSchema>; | ||
|
||
const slice = createSlice({ | ||
name: 'websocket', | ||
initialState, | ||
reducers: { | ||
connected: (state) => { | ||
state.connected = true; | ||
delete state.error; | ||
}, | ||
|
||
disconnected: (state) => { | ||
state.connected = false; | ||
}, | ||
|
||
error: (state, action: PayloadAction<websocketErrorPayload>) => { | ||
state.error = action.payload.error; | ||
}, | ||
|
||
case ActionType.WebSocketDisconnected: | ||
return { ...state, connected: false }; | ||
featureFlagEnabled: (state) => { | ||
state.featureFlagEnabled = true; | ||
}, | ||
}, | ||
}); | ||
|
||
case ActionType.WebSocketError: | ||
return { ...state, error: action.error }; | ||
export const { | ||
connected: websocketConnected, | ||
disconnected: websocketDisconnected, | ||
error: websocketError, | ||
featureFlagEnabled: websocketFeatureFlagEnabled, | ||
} = slice.actions; | ||
|
||
case ActionType.WebSocketFeatureFlagEnabled: | ||
return { ...state, featureFlagEnabled: true }; | ||
export const websocketErrorSchema = createWebsocketResponseSchema( | ||
websocketError, | ||
websocketErrorPayloadSchema, | ||
); | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
export default slice.reducer; |
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
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