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

Add tasks.js module #3839

Merged
merged 1 commit into from
Aug 19, 2021
Merged

Add tasks.js module #3839

merged 1 commit into from
Aug 19, 2021

Conversation

mjethani
Copy link
Contributor

@mjethani mjethani commented Aug 18, 2021

This patch adds a tasks.js file.

For now it's used only in static-net-filtering.js.

src/js/timer.js Outdated
// https://github.com/behnammodi/polyfill/blob/1a5965edc0e2eaf8e6d87902cc719462e2a889fb/window.polyfill.js

timer.requestIdleCallback = function (callback, options) {
options = options || {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill I'm not sure that all of this is useful but I've left it as it is. You had a much simpler polyfill. Let me know if you'd like to use that one.

@@ -26,6 +26,7 @@
/******************************************************************************/

import globals from './globals.js';
import timer from './timer.js';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now only this file uses it, but once all files use it then we can fix globals.js.

src/js/timer.js Outdated
const needsPolyfill = timer.requestIdleCallback === null;

if ( needsPolyfill ) {
// https://github.com/behnammodi/polyfill/blob/1a5965edc0e2eaf8e6d87902cc719462e2a889fb/window.polyfill.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code below is from the polyfill linked from here: https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep the implementation simple -- I had a look at this polyfill back when I created globals but decided against it because there was no use for uBO of more complicated than what is there, the use-case in uBO is simply to defer some operations so as to not delay readiness at launch time. I prefer your idea of a common variable allowing access to common utility functions throughout different parts of the API. For instance requestIdleCallback() could simply be a custom-named method using requestIdleCallback() underneath, but without having all the semantic of requestIdleCallback().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a simple API like this?

let id = timer.queue(myFunc);
timer.drop(id);

timer.drop() is not even necessary for now.

static-net-filtering.js does pass a timeout value so I'm wondering if that should be supported.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use a default timeout internally without requiring it to be part of the API -- the MDN doc strongly suggests the use of a timeout to avoid cases where the callback may never be called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed the file to tasks.js and it's a lot simpler now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer your idea of a common variable allowing access to common utility functions throughout different parts of the API.

We could call this file common.js if you prefer it. Afterwards, when there's a solution for location, that could go in this file too.

@mjethani mjethani changed the title Add timer.js module Add tasks.js module Aug 18, 2021
@@ -3691,18 +3692,18 @@ FilterContainer.prototype.freeze = function() {
// work properly, so defer this until later to allow for reduced delay to
// readiness when no valid selfie is available.
if ( this.optimizeTimerId === undefined ) {
this.optimizeTimerId = globals.requestIdleCallback(( ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill should I rename this to optimizeTaskId?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gorhill should I rename this to optimizeTaskId?

I've taken the liberty and done it now. I checked that it's not accessed from anywhere outside this file.

@gorhill gorhill merged commit b19393d into gorhill:master Aug 19, 2021
gorhill added a commit that referenced this pull request Aug 19, 2021
@mjethani mjethani deleted the timer-module branch August 19, 2021 11:27
gorhill added a commit that referenced this pull request Aug 19, 2021
Related discussion:
- #3839

`requestIdleCallback` can be assumed always present on
browser-related platforms.
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.

2 participants