Skip to content

Commit 471761a

Browse files
Merge pull request #111 from cerebral/wrappingUp
Wrapping up
2 parents ae2acf9 + eb8ad49 commit 471761a

File tree

18 files changed

+224
-94
lines changed

18 files changed

+224
-94
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styled from '../../styled-components'
33
export const Wrapper = styled.div`
44
padding: ${({ theme }) => theme.padding.normal};
55
background-color: ${({ theme }) => theme.color.fade(theme.color.white, 0.1)};
6+
overflow-y: scroll;
67
`
78

89
export const Container = styled.div`
@@ -14,3 +15,20 @@ export const Pipe = styled.div`
1415
align-items: center;
1516
margin: ${({ theme }) => `${theme.padding.small} 0`};
1617
`
18+
19+
export const ExpandAll = styled<
20+
{
21+
isActive: boolean
22+
},
23+
'div'
24+
>('div')`
25+
display: flex;
26+
justify-content: flex-end;
27+
font-size: ${({ theme }) => theme.fontSize.small};
28+
cursor: pointer;
29+
color: ${({ theme }) => theme.color.black};
30+
opacity: ${({ isActive }) => (isActive ? '1' : '0.5')};
31+
:hover {
32+
opacity: ${({ isActive }) => (isActive ? '1' : '0.75')};
33+
}
34+
`

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import { Connect, connect } from '../../app'
3-
import { Wrapper, Container } from './elements'
3+
import { Wrapper, Container, ExpandAll } from './elements'
44
import Operator from '../ActionOperator'
55
import Flush from '../ActionFlush'
66
import { getActionId, getOperatorId } from '../../app/utils'
@@ -16,6 +16,12 @@ const Action: React.SFC<Connect> = ({ app }) => {
1616
return (
1717
<Wrapper>
1818
<Container>
19+
<ExpandAll
20+
isActive={app.state.expandAllActionDetails}
21+
onClick={() => app.actions.toggleExpandAllActions()}
22+
>
23+
expand all
24+
</ExpandAll>
1925
{app.state.currentAction.operators.map((operator, index) => {
2026
const prevOperator = app.state.currentAction.operators[index - 1]
2127
const value =

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,3 @@ export const CenterWrapper = styled.div`
1818
align-items: center;
1919
justify-content: center;
2020
`
21-
22-
export const ExpandAll = styled<
23-
{
24-
isActive: boolean
25-
},
26-
'div'
27-
>('div')`
28-
display: flex;
29-
position: absolute;
30-
top: 10px;
31-
right: ${({ theme }) => theme.padding.normal};
32-
font-size: ${({ theme }) => theme.fontSize.small};
33-
cursor: pointer;
34-
color: ${({ theme }) => theme.color.black};
35-
opacity: ${({ isActive }) => (isActive ? '1' : '0.5')};
36-
:hover {
37-
opacity: ${({ isActive }) => (isActive ? '1' : '0.75')};
38-
}
39-
`

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import ActionsList from '../ActionsList'
33
import Action from '../Action'
4-
import { Wrapper, CenterWrapper, ExpandAll } from './elements'
4+
import { Wrapper, CenterWrapper } from './elements'
55
import { Connect, connect } from '../../app'
66
import Text from '../common/Text'
77

@@ -16,12 +16,6 @@ const Actions: React.SFC<Connect> = ({ app }) => {
1616

1717
return (
1818
<Wrapper>
19-
<ExpandAll
20-
isActive={app.state.expandAllActionDetails}
21-
onClick={() => app.actions.toggleExpandAllActions()}
22-
>
23-
expand all
24-
</ExpandAll>
2519
<ActionsList />
2620
<Action />
2721
</Wrapper>

packages/node_modules/overmind/src/derived.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,37 @@ describe('Derived', () => {
6666
expect(app.state.upperFoo).toBe('BAR2')
6767
expect(renderCount).toBe(1)
6868
})
69+
test('should not require flush to flag as dirty', () => {
70+
expect.assertions(1)
71+
const changeFoo: Action = ({ map }) =>
72+
map(({ state }) => state.upperFoo)
73+
.mutate((state) => (state.foo = 'bar2'))
74+
.run(({ state }) => {
75+
expect(state.upperFoo).toBe('BAR2')
76+
})
77+
78+
const derived: Derive<string> = (state: State) => state.foo.toUpperCase()
79+
80+
const config = {
81+
state: {
82+
foo: 'bar',
83+
upperFoo: derived,
84+
},
85+
actions: {
86+
changeFoo,
87+
},
88+
}
89+
type Config = TConfig<{
90+
state: {
91+
foo: string
92+
upperFoo: string
93+
}
94+
actions: typeof config.actions
95+
}>
96+
type Action<Input = void, Output = any> = TAction<Input, Output, Config>
97+
type Derive<T> = TDerive<T, Config>
98+
99+
const app = new App(config)
100+
app.actions.changeFoo()
101+
})
69102
})

packages/node_modules/overmind/src/derived.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ export default class Derived {
2121
this.value = this.cb(proxyStateTree.get())
2222
this.isDirty = false
2323
this.paths = proxyStateTree.clearPathsTracking(trackId)
24-
if (this.proxyStateTreeListener) {
25-
this.proxyStateTreeListener.update(this.paths)
26-
} else {
24+
if (!this.proxyStateTreeListener) {
2725
this.proxyStateTreeListener = proxyStateTree.addMutationListener(
28-
this.paths,
29-
(flushId) => {
30-
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
31-
path,
32-
flushId,
33-
})
34-
this.isDirty = true
26+
(mutation, flushId) => {
27+
if (this.paths.has(mutation.path)) {
28+
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
29+
path,
30+
flushId,
31+
})
32+
this.isDirty = true
33+
}
3534
}
3635
)
3736
}

packages/node_modules/overmind/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export default class App<
438438
return this.proxyStateTree.clearPathsTracking(id, cb)
439439
}
440440
addMutationListener(paths, cb) {
441-
return this.proxyStateTree.addMutationListener(paths, cb)
441+
return this.proxyStateTree.addFlushListener(paths, cb)
442442
}
443443
createReactionFactory(prefix: string) {
444444
const reactions = []

packages/node_modules/overmind/src/reaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Reaction {
1818
this.proxyStateTree.clearPathsTracking(trackId)
1919
)[0]
2020

21-
this.proxyStateTreeListener = this.proxyStateTree.addMutationListener(
21+
this.proxyStateTreeListener = this.proxyStateTree.addFlushListener(
2222
(mutations, flushId) => {
2323
for (let mutationIndex in mutations) {
2424
const mutation = mutations[mutationIndex]

packages/node_modules/proxy-state-tree/README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function render () {
102102
return tree.clearPathsTracking(trackId)
103103
}
104104

105-
const listener = tree.addMutationListener(render(), (flushId) => {
105+
const listener = tree.addFlushListener(render(), (flushId) => {
106106
// Runs when mutations matches paths passed in
107107

108108
// Whenever mutations affecting these paths occurs
@@ -126,7 +126,7 @@ listener.dispose()
126126

127127
Here we combine the tracked paths with the mutations performed to see if this components, computed or whatever indeed needs to run again, doing a new **startPathsTracking** and **clearPathsTracking**.
128128

129-
You can optionally declare a global listener which will be informed by all mutations:
129+
You can optionally declare a global listener which will be informed by all mutation flushes:
130130

131131
```js
132132
import ProxyStateTree from 'proxy-state-tree'
@@ -137,7 +137,7 @@ const tree = new ProxyStateTree({
137137
})
138138
const state = tree.get()
139139

140-
const listener = tree.addMutationListener((mutations, flushId) => {
140+
const listener = tree.addFlushListener((mutations, flushId) => {
141141
/*
142142
[{
143143
method: "set",
@@ -154,6 +154,33 @@ tree.flush()
154154
listener.dispose()
155155
```
156156

157+
In addition to this you can also listen to each individual mutation:
158+
159+
```js
160+
import ProxyStateTree from 'proxy-state-tree'
161+
162+
const tree = new ProxyStateTree({
163+
foo: 'bar',
164+
bar: 'baz'
165+
})
166+
const state = tree.get()
167+
168+
const listener = tree.addMutationListener((mutation) => {
169+
/*
170+
{
171+
method: "set",
172+
path: "foo",
173+
args: ["bar2"]
174+
}
175+
*/
176+
})
177+
178+
tree.startMutationTracking()
179+
state.foo = 'bar2'
180+
tree.clearMutationTracking()
181+
listener.dispose()
182+
```
183+
157184
## Dynamic state values
158185

159186
If you insert a function into the state tree it will be called when accessed. The function is passed the **proxy-state-tree** instance and the path of where the function lives in the tree.

0 commit comments

Comments
 (0)