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