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
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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