From eaaf4baa0b5d1db9e2af921fa20e0b6b5a35321c Mon Sep 17 00:00:00 2001 From: asmyshlyaev177 Date: Thu, 28 Nov 2024 23:34:21 +0400 Subject: [PATCH] refactor: refoctor queue logic --- package-lock.json | 15 ++++++++++++++ .../react-router/useUrlState/useUrlState.ts | 4 ++-- .../useUrlStateBase/useUrlStateBase.ts | 20 ++++++------------- packages/urlstate/utils.ts | 4 ++-- tests/useUrlState/main.spec.ts | 5 +++-- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4755610..9dec591 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32020,6 +32020,21 @@ "name": "state-in-url", "version": "1.0.0", "license": "ISC" + }, + "packages/example-nextjs15/node_modules/@next/swc-win32-ia32-msvc": { + "version": "14.2.16", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.16.tgz", + "integrity": "sha512-jhPl3nN0oKEshJBNDAo0etGMzv0j3q3VYorTSFqH1o3rwv1MQRdor27u1zhkgsHPNeY1jxcgyx1ZsCkDD1IHgg==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } } } } diff --git a/packages/urlstate/react-router/useUrlState/useUrlState.ts b/packages/urlstate/react-router/useUrlState/useUrlState.ts index b3379b5..ece54e8 100644 --- a/packages/urlstate/react-router/useUrlState/useUrlState.ts +++ b/packages/urlstate/react-router/useUrlState/useUrlState.ts @@ -151,9 +151,9 @@ export function useUrlState( _useHistory ? routerHistory : { - replace: (url: string, options: NavigateOptions) => + replace: (url: string, options?: NavigateOptions) => navigate(url, { ...defOpts, ...options }), - push: (url: string, options: NavigateOptions) => + push: (url: string, options?: NavigateOptions) => navigate(url, { ...defOpts, ...options }), }, [navigate], diff --git a/packages/urlstate/useUrlStateBase/useUrlStateBase.ts b/packages/urlstate/useUrlStateBase/useUrlStateBase.ts index 7f72086..38975a4 100644 --- a/packages/urlstate/useUrlStateBase/useUrlStateBase.ts +++ b/packages/urlstate/useUrlStateBase/useUrlStateBase.ts @@ -97,7 +97,6 @@ export function useUrlStateBase( const currUrl = `${window.location.pathname}${window.location.search}${window.location.hash}`; if (newUrl === currUrl) return; - let upd: (typeof queue.current)[0] | undefined; setState(newVal); const replace = options?.replace; @@ -105,21 +104,14 @@ export function useUrlStateBase( queue.current.push([replace ? "replace" : "push", newUrl, options]); clearTimeout(timer.current); - timer.current = setTimeout(() => { queueMicrotask(() => { - while (queue.current.length) { - const currUpd = queue.current.shift(); - if (!!currUpd && currUpd?.[1] !== upd?.[1]) { - upd = currUpd; - } - } - - // @ts-expect-error fots - const [method, url, opts] = upd || {}; - upd = undefined; - // @ts-expect-error fots - method && router[method](url, opts); + if (!queue.current.length) return; + const upd = queue.current.at(-1); + queue.current = []; + + const [method, url, opts] = upd || []; + router[method!](url!, opts); }); }, TIMEOUT); }, diff --git a/packages/urlstate/utils.ts b/packages/urlstate/utils.ts index afa468d..7561a40 100644 --- a/packages/urlstate/utils.ts +++ b/packages/urlstate/utils.ts @@ -129,8 +129,8 @@ export function assignValue(shape: T, newVal: Partial) { } export interface Router { - push: (href: string, opts: object) => void; - replace: (href: string, opts: object) => void; + push: (href: string, opts?: object) => void; + replace: (href: string, opts?: object) => void; } export const routerHistory: Router = { diff --git a/tests/useUrlState/main.spec.ts b/tests/useUrlState/main.spec.ts index fc3d29b..b031c23 100644 --- a/tests/useUrlState/main.spec.ts +++ b/tests/useUrlState/main.spec.ts @@ -164,13 +164,14 @@ test.describe('main tests', () => { await page.waitForSelector('button[name="Reload page"]'); - const text = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + const alpha = "abcdefghijklmnopqrstuvwxyz" + const text = alpha.repeat(5) await page .getByLabel('name') .pressSequentially(text, { delay: 0 }); await page.waitForTimeout(500); - await expect(page.url()).toContain(text) + await expect(page.url().split('?')[1]).toEqual(`name=%27${text}%27`) await expect(page.locator('button[name="Reload page"]')).toBeVisible();