/
index.ts
55 lines (51 loc) · 1.18 KB
/
index.ts
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
/**
* Represents an action in the application.
*/
export interface Action {
type: string;
}
/**
* Represents an action that sets the value of a specific key in the global state.
*/
export interface SetValueAction extends Action {
key: string;
value: unknown;
}
/**
* Represents the global state object.
*/
export interface GlobalState {
[key: string]: unknown;
}
/**
* Reducer function for managing the global state.
*
* @param state - The current global state.
* @param action - The action to be performed on the global state.
* @returns The updated global state.
*/
export function GlobalStateReducer(
state: GlobalState,
action: unknown,
): GlobalState {
switch ((action as Action).type) {
case "SET_GLOBAL_STATE_VALUE": {
const { key, value } = action as SetValueAction;
return {
...state,
[key]: value,
};
}
case "RESET_GLOBAL_STATE":
return {};
case "REMOVE_GLOBAL_STATE_VALUE": {
const { key } = action as SetValueAction;
const removeKey = key;
const { [removeKey]: removedKey, ...rest } = state;
console.warn(removedKey);
return rest;
}
default:
return state;
}
}