Skip to content

Commit f6422ca

Browse files
committed
refactor: simplify host-effect logic
1 parent 57358d5 commit f6422ca

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

src/shared/hostEffects.js

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,39 @@
11
import { add } from './math'
22

3-
// Update the store with the time spent while committing host effects
4-
const updateStore = (store, measure) => {
5-
if (measure.name.includes('⚛')) {
6-
const measureName = measure.name.split('⚛ ').join('')
3+
const COMMITTING_HOST_EFFECTS = 'Committing Host Effects'
74

8-
if (measureName.includes('Committing Host Effects')) {
9-
var totalEffects = Number(measureName.split(':')[1].split(' ')[1])
10-
if (!store['Committing Host Effects']) {
11-
store['Committing Host Effects'] = {
12-
timeSpent: [],
13-
totalEffects: []
5+
// Update the store with the time spent while committing host effects
6+
const getCommitHostEffectsTime = measures => {
7+
const measurementsByMatchingName = measures.filter(
8+
measure =>
9+
measure.name.includes('⚛') &&
10+
measure.name
11+
.split('⚛ ')
12+
.join('')
13+
.includes(COMMITTING_HOST_EFFECTS)
14+
)
15+
16+
return {
17+
[COMMITTING_HOST_EFFECTS]: {
18+
totalEffects: measurementsByMatchingName.map(
19+
measurementByMatchingName => {
20+
const measurementName = measurementByMatchingName.name
21+
.split('⚛ ')
22+
.join('')
23+
const effectValue = measurementName.split(':')[1].split(' ')[1]
24+
return Number(effectValue)
1425
}
15-
}
16-
17-
store['Committing Host Effects'].timeSpent.push(
18-
Number(measure.duration.toFixed(2))
19-
)
20-
store['Committing Host Effects'].totalEffects.push(totalEffects)
26+
),
27+
timeSpent: measurementsByMatchingName.map(measurementByMatchingName => {
28+
const durationValue = measurementByMatchingName.duration.toFixed(2)
29+
return Number(durationValue)
30+
})
2131
}
2232
}
23-
24-
return {}
25-
}
26-
27-
const getCommitHostEffectsTime = measures => {
28-
let store = {}
29-
30-
for (const measure of measures) {
31-
updateStore(store, measure)
32-
}
33-
34-
return store
3533
}
3634

3735
// Get total number of host effects
38-
const getTotalEffects = store => {
39-
let totalEffects = 0
40-
41-
for (const measure in store) {
42-
totalEffects = totalEffects || 0
43-
totalEffects += add(store[measure].totalEffects)
44-
}
45-
46-
return Number(totalEffects.toFixed(2))
47-
}
36+
const getTotalEffects = store =>
37+
Number(add(store[COMMITTING_HOST_EFFECTS].totalEffects))
4838

4939
export { getTotalEffects, getCommitHostEffectsTime }

0 commit comments

Comments
 (0)