|
1 | | -import { Action } from 'overmind' |
2 | | -import * as mutations from './mutations' |
3 | | -import * as operations from './operations' |
4 | | -import { Message, Tab } from './types' |
| 1 | +import { Action, pipe, OnInitialize, Pipe } from 'overmind' |
| 2 | +import { |
| 3 | + Message, |
| 4 | + Tab, |
| 5 | + AppMessageType, |
| 6 | + ExecutionType, |
| 7 | + ActionsListItemType, |
| 8 | +} from './types' |
| 9 | +import { |
| 10 | + forkEachMessage, |
| 11 | + ensureCurrentApp, |
| 12 | + addFlushAndRunMutations, |
| 13 | + updateComponent, |
| 14 | + addComponent, |
| 15 | + removeComponent, |
| 16 | + updateDerived, |
| 17 | + updateFlushWithDerived, |
| 18 | + addAction, |
| 19 | + addOperator, |
| 20 | + updateFlushWithReactionUpdate, |
| 21 | + updateOperator, |
| 22 | + updateAction, |
| 23 | + addMutations, |
| 24 | + addEffect, |
| 25 | + setPortExists, |
| 26 | + isPortExistsMessage, |
| 27 | + getMessages, |
| 28 | + addState, |
| 29 | + ensureApp, |
| 30 | + addClientMessages, |
| 31 | +} from './operators' |
5 | 32 |
|
6 | | -const handleClientMessages: Action<Message> = (action) => |
7 | | - action |
8 | | - .mutate(mutations.ensureCurrentApp) |
9 | | - .mutate(mutations.performMutationsByMessageType) |
10 | | - .mutate(mutations.addMessagesFromClient) |
| 33 | +export const onInitialize: OnInitialize = async ({ |
| 34 | + value: actions, |
| 35 | + state, |
| 36 | + storage, |
| 37 | + connector, |
| 38 | +}) => { |
| 39 | + const port = await storage.get<string>('currentPort') |
| 40 | + if (port) { |
| 41 | + state.port = port |
| 42 | + } |
| 43 | + connector.onMessage(actions.onMessage) |
| 44 | + connector.connect(state.port) |
| 45 | +} |
11 | 46 |
|
12 | | -const setPortExists: Action<Message> = (action) => |
13 | | - action.mutate(mutations.setPortExists) |
| 47 | +const handleClientMessage: Pipe<Message> = pipe( |
| 48 | + ensureCurrentApp, |
| 49 | + ensureApp, |
| 50 | + addClientMessages, |
| 51 | + getMessages, |
| 52 | + forkEachMessage({ |
| 53 | + [AppMessageType.PORT_EXISTS]: setPortExists, |
| 54 | + [ExecutionType.INIT]: addState, |
| 55 | + [ExecutionType.FLUSH]: addFlushAndRunMutations, |
| 56 | + [ExecutionType.DERIVED]: updateDerived, |
| 57 | + [ExecutionType.MUTATIONS]: addMutations, |
| 58 | + [ExecutionType.EFFECT]: addEffect, |
| 59 | + [ExecutionType.COMPONENT_ADD]: addComponent, |
| 60 | + [ExecutionType.COMPONENT_UPDATE]: updateComponent, |
| 61 | + [ExecutionType.COMPONENT_REMOVED]: removeComponent, |
| 62 | + [ExecutionType.DERIVED_DIRTY]: updateFlushWithDerived, |
| 63 | + [ExecutionType.REACTION_UPDATE]: updateFlushWithReactionUpdate, |
| 64 | + [ExecutionType.ACTION_START]: addAction, |
| 65 | + [ExecutionType.OPERATOR_START]: addOperator, |
| 66 | + [ExecutionType.OPERATOR_END]: updateOperator, |
| 67 | + [ExecutionType.ACTION_END]: updateAction, |
| 68 | + }) |
| 69 | +) |
14 | 70 |
|
15 | | -const onMessage: Action<Message> = (action) => |
16 | | - action.when(operations.isPortExistsMessage, { |
| 71 | +export const onMessage: Pipe<Message> = pipe( |
| 72 | + isPortExistsMessage({ |
17 | 73 | true: setPortExists, |
18 | | - false: handleClientMessages, |
| 74 | + false: handleClientMessage, |
19 | 75 | }) |
| 76 | +) |
20 | 77 |
|
21 | | -export const loadDevtools: Action = (action) => |
22 | | - action |
23 | | - // .run(({ storage }) => storage.clear()) |
24 | | - .map(operations.getCurrentPortFromStorage) |
25 | | - .mutate(mutations.setPort) |
26 | | - .run(operations.connectCurrentPort(onMessage(action as any))) |
| 78 | +export const setError: Action<string> = ({ value: error, state }) => |
| 79 | + (state.error = error) |
27 | 80 |
|
28 | | -export const setError: Action<string> = ({ mutate }) => |
29 | | - mutate(mutations.setError) |
| 81 | +export const changeNewPortValue: Action<string> = ({ value: port, state }) => |
| 82 | + (state.newPortValue = String(Number(port))) |
30 | 83 |
|
31 | | -export const changeNewPortValue: Action<string> = (action) => |
32 | | - action.map(operations.toNumber).mutate(mutations.setNewPortValue) |
| 84 | +export const addConnection: Action = ({ state, connector }) => { |
| 85 | + state.error = null |
| 86 | + state.isConnecting = true |
| 87 | + state.port = state.newPortValue |
| 88 | + state.newPortValue = '' |
| 89 | + connector.connect(state.port) |
| 90 | +} |
33 | 91 |
|
34 | | -export const addConnection: Action = (action) => |
35 | | - action |
36 | | - .mutate(mutations.setConnecting) |
37 | | - .map(operations.getNewPortFromState) |
38 | | - .mutate(mutations.setPort) |
39 | | - .mutate(mutations.resetNewPortValue) |
40 | | - .run(operations.connectCurrentPort(onMessage)) |
| 92 | +export const changeTab: Action<Tab> = ({ value: tab, state }) => |
| 93 | + (state.currentTab = tab) |
41 | 94 |
|
42 | | -export const changeTab: Action<Tab> = ({ mutate }) => |
43 | | - mutate(mutations.changeTab) |
| 95 | +export const toggleExpandState: Action<string[]> = ({ value: path, state }) => { |
| 96 | + const pathString = path.join('.') |
44 | 97 |
|
45 | | -export const toggleExpandState: Action<string[]> = ({ mutate }) => |
46 | | - mutate(mutations.toggleExpandStatePath) |
| 98 | + if (state.expandedStatePaths.indexOf(pathString) >= 0) { |
| 99 | + state.expandedStatePaths.splice( |
| 100 | + state.expandedStatePaths.indexOf(pathString), |
| 101 | + 1 |
| 102 | + ) |
| 103 | + } else { |
| 104 | + state.expandedStatePaths = state.expandedStatePaths.concat(pathString) |
| 105 | + } |
| 106 | +} |
47 | 107 |
|
48 | | -export const selectAction: Action<string> = ({ mutate }) => |
49 | | - mutate(mutations.toggleActionItemCollapse).mutate(mutations.selectAction) |
| 108 | +export const selectAction: Action<string> = ({ value: actionId, state }) => { |
| 109 | + for (let index in state.currentApp.actionsList) { |
| 110 | + const item = state.currentApp.actionsList[index] |
| 111 | + if ( |
| 112 | + item.type === ActionsListItemType.GROUP && |
| 113 | + item.id === actionId && |
| 114 | + state.currentApp.currentActionId === actionId |
| 115 | + ) { |
| 116 | + item.isCollapsed = !item.isCollapsed |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + state.currentApp.currentActionId = actionId |
| 121 | +} |
50 | 122 |
|
51 | | -type Collapsed = { |
52 | | - isCollapsed: boolean |
| 123 | +export const toggleCollapsedFlush: Action<number> = ({ value: id, state }) => { |
| 124 | + if (!state.expandAllActionDetails) { |
| 125 | + state.currentApp.flushes[id].isCollapsed = !state.currentApp.flushes[id] |
| 126 | + .isCollapsed |
| 127 | + } |
53 | 128 | } |
54 | 129 |
|
55 | | -export const toggleCollapsedActionItem: Action<Collapsed> = (action) => |
56 | | - action |
57 | | - .filter(operations.isNotExpandingAllActions) |
58 | | - .mutate(mutations.toggleCollapsed) |
| 130 | +export const toggleCollapsedOperator: Action<number> = ({ |
| 131 | + value: operatorIndex, |
| 132 | + state, |
| 133 | +}) => { |
| 134 | + if (!state.expandAllActionDetails) { |
| 135 | + const operator = state.currentAction.operators[operatorIndex] |
| 136 | + operator.isCollapsed = !operator.isCollapsed |
| 137 | + } |
| 138 | +} |
59 | 139 |
|
60 | | -export const toggleGroupedComponent: Action<string> = (action) => |
61 | | - action.mutate(mutations.toggleGroupedComponent) |
| 140 | +export const toggleGroupedComponent: Action<string> = ({ |
| 141 | + value: name, |
| 142 | + state, |
| 143 | +}) => { |
| 144 | + const index = state.expandedComponents.indexOf(name) |
| 145 | + if (index === -1) { |
| 146 | + state.expandedComponents.push(name) |
| 147 | + } else { |
| 148 | + state.expandedComponents.splice(index, 1) |
| 149 | + } |
| 150 | +} |
62 | 151 |
|
63 | | -export const selectApp: Action<string> = (action) => |
64 | | - action.mutate(mutations.selectApp) |
| 152 | +export const selectApp: Action<string> = ({ value: appName, state }) => |
| 153 | + (state.currentAppName = appName) |
65 | 154 |
|
66 | | -export const toggleExpandAllActions: Action = ({ mutate }) => |
67 | | - mutate(mutations.toggleExpandAllActions) |
| 155 | +export const toggleExpandAllActions: Action = ({ state }) => |
| 156 | + (state.expandAllActionDetails = !state.expandAllActionDetails) |
0 commit comments