Skip to content

Commit fcc6d8d

Browse files
Merge pull request cerebral#7 from cerebral/devtools-state
Devtools state
2 parents 700141b + c69e575 commit fcc6d8d

File tree

31 files changed

+457
-128
lines changed

31 files changed

+457
-128
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"ts-jest": "23.0.0",
6666
"ts-loader": "4.4.2",
6767
"tslib": "1.9.3",
68-
"typescript": "2.9.2",
68+
"typescript": "^2.9.2",
6969
"typescript-eslint-parser": "^16.0.1",
7070
"webpack": "4.15.1",
7171
"webpack-cli": "3.0.8",

packages/node_modules/action-chain/src/ActionBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function actionBaseFactory<
2626
Context,
2727
InitialValue,
2828
Value = InitialValue,
29-
ReturnAction = InitialValue extends undefined
29+
ReturnAction = InitialValue extends void
3030
? NoValueActionBase<Context, InitialValue, Value>
3131
: ActionBase<Context, InitialValue, Value>
3232
>(

packages/node_modules/overmind-devtools/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"ts-loader": "^4.4.2",
4141
"tslib": "^1.9.3",
4242
"typescript": "^2.9.2",
43+
"url-loader": "^1.0.1",
4344
"webpack": "^4.15.1",
4445
"webpack-cli": "^3.0.8"
4546
}

packages/node_modules/overmind-devtools/src/client/BackendConnector.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ipcRenderer } from 'electron'
2-
import { resolve } from 'url'
32

43
type Message = {
54
type: string
@@ -26,7 +25,6 @@ class Port {
2625
ipcRenderer.on('port:exists', this.onPortExists)
2726
}
2827
connect() {
29-
console.log('connecting', this.port)
3028
return new Promise((resolve, reject) => {
3129
this.connectionResolver = resolve
3230
this.connectionRejecter = reject
@@ -38,7 +36,6 @@ class Port {
3836
}
3937
onPortAdded = (_, addedPort) => {
4038
if (addedPort === this.port) {
41-
console.log('lisetning for messages')
4239
ipcRenderer.on('message', this.onMessage)
4340
this.connector.sendMessage(this.port, 'ping')
4441
}
@@ -51,7 +48,6 @@ class Port {
5148
}
5249
}
5350
onMessage = (_, message) => {
54-
console.log('got message', message)
5551
if (message.port !== this.port) {
5652
return
5753
}
Lines changed: 39 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,53 @@
1-
import { Action, Context } from './'
2-
import { State, Apps, Message } from './state'
1+
import { Action } from './'
2+
import * as mutations from './mutations'
3+
import * as helpers from './helpers'
4+
import { Message } from './state'
35

4-
/*
5-
TASKS
6-
*/
7-
8-
const getAppsFromStorage = (_, { storage }: Context) => storage.get('apps')
9-
10-
const getCurrentPortFromStorage = (_, { storage }: Context) =>
11-
storage.get('currentPort')
12-
13-
const getNewPortFromState = (_, { state }: Context) => state.newPortValue
14-
15-
const storeApps = (_, { storage, state }: Context) =>
16-
storage.set('apps', state.apps)
17-
18-
const toNumber = (value: string) => String(Number(value))
19-
20-
const connectCurrentPort = (action: (message: any) => void) => (
21-
_,
22-
{ state, connector }: Context
23-
) => connector.addPort(state.currentPort, action)
24-
25-
/*
26-
MUTATIONS
27-
*/
6+
export default (action: Action) => {
7+
const onMessage = action<Message>()
8+
.mutation(mutations.addMessagesFromClient)
9+
.mutation(mutations.performMutationsByMessageType)
2810

29-
const setApps = (apps: Apps, state: State) => (state.apps = apps || {})
11+
const loadDevtools = action()
12+
// .do((_, { storage }) => storage.clear())
13+
.map(helpers.getAppsFromStorage)
14+
.mutation(mutations.setApps)
15+
.map(helpers.getCurrentPortFromStorage)
16+
.mutation(mutations.setCurrentPort)
17+
.mutation(mutations.setAppLoaded)
18+
.map(helpers.connectCurrentPort(onMessage))
3019

31-
const setCurrentPort = (currentPort: string, state: State) => {
32-
if (currentPort) {
33-
state.currentPort = currentPort
34-
} else if (Object.keys(state.apps).length) {
35-
state.currentPort = Object.keys(state.apps)[0]
36-
}
37-
}
20+
const setError = action<string>().mutation(mutations.setError)
3821

39-
const setError = (error: string, state: State) => (state.error = error)
22+
const changeNewPortValue = action<string>()
23+
.map(helpers.toNumber)
24+
.mutation(mutations.setNewPortValue)
4025

41-
const setAppLoaded = (_, state: State) => (state.isLoading = false)
26+
const addPort = action()
27+
.map(helpers.getNewPortFromState)
28+
.mutation(mutations.setCurrentPort)
29+
.mutation(mutations.addNewApp)
30+
.mutation(mutations.resetNewPortValue)
31+
.do(helpers.storeApps)
4232

43-
const setNewPortValue = (value: string, state: State) =>
44-
(state.newPortValue = value)
33+
const openState = action().mutation(mutations.openState)
4534

46-
const addNewApp = (_, state: State) =>
47-
(state.apps[state.newPortValue] = {
48-
name: null,
49-
port: state.newPortValue,
50-
messages: [],
51-
})
35+
const openConsole = action().mutation(mutations.openConsole)
5236

53-
const resetNewPortValue = (_, state: State) => (state.newPortValue = '')
37+
const openApp = action<string>()
5438

55-
const addMessageFromClient = (message: Message, state: State) => {
56-
state.apps[message.port].messages = state.apps[message.port].messages.concat(
57-
message.message
39+
const toggleExpandState = action<string[]>().mutation(
40+
mutations.toggleExpandStatePath
5841
)
59-
}
60-
61-
/*
62-
ACTIONS
63-
*/
64-
65-
export default (action: Action) => {
66-
const onMessage = action<any>().mutation(addMessageFromClient)
6742

6843
return {
69-
loadDevtools: action()
70-
.map(getAppsFromStorage)
71-
.mutation(setApps)
72-
.map(getCurrentPortFromStorage)
73-
.mutation(setCurrentPort)
74-
.mutation(setAppLoaded)
75-
.map(connectCurrentPort(onMessage)),
76-
setError: action<string>().mutation(setError),
77-
changeNewPortValue: action<string>()
78-
.map(toNumber)
79-
.mutation(setNewPortValue),
80-
addPort: action()
81-
.map(getNewPortFromState)
82-
.mutation(setCurrentPort)
83-
.mutation(addNewApp)
84-
.mutation(resetNewPortValue)
85-
.do(storeApps),
86-
openApp: action<string>(),
44+
loadDevtools,
45+
setError,
46+
changeNewPortValue,
47+
addPort,
48+
openState,
49+
openConsole,
50+
openApp,
51+
toggleExpandState,
8752
}
8853
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Context } from './'
2+
3+
export const getAppsFromStorage = (_, { storage }: Context) =>
4+
storage.get('apps')
5+
6+
export const getCurrentPortFromStorage = (_, { storage }: Context) =>
7+
storage.get('currentPort')
8+
9+
export const getNewPortFromState = (_, { state }: Context) => state.newPortValue
10+
11+
export const storeApps = (_, { storage, state }: Context) =>
12+
storage.set('apps', state.apps)
13+
14+
export const toNumber = (value: string) => String(Number(value))
15+
16+
export const connectCurrentPort = (action: (message: any) => void) => (
17+
_,
18+
{ state, connector }: Context
19+
) => connector.addPort(state.currentPort, action)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Apps, State, Message, Tab } from './state'
2+
import { runMutation } from './utils'
3+
4+
export const setApps = (apps: Apps, state: State) => (state.apps = apps || {})
5+
6+
export const setCurrentPort = (currentPort: string, state: State) => {
7+
if (currentPort) {
8+
state.currentPort = currentPort
9+
} else if (Object.keys(state.apps).length) {
10+
state.currentPort = Object.keys(state.apps)[0]
11+
}
12+
}
13+
14+
export const setError = (error: string, state: State) => (state.error = error)
15+
16+
export const setAppLoaded = (_, state: State) => (state.isLoading = false)
17+
18+
export const setNewPortValue = (value: string, state: State) =>
19+
(state.newPortValue = value)
20+
21+
export const addNewApp = (_, state: State) =>
22+
(state.apps[state.newPortValue] = {
23+
name: null,
24+
port: state.newPortValue,
25+
messages: [],
26+
state: {},
27+
})
28+
29+
export const resetNewPortValue = (_, state: State) => (state.newPortValue = '')
30+
31+
export const addMessagesFromClient = (message: Message, state: State) => {
32+
state.apps[message.port].messages = state.apps[message.port].messages.concat(
33+
message.message
34+
)
35+
}
36+
37+
export const openState = (_, state: State) => (state.currentTab = Tab.State)
38+
39+
export const openConsole = (_, state: State) => (state.currentTab = Tab.Console)
40+
41+
export const toggleExpandStatePath = (path: string[], state: State) => {
42+
const pathString = path.join('.')
43+
44+
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
45+
state.expandedStatePaths.splice(
46+
state.expandedStatePaths.indexOf(pathString),
47+
1
48+
)
49+
} else {
50+
state.expandedStatePaths.push(pathString)
51+
}
52+
}
53+
54+
export const performMutationsByMessageType = (
55+
message: Message,
56+
state: State
57+
) => {
58+
message.message.forEach((clientMessage) => {
59+
switch (clientMessage.type) {
60+
case 'init':
61+
state.apps[message.port].state = clientMessage.data.state
62+
break
63+
case 'flush':
64+
clientMessage.data.mutations.forEach(
65+
runMutation(state.apps[message.port].state)
66+
)
67+
break
68+
}
69+
})
70+
}

packages/node_modules/overmind-devtools/src/client/app/state.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type App = {
22
name: string
33
port: string
44
messages: AppMessage[]
5+
state: object
56
}
67

78
export type Apps = {
@@ -10,21 +11,28 @@ export type Apps = {
1011

1112
export type AppMessage = {
1213
type: string
13-
data: object
14+
data: any
1415
}
1516

1617
export type Message = {
1718
port: string
1819
message: AppMessage[]
1920
}
2021

22+
export enum Tab {
23+
State = 'State',
24+
Console = 'Console',
25+
}
26+
2127
export type State = {
2228
isConnecting: boolean
2329
isLoading: boolean
2430
error: string
2531
currentPort: string
2632
apps: Apps
2733
newPortValue: string
34+
currentTab: Tab
35+
expandedStatePaths: string[]
2836
}
2937

3038
const state: State = {
@@ -34,6 +42,8 @@ const state: State = {
3442
currentPort: null,
3543
apps: {},
3644
newPortValue: '',
45+
currentTab: Tab.State,
46+
expandedStatePaths: [''],
3747
}
3848

3949
export default state
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const runMutation = (state) => (mutation) => {
2+
const pathArray = mutation.path.split('.')
3+
const key = pathArray.pop()
4+
const target = pathArray.reduce((current, pathKey) => current[pathKey], state)
5+
6+
switch (mutation.method) {
7+
case 'set':
8+
target[key] = mutation.args[0]
9+
break
10+
case 'unset':
11+
delete target[key]
12+
break
13+
default:
14+
target[key][mutation.method](...mutation.args)
15+
}
16+
}

packages/node_modules/overmind-devtools/src/client/components/Devtools/elements.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,3 @@ export const Wrapper = styled.div`
1313
export const Container = styled.div`
1414
max-width: 500px;
1515
`
16-
17-
export const Workspace = styled.div`
18-
display: flex;
19-
flex-direction: column;
20-
`

0 commit comments

Comments
 (0)