forked from sematic-ai/sematic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes log duplication issue (sematic-ai#722)
A bug occurs when logs are already loading, and an accidental simultaneous scroll event triggers another log pull. The second pull will append the same logs twice. According to the previous work, we can already sequentialize the pulls using a queue. So the fix is to avoid the second pull being issued if we discovered from the first pull that there would be no more logs. This PR does such, and some extras: 1. Add more loggings to understand the async queuing and execution details 2. A helper function `useRefFn` to avoid the initial value of `useRef` being re-created multiple times across rendering. (see also [here](facebook/react#14490)) 3. Convert `AsyncInvocationQueue` from a function to a class to carry more responsibilities 4. Updated unit test accordingly. --------- Co-authored-by: tscurtu <[email protected]>
- Loading branch information
Showing
4 changed files
with
107 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useRef } from 'react'; | ||
|
||
export function useRefFn<T>(initializer: () => T): T { | ||
const instanceRef = useRef<T | null>(null) | ||
|
||
function getInstance() { | ||
let instance = instanceRef.current; | ||
if (instance !== null) { | ||
return instance; | ||
} | ||
// Lazy init | ||
let newInstance = initializer(); | ||
instanceRef.current = newInstance; | ||
return newInstance; | ||
} | ||
|
||
return getInstance(); | ||
} |
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
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