-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core][data grid] Fix useTimeout clear lifecycle
- Loading branch information
1 parent
dac7c3d
commit b407d7d
Showing
4 changed files
with
56 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
export { default as useTimeout } from '@mui/utils/useTimeout'; | ||
'use client'; | ||
/** | ||
* TODO, remove this file, have dependents import from: | ||
* import useTimeout from '@mui/utils/useTimeout'; | ||
* directly. | ||
*/ | ||
import useLazyRef from '@mui/utils/useLazyRef'; | ||
import useEnhancedEffect from '@mui/utils/useEnhancedEffect'; | ||
|
||
class Timeout { | ||
static create() { | ||
return new Timeout(); | ||
} | ||
|
||
currentId: ReturnType<typeof setTimeout> | null = null; | ||
|
||
/** | ||
* Executes `fn` after `delay`, clearing any previously scheduled call. | ||
*/ | ||
start(delay: number, fn: Function) { | ||
this.clear(); | ||
this.currentId = setTimeout(() => { | ||
this.currentId = null; | ||
fn(); | ||
}, delay); | ||
} | ||
|
||
clear = () => { | ||
if (this.currentId !== null) { | ||
clearTimeout(this.currentId); | ||
this.currentId = null; | ||
} | ||
}; | ||
|
||
disposeEffect = () => { | ||
return this.clear; | ||
}; | ||
} | ||
|
||
export function useTimeout() { | ||
const timeout = useLazyRef(Timeout.create).current; | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEnhancedEffect(timeout.disposeEffect, []); | ||
|
||
return timeout; | ||
} |
This file was deleted.
Oops, something went wrong.