mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-03 06:47:29 +03:00
Use test seams instead of DI in useTimeoutToggle
This commit is contained in:
parent
6926afbac1
commit
7a7884f38d
3 changed files with 13 additions and 14 deletions
|
@ -6,8 +6,5 @@
|
||||||
"parserOptions": {
|
"parserOptions": {
|
||||||
"project": "./tsconfig.json"
|
"project": "./tsconfig.json"
|
||||||
},
|
},
|
||||||
"ignorePatterns": ["src/service*.ts"],
|
"ignorePatterns": ["src/service*.ts"]
|
||||||
"rules": {
|
|
||||||
"react-hooks/rules-of-hooks": "off"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
import { parseQuery } from '@shlinkio/shlink-frontend-kit';
|
import { parseQuery } from '@shlinkio/shlink-frontend-kit';
|
||||||
import { useRef, useState } from 'react';
|
import { useCallback, useRef, useState } from 'react';
|
||||||
import { useLocation, useNavigate } from 'react-router-dom';
|
import { useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
const DEFAULT_DELAY = 2000;
|
const DEFAULT_DELAY = 2000;
|
||||||
|
|
||||||
export type TimeoutToggle = (initialValue?: boolean, delay?: number) => [boolean, () => void];
|
export type TimeoutToggle = typeof useTimeoutToggle;
|
||||||
|
|
||||||
export const useTimeoutToggle = (
|
export const useTimeoutToggle = (
|
||||||
setTimeout: (callback: Function, timeout: number) => number,
|
initialValue = false,
|
||||||
clearTimeout: (timer: number) => void,
|
delay = DEFAULT_DELAY,
|
||||||
): TimeoutToggle => (initialValue = false, delay = DEFAULT_DELAY) => {
|
|
||||||
|
// Test seams
|
||||||
|
setTimeout = window.setTimeout,
|
||||||
|
clearTimeout = window.clearTimeout,
|
||||||
|
): [boolean, () => void] => {
|
||||||
const [flag, setFlag] = useState<boolean>(initialValue);
|
const [flag, setFlag] = useState<boolean>(initialValue);
|
||||||
const timeout = useRef<number | undefined>(undefined);
|
const timeout = useRef<number | undefined>(undefined);
|
||||||
const callback = () => {
|
const callback = useCallback(() => {
|
||||||
setFlag(!initialValue);
|
setFlag(!initialValue);
|
||||||
|
|
||||||
if (timeout.current) {
|
if (timeout.current) {
|
||||||
|
@ -20,7 +24,7 @@ export const useTimeoutToggle = (
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout.current = setTimeout(() => setFlag(initialValue), delay);
|
timeout.current = setTimeout(() => setFlag(initialValue), delay);
|
||||||
};
|
}, [clearTimeout, delay, initialValue, setTimeout]);
|
||||||
|
|
||||||
return [flag, callback];
|
return [flag, callback];
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,7 +12,5 @@ export const provideServices = (bottle: Bottle) => {
|
||||||
bottle.constant('csvToJson', csvToJson);
|
bottle.constant('csvToJson', csvToJson);
|
||||||
bottle.constant('jsonToCsv', jsonToCsv);
|
bottle.constant('jsonToCsv', jsonToCsv);
|
||||||
|
|
||||||
bottle.constant('setTimeout', window.setTimeout);
|
bottle.serviceFactory('useTimeoutToggle', () => useTimeoutToggle);
|
||||||
bottle.constant('clearTimeout', window.clearTimeout);
|
|
||||||
bottle.serviceFactory('useTimeoutToggle', useTimeoutToggle, 'setTimeout', 'clearTimeout');
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue