Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace series function used to queue async callbacks #11485

Merged
merged 6 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions packages/native/src/__tests__/NavigationContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TabRouter,
useNavigationBuilder,
} from '@react-navigation/core';
import { act, render } from '@testing-library/react-native';
import { act, render, waitFor } from '@testing-library/react-native';
import * as React from 'react';

import { window } from '../__mocks__/window';
Expand All @@ -18,9 +18,7 @@ Object.assign(global, window);
// eslint-disable-next-line import/extensions
jest.mock('../useLinking', () => require('../useLinking.tsx'));

it('integrates with the history API', () => {
jest.useFakeTimers();

it('integrates with the history API', async () => {
const createStackNavigator = createNavigatorFactory((props: any) => {
const { state, descriptors, NavigationContent } = useNavigationBuilder(
StackRouter,
Expand Down Expand Up @@ -104,51 +102,44 @@ it('integrates with the history API', () => {

act(() => navigation.current?.navigate('Profile', { user: 'jane' }));

expect(window.location.pathname).toBe('/jane');
await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => navigation.current?.navigate('Updates'));

expect(window.location.pathname).toBe('/updates');
await waitFor(() => expect(window.location.pathname).toBe('/updates'));

act(() => navigation.current?.goBack());

jest.runAllTimers();

expect(window.location.pathname).toBe('/jane');
await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => {
window.history.back();
jest.runAllTimers();
});

expect(window.location.pathname).toBe('/feed');
await waitFor(() => expect(window.location.pathname).toBe('/feed'));

act(() => {
window.history.forward();
jest.runAllTimers();
});

expect(window.location.pathname).toBe('/jane');
await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => navigation.current?.navigate('Settings'));

expect(window.location.pathname).toBe('/edit');
await waitFor(() => expect(window.location.pathname).toBe('/edit'));

act(() => {
window.history.go(-2);
jest.runAllTimers();
});

expect(window.location.pathname).toBe('/feed');
await waitFor(() => expect(window.location.pathname).toBe('/feed'));

act(() => navigation.current?.navigate('Settings'));
act(() => navigation.current?.navigate('Chat'));

expect(window.location.pathname).toBe('/chat');
await waitFor(() => expect(window.location.pathname).toBe('/chat'));

act(() => navigation.current?.navigate('Home'));

jest.runAllTimers();

expect(window.location.pathname).toBe('/edit');
await waitFor(() => expect(window.location.pathname).toBe('/edit'));
});
32 changes: 4 additions & 28 deletions packages/native/src/useLinking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,11 @@ const findMatchingState = <T extends NavigationState>(
/**
* Run async function in series as it's called.
*/
const series = (cb: () => Promise<void>) => {
// Whether we're currently handling a callback
let handling = false;
let queue: (() => Promise<void>)[] = [];

const callback = async () => {
try {
if (handling) {
// If we're currently handling a previous event, wait before handling this one
// Add the callback to the beginning of the queue
queue.unshift(callback);
return;
}

handling = true;

await cb();
} finally {
handling = false;

if (queue.length) {
// If we have queued items, handle the last one
const last = queue.pop();

last?.();
}
}
export const series = (cb: () => Promise<void>) => {
let queue = Promise.resolve();
const callback = () => {
queue = queue.then(cb);
};

return callback;
};

Expand Down
Loading