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

Prevent interruptAndCheck from delaying too often when delay takes to… #1787

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thejoecode
Copy link

…o long

I ran into an issue where Firefox setTimeout(resolve, 0) was taking the majority of the time between interruptAndCheck calls.

performance.now() - current will be greater than globalInterruptionPeriod so it gets called every iteration

As a work around I can greatly increase the globalinterruptionPeriod with something like setInterruptionPeriod(150) - but then other browsers that don't have the slower setTimeout callback will have slower cancellation responses.

Other fixes I considered: have delayNextTick return performance.now() only for setTimout path (code below) or letting users provide a function that gets called back with current and they can either return current or process.now()

export function delayNextTick(current: number): Promise {
return new Promise(resolve => {
// In case we are running in a non-node environment, setImmediate isn't available.
// Using setTimeout of the browser API accomplishes the same result.
if (typeof setImmediate === 'undefined') {
// there is no way to guarantee
setTimeout(() => resolve(performance.now()), 0);
} else {
setImmediate(() => resolve(current));
}
});
}

…o long

I ran into an issue where Firefox setTimeout(resolve, 0) was taking the majority of the time between interruptAndCheck calls.

performance.now() - current will be greater than globalInterruptionPeriod so it gets called every iteration

As a work around I can greatly increase the globalinterruptionPeriod with something like setInterruptionPeriod(150) - but then other browsers that don't have the slower setTimeout callback will have slower cancellation responses.

Other fixes I considered: have delayNextTick return performance.now() only for setTimout path (code below) or letting users provide a function that gets called back with current and they can either return current or process.now()

export function delayNextTick(current: number): Promise<number> {
    return new Promise(resolve => {
        // In case we are running in a non-node environment, `setImmediate` isn't available.
        // Using `setTimeout` of the browser API accomplishes the same result.
        if (typeof setImmediate === 'undefined') {
            // there is no way to guarantee
            setTimeout(() => resolve(performance.now()), 0);
        } else {
            setImmediate(() => resolve(current));
        }
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant