forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.ts
More file actions
164 lines (147 loc) · 4.37 KB
/
actions.ts
File metadata and controls
164 lines (147 loc) · 4.37 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Action, pipe, OnInitialize, Operator } from 'overmind'
import {
Message,
Tab,
AppMessageType,
ExecutionType,
ActionsListItemType,
} from './types'
import {
forkEachMessage,
ensureCurrentApp,
addFlushAndRunMutations,
updateComponent,
addComponent,
removeComponent,
updateDerived,
updateFlushWithDerived,
addAction,
addOperator,
updateFlushWithReactionUpdate,
updateOperator,
updateAction,
addMutations,
addEffect,
setPortExists,
isPortExistsMessage,
getMessages,
addState,
ensureApp,
addClientMessages,
} from './operators'
export const onInitialize: OnInitialize = async ({
value: actions,
state,
storage,
connector,
}) => {
const port = await storage.get<string>('currentPort')
if (port) {
state.port = port
}
connector.onMessage(actions.onMessage)
connector.connect(state.port)
}
const handleClientMessage: Operator<Message, any> = pipe(
ensureCurrentApp,
ensureApp,
addClientMessages,
getMessages,
forkEachMessage({
[AppMessageType.PORT_EXISTS]: setPortExists,
[ExecutionType.INIT]: addState,
[ExecutionType.FLUSH]: addFlushAndRunMutations,
[ExecutionType.DERIVED]: updateDerived,
[ExecutionType.MUTATIONS]: addMutations,
[ExecutionType.EFFECT]: addEffect,
[ExecutionType.COMPONENT_ADD]: addComponent,
[ExecutionType.COMPONENT_UPDATE]: updateComponent,
[ExecutionType.COMPONENT_REMOVE]: removeComponent,
[ExecutionType.DERIVED_DIRTY]: updateFlushWithDerived,
[ExecutionType.REACTION_UPDATE]: updateFlushWithReactionUpdate,
[ExecutionType.ACTION_START]: addAction,
[ExecutionType.OPERATOR_START]: addOperator,
[ExecutionType.OPERATOR_END]: updateOperator,
[ExecutionType.ACTION_END]: updateAction,
})
)
export const onMessage: Operator<Message, any> = pipe(
isPortExistsMessage({
true: setPortExists,
false: handleClientMessage,
})
)
export const setError: Action<string> = ({ value: error, state }) =>
(state.error = error)
export const changeNewPortValue: Action<string> = ({ value: port, state }) =>
(state.newPortValue = String(Number(port)))
export const addConnection: Action = ({ state, connector }) => {
state.error = null
state.isConnecting = true
state.port = state.newPortValue
state.newPortValue = ''
connector.connect(state.port)
}
export const changeTab: Action<Tab> = ({ value: tab, state }) =>
(state.currentTab = tab)
export const toggleExpandState: Action<string[]> = ({ value: path, state }) => {
const pathString = path.join('.')
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
state.expandedStatePaths.splice(
state.expandedStatePaths.indexOf(pathString),
1
)
} else {
state.expandedStatePaths = state.expandedStatePaths.concat(pathString)
}
}
export const selectAction: Action<string> = ({ value: actionId, state }) => {
for (let index in state.currentApp.actionsList) {
const item = state.currentApp.actionsList[index]
if (
item.type === ActionsListItemType.GROUP &&
item.id === actionId &&
state.currentApp.currentActionId === actionId
) {
item.isCollapsed = !item.isCollapsed
break
}
}
state.currentApp.currentActionId = actionId
}
export const toggleCollapsedFlush: Action<number> = ({ value: id, state }) => {
if (!state.expandAllActionDetails) {
state.currentApp.flushes[id].isCollapsed = !state.currentApp.flushes[id]
.isCollapsed
}
}
export const toggleCollapsedOperator: Action<number> = ({
value: operatorIndex,
state,
}) => {
if (!state.expandAllActionDetails) {
const currentApp = state.apps[state.currentAppName]
const currentAction = currentApp.actions[currentApp.currentActionId]
const operator = currentAction.operators[operatorIndex]
operator.isCollapsed = !operator.isCollapsed
}
}
export const toggleGroupedComponent: Action<string> = ({
value: name,
state,
}) => {
const index = state.expandedComponents.indexOf(name)
if (index === -1) {
state.expandedComponents.push(name)
} else {
state.expandedComponents.splice(index, 1)
}
}
export const selectApp: Action<string> = ({ value: appName, state }) => {
state.currentAppName = appName
state.showApps = false
}
export const toggleExpandAllActions: Action = ({ state }) =>
(state.expandAllActionDetails = !state.expandAllActionDetails)
export const toggleShowApps: Action = ({ state }) =>
(state.showApps = !state.showApps)