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 stopOnDomReady option, remove .cancel(), add .stop() #17

Merged
merged 17 commits into from
May 27, 2019

Conversation

fregante
Copy link
Collaborator

@fregante fregante commented Mar 20, 2019

Requires semver major because it changes the default behavior.

  • Approval
  • index.test-d.ts with null, why is it failing?
  • Docs

Fixes #8

@sindresorhus
Copy link
Owner

Requires semver major because it changes the default behavior.

Good timing. I already need to do a major release for other things in master.

Approval

👍

index.test-d.ts with null, why is it failing?

I commented inline.


Can you fix the merge conflict?


I think we should use undefined instead of null.

index.test-d.ts Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title Add cancelOnDomLoaded: true option Add cancelOnDomLoaded option Apr 1, 2019
@fregante
Copy link
Collaborator Author

fregante commented Apr 1, 2019

Null is what querySelector returns, so I thought of keeping it consistent

@sindresorhus
Copy link
Owner

I’m trying to get rid of null in my packages. We can’t change the browser APIs, but at least we can make our code better.

@fregante
Copy link
Collaborator Author

fregante commented Apr 2, 2019

What’s wrong with null? Is it typeof null === 'object'?

@sindresorhus
Copy link
Owner

sindresorhus/meta#7

@fregante
Copy link
Collaborator Author

Are only docs missing here?

@fregante fregante mentioned this pull request Apr 11, 2019
@sindresorhus
Copy link
Owner

Yes

@fregante
Copy link
Collaborator Author

fregante commented May 15, 2019

Good to go, however we should probably rename it:

  • the promise isn't actually cancelled (like .cancel()), but resolved to null
  • the "dom ready" event is not commonly known as "dom loaded"

So perhaps it should be stopOnDomReady, untilDomReady, beforeDomReady. The name should reflect that it does indeed stop on dom ready, but it can still be used after the event.

@sindresorhus
Copy link
Owner

sindresorhus commented May 20, 2019

I'm starting to think that .cancel() should be changed too. Since there are many paths that could end up with undefined now, consumers needs to handle it anyway, so .cancel() could just resolve to undefined instead of rejecting. So maybe we should rename .cancel() to .stop() too?

I like stopOnDomReady.

index.js Outdated
domLoaded.then(() => {
cancelAnimationFrame(rafId);
resolve();
});
Copy link
Owner

Choose a reason for hiding this comment

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

I know you hate this, but I prefer:

(async () => {
	await domLoaded;
	cancelAnimationFrame(rafId);
	resolve();
})();

@fregante
Copy link
Collaborator Author

So maybe we should rename .cancel() to .stop() too?

So I should drop p-cancelable since it wouldn't be a regular cancelable promise.

@fregante fregante changed the title Add cancelOnDomLoaded option Add stopOnDomReady option May 23, 2019
test.js Show resolved Hide resolved
@sindresorhus
Copy link
Owner

So I should drop p-cancelable since it wouldn't be a regular cancelable promise.

Yes

index.d.ts Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
const {
target = document,
stopOnDomReady = true
} = options;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thoughts on this pattern instead?

const elementReady = (selector, {
	target = document,
	stopOnDomReady = true
} = {}) => {
	// Code
};

Copy link
Owner

Choose a reason for hiding this comment

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

Sure, that's fine too. It just has a tendency to become kinda messy when you have a lot of options.

@sindresorhus sindresorhus changed the title Add stopOnDomReady option Add stopOnDomReady option, remove .cancel(), add .stop() May 27, 2019
@sindresorhus sindresorhus merged commit 32363fa into master May 27, 2019
@sindresorhus sindresorhus deleted the dom-loaded branch May 27, 2019 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically stop on dom ready
2 participants