Skip to content

Commit

Permalink
refactor: Use flushSync, reduce complexity (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyronald authored Apr 9, 2024
1 parent 8aa9537 commit de762fc
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export default function App() {
useEffect(() => {
let selfId: number | undefined
let lastTimestamp = ""
let lastAction = -1
let currentZone = "Unknown"

let timeoutId: number | undefined = undefined
Expand Down Expand Up @@ -134,8 +133,7 @@ export default function App() {
(actionId >= 9 && actionId <= 30000) ||
actionId === SPRINT_ACTION_ID
const isCraftingAction = actionId >= 100001 && actionId <= 100300
const isBugOrDuplicate =
logTimestamp === lastTimestamp && actionId === lastAction
const isBugOrDuplicate = logTimestamp === lastTimestamp
const isItem = ability.startsWith("item_")

if (
Expand All @@ -146,36 +144,27 @@ export default function App() {
}

if (Date.now() - Date.parse(lastTimestamp) > 120000) {
openNewEncounter() //last action > 120s ago
openNewEncounter() // last action > 120s ago
}

lastTimestamp = logTimestamp
lastAction = actionId

let keyToRemove: string | null = null
const key = logTimestamp

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

keyToRemove = lastAction?.key ?? null

if (logCode === LINE_ID.NetworkCancelAbility) {
return actionList.slice(0, -1)
} else if (
lastAction?.actionId === actionId &&
lastAction?.casting
) {
const nextActionList = actionList.slice()
nextActionList[nextActionList.length - 1] = {
...lastAction,
casting: false,
}
nextActionList[nextActionList.length - 1].casting = false
return nextActionList
} else {
const key = logTimestamp
return actionList.concat({
actionId,
ability,
Expand All @@ -184,6 +173,7 @@ export default function App() {
})
}
})

setEncounterList((encounterList) => {
if (logCode !== LINE_ID.NetworkAbility) return encounterList

Expand All @@ -200,13 +190,11 @@ export default function App() {
})
})

if (keyToRemove != null) {
timeoutId = window.setTimeout(() => {
setActionList((actionList) =>
actionList.filter((action) => action.key !== keyToRemove),
)
}, 10000)
}
timeoutId = window.setTimeout(() => {
setActionList((actionList) =>
actionList.filter((action) => action.key !== key),
)
}, 10000)
}
},
})
Expand Down

0 comments on commit de762fc

Please sign in to comment.