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 elementReady-like helper for DOM detections #85

Open
fregante opened this issue Jul 9, 2021 · 3 comments
Open

Add elementReady-like helper for DOM detections #85

fregante opened this issue Jul 9, 2021 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@fregante
Copy link
Member

fregante commented Jul 9, 2021

Sometimes we want to test whether a page matches before it's done downloading, this can be done by passing a selector to element-ready.

I have 2 ideas:

Static definitions

Instead of being just a function, we can define the detections as a list of constraints like:

- const canUserEditOrg = (url) => isOrganizationProfile() && exists('.pagehead-tabs-item[href$="/settings/profile"]')
+ const canUserEditOrgDefinition = [isOrganizationProfile, '.pagehead-tabs-item[href$="/settings/profile"]']
+ export canUserEditOrg = solveConstraints(canUserEditOrgDefinition);
+ export canUserEditOrgAsync = solveConstraintsAsync(canUserEditOrgDefinition);

But this gets wordy FAST

Duplicate functions

  const canUserEditOrg = (url) => isOrganizationProfile() && exists('.pagehead-tabs-item[href$="/settings/profile"]')
+ const canUserEditOrgAsync = async (url) => isOrganizationProfile() && await existsReady('.pagehead-tabs-item[href$="/settings/profile"]')
@fregante fregante added enhancement New feature or request help wanted Extra attention is needed labels Nov 23, 2021
@fregante
Copy link
Member Author

fregante commented Jul 11, 2022

Wrapper idea

ElementReady is nothing but a requestAnimationFrame-based loop that calls querySelector until found. This means we can offer a simple wrapper that does this with any detection:

import {wait, canEditSidebar} from '';

function init() {
  if(!await wait(canEditSidebar)) 
    return false
}
  • Pure DOM-based detections would be perfect this way;
  • Pure URL-based detections wouldn’t need it;
  • Mixed detections would unnecessarily test the URL every time, but probably we don’t have to worry about the “performance impact” of this

Alternatively the detections could internally replace exists with an async version, but it would be impossible to effectively handle multiple checks in the same detection:

await canEditSidebar({async: true})

@fregante fregante changed the title How to turn DOM-based into checks that can be run with elementReady? Add elementReady-like helper for DOM detections Jul 28, 2022
@fregante
Copy link
Member Author

Something like:

export async function wait(detection) {
	while (!detection() && document isn't ready) {
		await Promise(requestAnimationFrame)
	}

	return detection();
}

@fregante
Copy link
Member Author

Note that we moved these user-capability detections to the API in Refined GitHub:

However there are still other DOM-based detection like isOrganizationProfile that would benefit from this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Development

No branches or pull requests

1 participant