mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
fix npm package + use our own "useDelayedState"
This commit is contained in:
@@ -11,7 +11,6 @@ import dateFormat from "dateformat";
|
||||
import { AudioPlayerService } from "renderer/services/audio-player.service";
|
||||
import { useObservable } from "renderer/hooks/use-observable.hook";
|
||||
import { map } from "rxjs/operators";
|
||||
import useDelayedState from "use-delayed-state";
|
||||
import equal from "fast-deep-equal/es6";
|
||||
import { getMapZipUrlFromHash } from "renderer/helpers/maps-utils";
|
||||
import { BsmBasicSpinner } from "../shared/bsm-basic-spinner/bsm-basic-spinner.component";
|
||||
@@ -21,6 +20,7 @@ import { MAP_DIFFICULTIES } from "renderer/partials/maps/map-difficulties/map-di
|
||||
import { MAP_DIFFICULTIES_COLORS } from "renderer/partials/maps/map-difficulties/map-difficulties-colors"
|
||||
import useDoubleClick from "use-double-click";
|
||||
import { GlowEffect } from "../shared/glow-effect.component";
|
||||
import { useDelayedState } from "renderer/hooks/use-delayed-state.hook";
|
||||
|
||||
export type ParsedMapDiff = {type: BsvMapDifficultyType, name: string, stars: number}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { useEffect, useState, useRef } from 'react'
|
||||
|
||||
// Code improved from : https://www.npmjs.com/package/use-delayed-state
|
||||
|
||||
export function useDelayedState<T = unknown>(initialState: T): [T, (newState: T, delay: number) => void, () => void] {
|
||||
|
||||
const [state, setState] = useState(initialState);
|
||||
const timeoutRef = useRef<NodeJS.Timeout>();
|
||||
|
||||
const setStateAfter = (newState: T, delay: number) => {
|
||||
if (delay === 0 || delay === undefined) {
|
||||
return setState(newState);
|
||||
}
|
||||
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
}
|
||||
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
setState(newState);
|
||||
timeoutRef.current = null;
|
||||
}, delay);
|
||||
}
|
||||
|
||||
const cancelSetState = () => {
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
timeoutRef.current = null
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timeoutRef.current) { clearTimeout(timeoutRef.current); }
|
||||
}
|
||||
}, []);
|
||||
|
||||
return [state, setStateAfter, cancelSetState]
|
||||
}
|
||||
Reference in New Issue
Block a user