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] An alternative setInterval that does not use a Worker #1402

Merged
merged 1 commit into from
Mar 7, 2024

Conversation

xeolabs
Copy link
Member

@xeolabs xeolabs commented Mar 7, 2024

This PR uses an alternative technique for implementing a custom setInterval that runs in a background tab, for the purpose of loading models in the background.

Previously we used a Worker, but one user reports security issues using the Worker behind Apache (see xeokit/xeokit-bim-viewer#168), so we needed to change to a different technique that does not use a Worker.

Before

class WorkerInterval {
    worker = null;

    constructor(callback, interval) {
        const blob = new Blob([`setInterval(() => postMessage(0), ${interval});`]);
        const workerScript = URL.createObjectURL(blob);
        this.worker = new Worker(workerScript);
        this.worker.onmessage = callback;
    }

    stop() {
        this.worker.terminate();
    }
}

const interval = new WorkerInterval(frame, 100);

After

function customSetInterval(callback, interval) {
    let expected = Date.now() + interval;
    function loop() {
        const elapsed = Date.now() - expected;
        callback();
        expected += interval;
        setTimeout(loop, Math.max(0, interval - elapsed));
    }
    loop();
    return {
        cancel: function() {
            // No need to do anything, setTimeout cannot be directly cancelled
        }
    };
}

customSetInterval(() => {
    frame();
}, 100);

@xeolabs xeolabs added this to the 2.5.3 milestone Mar 7, 2024
@xeolabs xeolabs merged commit f5d5ccf into master Mar 7, 2024
2 of 4 checks passed
@xeolabs xeolabs changed the title An alternative setInterval that does not use a Worker [FIX] An alternative setInterval that does not use a Worker Apr 21, 2024
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