完成80%
This commit is contained in:
@@ -9,16 +9,16 @@ const requireFunction = (value, name) => {
|
||||
};
|
||||
|
||||
export const createAuthSmsCooldown = ({
|
||||
sceneCode,
|
||||
operationCode,
|
||||
onChange,
|
||||
now = Date.now,
|
||||
setIntervalFn = setInterval,
|
||||
clearIntervalFn = clearInterval,
|
||||
}) => {
|
||||
if (typeof sceneCode !== "string" || !sceneCode.trim()) {
|
||||
throw new TypeError("sceneCode must be a non-empty string");
|
||||
if (typeof operationCode !== "string" || !operationCode.trim()) {
|
||||
throw new TypeError("operationCode must be a non-empty string");
|
||||
}
|
||||
const normalizedSceneCode = sceneCode.trim();
|
||||
const normalizedOperationCode = operationCode.trim();
|
||||
const publish = requireFunction(onChange, "onChange");
|
||||
const readNow = requireFunction(now, "now");
|
||||
const scheduleInterval = requireFunction(setIntervalFn, "setIntervalFn");
|
||||
@@ -32,13 +32,13 @@ export const createAuthSmsCooldown = ({
|
||||
};
|
||||
|
||||
const sync = () => {
|
||||
const expiresAt = sceneExpiresAt.get(normalizedSceneCode) || 0;
|
||||
const expiresAt = sceneExpiresAt.get(normalizedOperationCode) || 0;
|
||||
const remaining = Math.max(
|
||||
0,
|
||||
Math.ceil((expiresAt - Number(readNow())) / 1000),
|
||||
);
|
||||
if (remaining === 0) {
|
||||
sceneExpiresAt.delete(normalizedSceneCode);
|
||||
sceneExpiresAt.delete(normalizedOperationCode);
|
||||
stopTimer();
|
||||
} else if (timer === null) {
|
||||
timer = scheduleInterval(sync, 1000);
|
||||
@@ -52,7 +52,7 @@ export const createAuthSmsCooldown = ({
|
||||
throw new TypeError("cooldown seconds must be an integer from 1 to 300");
|
||||
}
|
||||
sceneExpiresAt.set(
|
||||
normalizedSceneCode,
|
||||
normalizedOperationCode,
|
||||
Number(readNow()) + seconds * 1000,
|
||||
);
|
||||
return sync();
|
||||
|
||||
Reference in New Issue
Block a user