Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Prototype type safe query selector#63

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
nojaf wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromtyped-queryselector

Conversation

@nojaf
Copy link
Member

Got a remark recently on

let h2 = Global.document->Document.querySelector("h2")h2->Null.toOption->Option.forEach(h2 => {  let heading: DOMAPI.htmlElement = h2->Obj.magic  heading.style.color = "red"})

That it is pretty far from what you can do in JS:h2?.style.color = "red".

Made me wonder if we should include some ergonomic helpers in this library as well.
Thoughts?

@julien-deoux
Copy link
Contributor

julien-deoux commentedApr 18, 2025
edited
Loading

Hi. First of all, thanks for your work on those bindings, it is much appreciated!

Assigning a value to a conditionally chained field actually hits you with aSyntaxError: Left side of assignment is not a reference, which means the JS we're benchmarking against looks more like this:

consth2=document.querySelector("h2");if(h2){h2.style.color="red";}

Still very succinct for sure, but a fairer comparison.

The current version of @rescript/webapi provides anasHtmlElement utility in theWebapi__Dom__Element module which returns an option (Seehere). I think your approach withtryParse from theHTMLHeadingElement side makes more sense. Not sure about the name (fromInstance?).

In any case, I'm not sure this function would fit in this library. I like the idea of having zero-cost bindings available and maybe a utility library on the side. The question then becomes: should @rescript/webapi be the zero-cost bindings library, with something like @rescript/webapi-utils for utilities and a more idomatic API, of instead be the accessible default while @rescript/webapi-raw provides the zero-cost version for those who want it?

If we suppose the utility library also wraps all nullables with options, we could dream of something like:

openWrappedWebAPIGlobal.document->Document.querySelector("h2")->Option.flatMap(HTMLElement.tryParse)->Option.forEach(h2=> {h2.style.color="red"})

which is still quite verbose but doesn't feel too bad to me.

@nojaf
Copy link
MemberAuthor

Hello, thank you for your recent PRs and sharing your thoughts on this.
I think it make sense to keep@rescript/webapi a zero-cost bindings only package.
Something like@rescript/webapi-utils might fit the bill better. (Although, I don't like that exact name).

It also makes wonder if we should not go for a workspace approach with multiple packages.
Extract some parts of the current package into new packages to keep@rescript/webapi a bit more lightweight.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@nojaf@julien-deoux

[8]ページ先頭

©2009-2025 Movatter.jp