From 36d0774231c02df0da5f04f72828a14c5c3f0f72 Mon Sep 17 00:00:00 2001 From: JohnieXu <281910378@qq.com> Date: Sun, 29 Jun 2025 18:56:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20runWithDelayed?= =?UTF-8?q?Loading=20=E6=96=B9=E6=B3=95=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/common.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/common.test.ts b/src/common.test.ts index 5812b73..1b7a39a 100644 --- a/src/common.test.ts +++ b/src/common.test.ts @@ -1,5 +1,6 @@ import Big from "big.js" import { delay, divide, floatDivide, floatMultiply, genMessageId, isEncodeURILike, isFormData, isFunction, isNormalObject, isNumber, isPromise, minus, multiply, plus, promisify, randomNumber, toNonExponential, isValidToken, isString, isUrlLike, isBlobUrlLike, isDef, isUndef, isStartWithSlash, isEndWithSlash, noop, getQuery, runWithTimeout, runWithDelayedLoading } from "./common" +import {expect} from "vitest"; describe('isNumber', () => { it('normal case', () => { @@ -847,5 +848,47 @@ describe("runWithDelayedLoading", () => { await promise // No assertions needed, just verifying it doesn't throw }) + + it("default value of loadingDelay should be 300ms", async () => { + vi.useFakeTimers() + const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) + const onLoading = vi.fn() + const onSettled = vi.fn() + + const promise = runWithDelayedLoading(mockTask, { + onLoading: onLoading, + onSettled: onSettled, + minLoadingDuration: 3000 + }) + + await vi.advanceTimersByTimeAsync(100) + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(190) + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(1000) + expect(onLoading).toHaveBeenCalledTimes(1) + await vi.advanceTimersByTimeAsync(1000 * 2) + expect(onLoading).toHaveBeenCalledTimes(1) + }) +}) + +it("default value of minLoadingDuration should be 1000ms", async () => { + vi.useFakeTimers() + const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) + const onLoading = vi.fn() + const onSettled = vi.fn() + + const promise = runWithDelayedLoading(mockTask, { + onLoading: onLoading, + onSettled: onSettled, + loadingDelay: 200, + }) + + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(300) + expect(onLoading).toBeCalledTimes(1) + await vi.advanceTimersByTimeAsync(1000) + expect(onLoading).toBeCalledTimes(1) + expect(onSettled).toBeCalledTimes(1) }) diff --git a/src/common.ts b/src/common.ts index 2171c9a..f7f377f 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1047,7 +1047,7 @@ export function runWithTimeout (fn: Function, timeout: number, context * @public */ export async function runWithDelayedLoading(asyncTask: () => Promise, { - loadingDelay = 1000, + loadingDelay = 300, minLoadingDuration = 1000, onLoading, onSettled, From 1e636eed99c615716b99834dd07d2ad400492ccd Mon Sep 17 00:00:00 2001 From: JohnieXu <281910378@qq.com> Date: Sun, 29 Jun 2025 19:06:43 +0800 Subject: [PATCH 2/2] chore: merge code & improve code style --- src/common.test.ts | 74 +++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/common.test.ts b/src/common.test.ts index 7334885..f22a736 100644 --- a/src/common.test.ts +++ b/src/common.test.ts @@ -667,7 +667,7 @@ describe('getQuery', () => { it('normal getQuery', () => { expect(getQuery('http://wewe.com?a=11&b=22&c=33', 'a')).toEqual('11') expect(getQuery('http://wewe.com?a=11&b=22&c=33', 'y')).toEqual(undefined) - expect(getQuery('https://www.baidu.com/s?ie=UTF-8&wd=%E8%AE%A2%E5%8D%95', 'wd')).toEqual('%E8%AE%A2%E5%8D%95') + expect(getQuery('https://www.baidu.com/s?ie=UTF-8&wd=%E8%AE%A2%E5%8D%95', 'wd')).toEqual('%E8%AE%A2%E5%8D%95') }) it('not normal getQuery', () => { expect(getQuery('http://wewe.com?a=11&b=22&c=33','b')).not.to.equal('1') @@ -686,7 +686,7 @@ describe('runWithTimeout', () => { const promiseFn = function () { return new Promise((resolve)=>{ setTimeout(()=>{ - resolve('Success') + resolve('Success') }, 2000) }) } @@ -896,46 +896,46 @@ describe("runWithDelayedLoading", () => { expect(onSettled).toBeCalledTimes(1) }) - it("default value of loadingDelay should be 300ms", async () => { - vi.useFakeTimers() - const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) - const onLoading = vi.fn() - const onSettled = vi.fn() - - const promise = runWithDelayedLoading(mockTask, { - onLoading: onLoading, - onSettled: onSettled, - minLoadingDuration: 3000 - }) + it("default value of loadingDelay should be 300ms", async () => { + vi.useFakeTimers() + const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) + const onLoading = vi.fn() + const onSettled = vi.fn() - await vi.advanceTimersByTimeAsync(100) - expect(onLoading).not.toHaveBeenCalled() - await vi.advanceTimersByTimeAsync(190) - expect(onLoading).not.toHaveBeenCalled() - await vi.advanceTimersByTimeAsync(1000) - expect(onLoading).toHaveBeenCalledTimes(1) - await vi.advanceTimersByTimeAsync(1000 * 2) - expect(onLoading).toHaveBeenCalledTimes(1) + const promise = runWithDelayedLoading(mockTask, { + onLoading: onLoading, + onSettled: onSettled, + minLoadingDuration: 3000 }) - it("default value of minLoadingDuration should be 1000ms", async () => { - vi.useFakeTimers() - const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) - const onLoading = vi.fn() - const onSettled = vi.fn() + await vi.advanceTimersByTimeAsync(100) + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(190) + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(1000) + expect(onLoading).toHaveBeenCalledTimes(1) + await vi.advanceTimersByTimeAsync(1000 * 2) + expect(onLoading).toHaveBeenCalledTimes(1) + }) - const promise = runWithDelayedLoading(mockTask, { - onLoading: onLoading, - onSettled: onSettled, - loadingDelay: 200, - }) + it("default value of minLoadingDuration should be 1000ms", async () => { + vi.useFakeTimers() + const mockTask = vi.fn(() => new Promise(resolve => { setTimeout(() => { resolve("result") }, 300) })) + const onLoading = vi.fn() + const onSettled = vi.fn() - expect(onLoading).not.toHaveBeenCalled() - await vi.advanceTimersByTimeAsync(300) - expect(onLoading).toBeCalledTimes(1) - await vi.advanceTimersByTimeAsync(1000) - expect(onLoading).toBeCalledTimes(1) - expect(onSettled).toBeCalledTimes(1) + const promise = runWithDelayedLoading(mockTask, { + onLoading: onLoading, + onSettled: onSettled, + loadingDelay: 200, }) + + expect(onLoading).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(300) + expect(onLoading).toBeCalledTimes(1) + await vi.advanceTimersByTimeAsync(1000) + expect(onLoading).toBeCalledTimes(1) + expect(onSettled).toBeCalledTimes(1) + }) })