Skip to content

Commit

Permalink
feat: add a way to wait for query update
Browse files Browse the repository at this point in the history
fixes: #48
  • Loading branch information
Djaler committed Dec 4, 2022
1 parent 10f1b67 commit fda600a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './transformers';
export { useRouteQuery } from './useRouteQuery';
export { waitForQueryUpdate } from './queue-query-update';
6 changes: 6 additions & 0 deletions packages/core/src/queue-query-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export function queueQueryUpdate(
void queryReplaceQueue.run(currentQuery);
}

export async function waitForQueryUpdate() {
if (queryReplaceQueue) {
await queryReplaceQueue.waitForFinish();
}
}

async function updateQuery(router: Router, previousQuery: RouteQuery, query: RouteQuery) {
try {
await router.replace({
Expand Down
11 changes: 10 additions & 1 deletion packages/tests-base/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setImmediate } from 'timers';
import { expect, TestAPI } from 'vitest';
import { RouteQueryTransformer, useRouteQuery } from 'vue-use-route-query/src';
import { RouteQueryTransformer, useRouteQuery, waitForQueryUpdate } from 'vue-use-route-query/src';
import { RouteQuery } from 'vue-use-route-query/src/types';

interface MountResult<R> {
Expand Down Expand Up @@ -186,6 +186,15 @@ export function testUseRouteQuery(

expect(getCurrentQuery().array).toBe('foo,bar');
});

it('should allow to wait for query update', async () => {
const { result } = mountComposition(() => useRouteQuery('foo', null));

result.value = 'bar';
await waitForQueryUpdate();

expect(getCurrentQuery().foo).toBe('bar');
});
}

function flushPromises(): Promise<void> {
Expand Down

0 comments on commit fda600a

Please sign in to comment.