pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { default } from './useTimeout';
|
||||
export { Timeout } from './useTimeout';
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default } from './useTimeout';
|
||||
export { Timeout } from './useTimeout';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useTimeout/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
export declare class Timeout {
|
||||
static create(): Timeout;
|
||||
currentId: ReturnType<typeof setTimeout> | null;
|
||||
/**
|
||||
* Executes `fn` after `delay`, clearing any previously scheduled call.
|
||||
*/
|
||||
start(delay: number, fn: Function): void;
|
||||
clear: () => void;
|
||||
disposeEffect: () => () => void;
|
||||
}
|
||||
export default function useTimeout(): Timeout;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use client';
|
||||
|
||||
import useLazyRef from '../useLazyRef/useLazyRef';
|
||||
import useOnMount from '../useOnMount/useOnMount';
|
||||
export class Timeout {
|
||||
constructor() {
|
||||
this.currentId = null;
|
||||
this.clear = () => {
|
||||
if (this.currentId !== null) {
|
||||
clearTimeout(this.currentId);
|
||||
this.currentId = null;
|
||||
}
|
||||
};
|
||||
this.disposeEffect = () => {
|
||||
return this.clear;
|
||||
};
|
||||
}
|
||||
static create() {
|
||||
return new Timeout();
|
||||
}
|
||||
/**
|
||||
* Executes `fn` after `delay`, clearing any previously scheduled call.
|
||||
*/
|
||||
start(delay, fn) {
|
||||
this.clear();
|
||||
this.currentId = setTimeout(() => {
|
||||
this.currentId = null;
|
||||
fn();
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
export default function useTimeout() {
|
||||
const timeout = useLazyRef(Timeout.create).current;
|
||||
useOnMount(timeout.disposeEffect);
|
||||
return timeout;
|
||||
}
|
||||
Reference in new issue
Block a user