Skip to content

Commit fe3ffc2

Browse files
Merge pull request cerebral#104 from cerebral/removeComputedAndFixDerived
feat(overmind): make derived work as plain function and remove computed
2 parents a986d6c + fdbcd20 commit fe3ffc2

File tree

18 files changed

+105
-517
lines changed

18 files changed

+105
-517
lines changed

packages/demos/react-typescript-todomvc/src/app/derived.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/demos/react-typescript-todomvc/src/app/state.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { derive } from 'overmind'
2-
import * as derived from './derived'
1+
import { Derive } from 'overmind'
32

43
export type Todo = {
54
id: string
@@ -11,4 +10,4 @@ export const todos: Todo[] = []
1110

1211
export let newTodoTitle: string = ''
1312

14-
export const count: number = derive(derived.count)
13+
export const count: Derive<number> = (state) => state.todos.length

packages/node_modules/overmind-devtools/src/app/derived.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/node_modules/overmind-devtools/src/app/mutations.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,7 @@ export const performMutationsByMessageType: Mutate<Message> = (
176176
const path = clientMessage.data.path.split('.')
177177
const key = path.pop()
178178
const target = path.reduce((aggr, pathKey) => aggr[pathKey], appState)
179-
target[key] = {
180-
paths: clientMessage.data.paths,
181-
updateCount: clientMessage.data.updateCount,
182-
value: clientMessage.data.value,
183-
}
179+
target[key] = clientMessage.data.value
184180

185181
state.apps[message.port].derived[clientMessage.data.path] =
186182
clientMessage.data
@@ -197,31 +193,6 @@ export const performMutationsByMessageType: Mutate<Message> = (
197193
].derived.push(clientMessage.data.path)
198194
break
199195
}
200-
case 'computed': {
201-
const appState = state.apps[message.port].state
202-
const path = clientMessage.data.path.split('.')
203-
const key = path.pop()
204-
const target = path.reduce((aggr, pathKey) => aggr[pathKey], appState)
205-
target[key] = {
206-
cache: clientMessage.data.cache,
207-
limit: clientMessage.data.limit,
208-
updateCount: clientMessage.data.updateCount,
209-
}
210-
211-
state.apps[message.port].computed[clientMessage.data.path] =
212-
clientMessage.data
213-
break
214-
}
215-
case 'computed:dirty': {
216-
ensureFlushExists(
217-
state.apps[message.port].flushes,
218-
clientMessage.data.flushId
219-
)
220-
state.apps[message.port].flushes[
221-
clientMessage.data.flushId
222-
].computed.push(clientMessage.data.path)
223-
break
224-
}
225196
case 'reaction:update': {
226197
ensureFlushExists(
227198
state.apps[message.port].flushes,
Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { derive } from 'overmind'
2-
import * as derived from './derived'
31
import { Apps, Tab, App, Component, Flush, Action } from './types'
2+
import { Derive } from 'overmind'
43

54
export let isConnecting: boolean = true
65

@@ -20,28 +19,54 @@ export let currentTab: Tab = Tab.State
2019

2120
export let expandedStatePaths: string[] = ['']
2221

23-
export const currentApp: App = derive(derived.currentApp)
24-
25-
export const componentsMounted: Component[] = derive(derived.componentsMounted)
26-
27-
export const componentsUpdateCount: number = derive(
28-
derived.componentsUpdateCount
29-
)
30-
31-
export const componentsStatePathCount: number = derive(
32-
derived.componentsStatePathCount
33-
)
34-
35-
export const flushes: Flush[] = derive(derived.flushes)
36-
37-
export const flushesMutationsCount: number = derive(
38-
derived.flushesMutationsCount
39-
)
40-
41-
export const flushesStatePathCount: number = derive(
42-
derived.flushesStatePathCount
43-
)
44-
45-
export const currentAction: Action = derive(derived.currentAction)
22+
export const currentApp: Derive<App> = (state) => state.apps[state.currentPort]
23+
24+
export const componentsMounted: Derive<Component[]> = (state) =>
25+
Object.keys(state.currentApp.components).reduce(
26+
(aggr, key) => {
27+
if (state.currentApp.components[key].isMounted) {
28+
return aggr.concat(state.currentApp.components[key])
29+
}
30+
31+
return aggr
32+
},
33+
[] as Component[]
34+
)
35+
36+
export const componentsUpdateCount: Derive<number> = (state) =>
37+
state.componentsMounted.reduce(
38+
(aggr, component) => aggr + component.updateCount,
39+
0
40+
)
41+
42+
export const componentsStatePathCount: Derive<number> = (state) =>
43+
state.componentsMounted.reduce(
44+
(aggr, component) => aggr + component.paths.length,
45+
0
46+
)
47+
48+
export const flushes: Derive<Flush[]> = (state) =>
49+
Object.keys(state.currentApp.flushes)
50+
.sort(
51+
(idA, idB) =>
52+
state.currentApp.flushes[idB].flushId -
53+
state.currentApp.flushes[idA].flushId
54+
)
55+
.map((id) => state.currentApp.flushes[id])
56+
57+
export const flushesMutationsCount: Derive<number> = (state) =>
58+
state.flushes.reduce((aggr, flush) => aggr + flush.mutations.length, 0)
59+
60+
export const flushesStatePathCount: Derive<number> = (state) =>
61+
state.flushes.reduce((aggr, flush) => {
62+
return flush.mutations.reduce(
63+
(aggr, mutation) =>
64+
aggr.includes(mutation.path) ? aggr : aggr.concat(mutation.path),
65+
aggr
66+
)
67+
}, []).length
68+
69+
export const currentAction: Derive<Action> = (state) =>
70+
state.currentApp.actions[state.currentApp.currentActionId]
4671

4772
export let expandAllActionDetails: boolean = false

packages/node_modules/overmind-devtools/src/app/types.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export type Flush = {
6565
mutations: Mutation[]
6666
components: string[]
6767
derived: string[]
68-
computed: string[]
6968
reactions: string[]
7069
}
7170

@@ -94,17 +93,6 @@ export type Derived = {
9493
value: any
9594
}
9695

97-
export type Computed = {
98-
cacheKeyIndex: number
99-
cacheKeysCount: number
100-
flushId: number
101-
limit: number
102-
path: string
103-
paths: string[]
104-
updateCount: number
105-
value: number
106-
}
107-
10896
export type Components = {
10997
[id: string]: Component
11098
}
@@ -113,18 +101,13 @@ export type DerivedMap = {
113101
[path: string]: Derived
114102
}
115103

116-
export type ComputedMap = {
117-
[path: string]: Computed
118-
}
119-
120104
export type App = {
121105
name: string
122106
port: string
123107
messages: AppMessage[]
124108
state: object
125109
components: Components
126110
derived: Derived
127-
computed: Computed
128111
flushes: Flushes
129112
flushByActionId: {
130113
[id: string]: FlushReference

packages/node_modules/overmind-devtools/src/app/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export const createApp = (data: Partial<App>): App =>
3838
state: {},
3939
components: {},
4040
derived: {},
41-
computed: {},
4241
flushes: {},
4342
actions: {},
4443
actionsList: [],
@@ -65,7 +64,6 @@ export const ensureFlushExists = (flushes, flushId) => {
6564
mutations: [],
6665
components: [],
6766
derived: [],
68-
computed: [],
6967
reactions: [],
7068
}
7169
flushes[flushId] = flush

packages/node_modules/overmind-devtools/src/components/ActionFlush/index.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ const ActionFlush: React.SFC<Props> = ({ flush, flushReference, app }) => {
1616
const isExpanded =
1717
app.state.expandAllActionDetails || !flushReference.isCollapsed
1818

19-
return flush.components.length ||
20-
flush.computed.length ||
21-
flush.derived.length ? (
19+
return flush.components.length || flush.derived.length ? (
2220
<Pipe>
2321
<Path />
2422
<Flush>
@@ -31,9 +29,6 @@ const ActionFlush: React.SFC<Props> = ({ flush, flushReference, app }) => {
3129
<Text variant="hint">
3230
<Icon>chain</Icon> {flush.derived.length}
3331
</Text>
34-
<Text variant="hint">
35-
<Icon>calculator</Icon> {flush.computed.length}
36-
</Text>
3732
<Text variant="hint">
3833
<Icon>flask</Icon> {flush.reactions.length}
3934
</Text>
@@ -58,13 +53,6 @@ const ActionFlush: React.SFC<Props> = ({ flush, flushReference, app }) => {
5853
</Text>
5954
</div>
6055
))}
61-
{flush.computed.map((computedPath) => (
62-
<div key={computedPath}>
63-
<Text variant="hint">
64-
<Icon>calculator</Icon> {computedPath}
65-
</Text>
66-
</div>
67-
))}
6856
{flush.reactions.map((reactionPath) => (
6957
<div key={reactionPath}>
7058
<Text variant="hint">

packages/node_modules/overmind-devtools/src/components/State/elements.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ export const LabelWrapper = styled.div`
1616
align-items: center;
1717
`
1818

19-
export const ComputedLabel = styled.div`
20-
margin-right: 5px;
21-
background-color: ${({ theme }) => theme.color.primary};
22-
padding: ${({ theme }) => `0 ${theme.padding.smallerer}`};
23-
border-radius: ${({ theme }) => theme.borderRadius.normal};
24-
color: ${({ theme }) => theme.color.white};
25-
`
26-
2719
export const DerivedLabel = styled.div`
2820
margin-right: 5px;
2921
background-color: ${({ theme }) => theme.color.primary};

packages/node_modules/overmind-devtools/src/components/State/index.tsx

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import * as React from 'react'
22
import { connect, Connect } from '../../app'
3-
import {
4-
Wrapper,
5-
Label,
6-
LabelWrapper,
7-
ComputedLabel,
8-
DerivedLabel,
9-
} from './elements'
3+
import { Wrapper, Label, LabelWrapper, DerivedLabel } from './elements'
104
import Inspector, { RenderPaths } from '../Inspector'
115
import Text from '../common/Text'
126

@@ -23,20 +17,6 @@ const DerivedWrapper = ({ children }) => (
2317
</Label>
2418
)
2519

26-
const ComputedWrapper = ({ children }) => (
27-
<Label>
28-
<LabelWrapper>
29-
<ComputedLabel>
30-
{' '}
31-
<Text variant="hint" mono>
32-
computed
33-
</Text>
34-
</ComputedLabel>
35-
</LabelWrapper>
36-
<div>{children}</div>
37-
</Label>
38-
)
39-
4020
const State: React.SFC<Connect> = ({ app }) => (
4121
<Wrapper>
4222
<Inspector
@@ -54,15 +34,6 @@ const State: React.SFC<Connect> = ({ app }) => (
5434
),
5535
}),
5636
{}
57-
),
58-
Object.keys(app.state.currentApp.computed || {}).reduce(
59-
(aggr, key) =>
60-
Object.assign(aggr, {
61-
[key]: (children) => (
62-
<ComputedWrapper key={key}>{children}</ComputedWrapper>
63-
),
64-
}),
65-
{}
6637
)
6738
) as RenderPaths
6839
}

0 commit comments

Comments
 (0)