-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exract async code to separate classes (#6258)
## Summary Previously to use function `runOnUIBlocking` we need a reference to TestRunner. I wanted to use `runOnUIBlocking` while implementing some other feature (`expect(()=>{}).toThrow()`) and found it a bit troublesome. Therefore I've decided to extract this code into separate class first. ## Test plan
- Loading branch information
Showing
4 changed files
with
77 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
apps/common-app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/SyncUIRunner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { runOnJS, runOnUI } from 'react-native-reanimated'; | ||
import type { LockObject } from './types'; | ||
|
||
class WaitForUnlock { | ||
private _lock: LockObject = { | ||
lock: false, | ||
}; | ||
|
||
_setLock(value: boolean) { | ||
this._lock = { lock: value }; | ||
} | ||
|
||
_waitForUnlock(maxWaitTime?: number) { | ||
return new Promise(resolve => { | ||
const startTime = performance.now(); | ||
const interval = setInterval(() => { | ||
const currentTime = performance.now(); | ||
const waitTimeExceeded = maxWaitTime && maxWaitTime < currentTime - startTime; | ||
if (this._lock.lock !== true || waitTimeExceeded) { | ||
clearInterval(interval); | ||
resolve(this._lock.lock); | ||
} | ||
}, 10); | ||
}); | ||
} | ||
} | ||
|
||
export class SyncUIRunner extends WaitForUnlock { | ||
public async runOnUIBlocking(worklet: () => void, maxWaitTime?: number) { | ||
const unlock = () => this._setLock(false); | ||
this._setLock(true); | ||
runOnUI(() => { | ||
'worklet'; | ||
worklet(); | ||
runOnJS(unlock)(); | ||
})(); | ||
await this._waitForUnlock(maxWaitTime); | ||
} | ||
} | ||
|
||
export class RenderLock extends WaitForUnlock { | ||
public lock() { | ||
this._setLock(true); | ||
} | ||
|
||
public unlock() { | ||
this._setLock(false); | ||
} | ||
|
||
public async waitForUnlock(maxWaitTime?: number) { | ||
await this._waitForUnlock(maxWaitTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters