Skip to content

Commit 3998ae4

Browse files
fix(overmind): flush handling with devtools
1 parent 08bc94e commit 3998ae4

File tree

10 files changed

+185
-85
lines changed

10 files changed

+185
-85
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
FaVolumeDown,
1616
FaVolumeUp,
1717
FaDatabase,
18+
FaClock,
19+
FaCheck,
1820
} from 'react-icons/fa'
1921

2022
const History: FunctionComponent = () => {
@@ -53,25 +55,43 @@ const History: FunctionComponent = () => {
5355
<div className={styles.label}>
5456
<FaSave />
5557
</div>
58+
<div className={styles.flushActionName}>
59+
{flushRecord.data.actionName}
60+
</div>
5661
<div className={styles.flushDetail}>
5762
{flushRecord.data.components.length} <FaCode />
63+
{flushRecord.data.components.length ? (
64+
<div className={styles.flushPopup}>
65+
{flushRecord.data.components.join(', ')}
66+
</div>
67+
) : null}
5868
</div>
5969
<div className={styles.flushDetail}>
6070
{flushRecord.data.derived.length} <FaLink />
71+
{flushRecord.data.derived.length ? (
72+
<div className={styles.flushPopup}>
73+
{flushRecord.data.derived.join(', ')}
74+
</div>
75+
) : null}
6176
</div>
6277
</div>
6378
)
6479
case HistoryRecordType.Effect:
6580
const effectRecord = record as EffectHistoryRecord
6681

82+
// If the effect was sync, we do not show both start and end
83+
if (
84+
state.history[index - 1] &&
85+
state.history[index - 1].data.effectId ===
86+
effectRecord.data.effectId
87+
) {
88+
return null
89+
}
90+
6791
return (
6892
<div className={styles.recordWrapper} key={index}>
6993
<div className={styles.label}>
70-
{effectRecord.data.isPending ? (
71-
<FaVolumeDown />
72-
) : (
73-
<FaVolumeUp />
74-
)}
94+
{effectRecord.data.isPending ? <FaClock /> : <FaCheck />}
7595
</div>
7696
<div className={styles.effectPath}>
7797
{effectRecord.data.name

packages/node_modules/overmind-devtools/src/components/History/styles.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@ export const wrapper = css({
1111

1212
export const label = css({
1313
padding: '0 0.5rem',
14-
marginRight: '0.5rem',
1514
color: colors.blue,
1615
opacity: 0.25,
1716
display: 'flex',
1817
alignItems: 'center',
1918
})
2019

20+
export const addComponent = css({
21+
color: colors.green,
22+
padding: '0 0.5rem',
23+
})
24+
25+
export const updateComponent = css({
26+
color: colors.white2,
27+
padding: '0 0.5rem',
28+
})
29+
30+
export const removeComponent = css({
31+
color: colors.red,
32+
padding: '0 0.5rem',
33+
})
34+
2135
export const recordWrapper = css({
2236
display: 'flex',
2337
alignItems: 'center',
@@ -44,12 +58,35 @@ export const arg = css({
4458
})
4559

4660
export const flushDetail = css({
61+
position: 'relative',
4762
display: 'flex',
4863
margin: '0 0.5rem',
4964
fontSize: 12,
5065
alignItems: 'center',
5166
color: colors.white,
67+
cursor: 'default',
5268
'> svg': {
5369
marginLeft: '0.25rem',
5470
},
71+
':hover > div': {
72+
display: 'block',
73+
},
74+
})
75+
export const flushActionName = css({
76+
padding: '0 0.5rem',
77+
})
78+
79+
export const flushPopup = css({
80+
position: 'absolute',
81+
boxShadow: '0px 10px 13px 0px rgba(0,0,0,0.1)',
82+
display: 'none',
83+
width: 300,
84+
zIndex: 2,
85+
borderRadius: 3,
86+
fontSize: 14,
87+
top: '100%',
88+
left: 0,
89+
backgroundColor: colors.dark,
90+
color: colors.white,
91+
padding: '0.5rem',
5592
})

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
Flush,
88
GroupedComponents,
99
Tab,
10-
AppMessageType,
1110
HistoryRecord,
1211
ExecutionType,
1312
MutationsMessage,
@@ -135,9 +134,18 @@ const state: State = {
135134
)
136135
case ExecutionType.FLUSH:
137136
const flushMessage = message as FlushMessage
137+
const flush = state.currentApp.flushes[flushMessage.data.flushId]
138138
const flushRecord: FlushHistoryRecord = {
139139
type: HistoryRecordType.Flush,
140-
data: state.currentApp.flushes[flushMessage.data.flushId],
140+
data: {
141+
...flush,
142+
components: flush.components.map(
143+
(componentId) => state.currentApp.components[componentId].name
144+
),
145+
derived: flush.derived.map(
146+
(derivedId) => state.currentApp.derived[derivedId]
147+
),
148+
},
141149
}
142150

143151
return aggr.concat(flushRecord)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ export type DerivedMap = {
132132
export enum HistoryRecordType {
133133
Mutation = 'Mutation',
134134
Effect = 'Effect',
135-
PendingEffect = 'PendingEffect',
136-
AddComponent = 'AddComponent',
137-
UpdateComponent = 'UpdateComponent',
138-
RemoveComponent = 'RemoveComponent',
139135
Flush = 'Flush',
140136
}
141137

packages/node_modules/overmind-react/src/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ export const createHook = <Config extends IConfiguration>(
7474
? ReactCurrentOwner.current.elementType
7575
: {}
7676
}
77-
const useForceRerender = () => {
78-
const [, setState] = useState(true)
77+
const useForceRerender = (cb?: () => void) => {
78+
const [, setState] = useState(() => {
79+
cb && cb()
80+
return true
81+
})
7982
const forceRerender = (_, __, flushId): void => {
8083
setState(flushId)
8184
}
@@ -103,12 +106,13 @@ export const createHook = <Config extends IConfiguration>(
103106
? nextComponentId++
104107
: component.__componentId
105108

106-
const rerenderComponent = useForceRerender()
107109
const { current: tree } = useRef<any>(
108110
(overmind as any).proxyStateTree.getTrackStateTree()
109111
)
110112

111113
if (IS_PRODUCTION) {
114+
const rerenderComponent = useForceRerender()
115+
112116
tree.track(rerenderComponent)
113117

114118
useEffect(
@@ -124,6 +128,15 @@ export const createHook = <Config extends IConfiguration>(
124128
currentComponentInstanceId++
125129
)
126130

131+
const rerenderComponent = useForceRerender(() => {
132+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
133+
componentId: component.__componentId,
134+
componentInstanceId,
135+
name,
136+
paths: Array.from(tree.pathDependencies) as any,
137+
})
138+
})
139+
127140
tree.track((_, __, flushId) => {
128141
rerenderComponent(_, __, flushId)
129142
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
@@ -138,12 +151,6 @@ export const createHook = <Config extends IConfiguration>(
138151
useLayoutEffect(() => tree.stopTracking())
139152

140153
useEffect(() => {
141-
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
142-
componentId: component.__componentId,
143-
componentInstanceId,
144-
name,
145-
paths: Array.from(tree.pathDependencies) as any,
146-
})
147154
return () => {
148155
;(overmind as any).proxyStateTree.disposeTree(tree)
149156
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {

packages/node_modules/overmind/src/derived.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
ProxyStateTree,
44
VALUE,
55
IS_PROXY,
6-
ITrackCallback,
76
ITrackStateTree,
87
TrackStateTree,
98
} from 'proxy-state-tree'
@@ -41,18 +40,20 @@ export class Derived {
4140
return
4241
}
4342

44-
for (let path of paths) {
45-
if (this.paths.has(path)) {
43+
for (let mutationPath of paths) {
44+
if (this.paths.has(mutationPath)) {
45+
this.isDirty = true
4646
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
47-
path,
47+
derivedPath: path,
48+
path: mutationPath,
4849
flushId,
4950
})
50-
this.isDirty = true
5151
return
5252
}
5353
}
5454
})
5555
}
56+
5657
if (
5758
this.isDirty ||
5859
(this.value &&

0 commit comments

Comments
 (0)