Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/pure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import ReactDOMClient from 'react-dom/client'

const { debug, getElementLocatorSelectors } = utils

const activeActs = new Set<string>()
let activeActs = 0

function setActEnvironment(env: boolean | undefined): void {
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = env
}

function updateActEnvironment(): void {
setActEnvironment(activeActs > 0)
}

// @ts-expect-error unstable_act is not typed, but exported
const _act = React.act || React.unstable_act

Expand All @@ -21,17 +25,14 @@ const _act = React.act || React.unstable_act
const act = typeof _act !== 'function'
? async (cb: () => unknown) => { await cb() }
: async (cb: () => unknown) => {
setActEnvironment(true)
const actId = crypto.randomUUID()
activeActs.add(actId)
activeActs++
updateActEnvironment()
try {
await _act(cb)
}
finally {
activeActs.delete(actId)
if (!activeActs.size) {
setActEnvironment(false)
}
activeActs--
updateActEnvironment()
}
}

Expand Down