Skip to content

Commit de762fc

Browse files
authored
refactor: Use flushSync, reduce complexity (#8)
1 parent 8aa9537 commit de762fc

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/App.tsx

+11-23
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export default function App() {
4343
useEffect(() => {
4444
let selfId: number | undefined
4545
let lastTimestamp = ""
46-
let lastAction = -1
4746
let currentZone = "Unknown"
4847

4948
let timeoutId: number | undefined = undefined
@@ -134,8 +133,7 @@ export default function App() {
134133
(actionId >= 9 && actionId <= 30000) ||
135134
actionId === SPRINT_ACTION_ID
136135
const isCraftingAction = actionId >= 100001 && actionId <= 100300
137-
const isBugOrDuplicate =
138-
logTimestamp === lastTimestamp && actionId === lastAction
136+
const isBugOrDuplicate = logTimestamp === lastTimestamp
139137
const isItem = ability.startsWith("item_")
140138

141139
if (
@@ -146,36 +144,27 @@ export default function App() {
146144
}
147145

148146
if (Date.now() - Date.parse(lastTimestamp) > 120000) {
149-
openNewEncounter() //last action > 120s ago
147+
openNewEncounter() // last action > 120s ago
150148
}
151149

152150
lastTimestamp = logTimestamp
153-
lastAction = actionId
154151

155-
let keyToRemove: string | null = null
152+
const key = logTimestamp
156153

157-
// This is pretty silly but it's the neatest way to handle the updates going
158-
// out at the same time, without finding some way to merge the action lists....
159-
ReactDOM.unstable_batchedUpdates(() => {
154+
ReactDOM.flushSync(() => {
160155
setActionList((actionList) => {
161156
const lastAction = actionList.at(-1)
162157

163-
keyToRemove = lastAction?.key ?? null
164-
165158
if (logCode === LINE_ID.NetworkCancelAbility) {
166159
return actionList.slice(0, -1)
167160
} else if (
168161
lastAction?.actionId === actionId &&
169162
lastAction?.casting
170163
) {
171164
const nextActionList = actionList.slice()
172-
nextActionList[nextActionList.length - 1] = {
173-
...lastAction,
174-
casting: false,
175-
}
165+
nextActionList[nextActionList.length - 1].casting = false
176166
return nextActionList
177167
} else {
178-
const key = logTimestamp
179168
return actionList.concat({
180169
actionId,
181170
ability,
@@ -184,6 +173,7 @@ export default function App() {
184173
})
185174
}
186175
})
176+
187177
setEncounterList((encounterList) => {
188178
if (logCode !== LINE_ID.NetworkAbility) return encounterList
189179

@@ -200,13 +190,11 @@ export default function App() {
200190
})
201191
})
202192

203-
if (keyToRemove != null) {
204-
timeoutId = window.setTimeout(() => {
205-
setActionList((actionList) =>
206-
actionList.filter((action) => action.key !== keyToRemove),
207-
)
208-
}, 10000)
209-
}
193+
timeoutId = window.setTimeout(() => {
194+
setActionList((actionList) =>
195+
actionList.filter((action) => action.key !== key),
196+
)
197+
}, 10000)
210198
}
211199
},
212200
})

0 commit comments

Comments
 (0)