Skip to content

Commit

Permalink
refactor: refoctor queue logic
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Nov 28, 2024
1 parent 727b2c6 commit eaaf4ba
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/urlstate/react-router/useUrlState/useUrlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ export function useUrlState<T extends JSONCompatible>(
_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],
Expand Down
20 changes: 6 additions & 14 deletions packages/urlstate/useUrlStateBase/useUrlStateBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,21 @@ export function useUrlStateBase<T extends JSONCompatible>(
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;
delete options?.replace;
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);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/urlstate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export function assignValue<T extends object>(shape: T, newVal: Partial<T>) {
}

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 = {
Expand Down
5 changes: 3 additions & 2 deletions tests/useUrlState/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eaaf4ba

Please sign in to comment.