Bindings for the Web API (Notification, Worker, MessageChannel, ...)

Hello, is there a plan for rescript’s api modules to provide bindings for the rest of the Web API such as Notification?

Hey,

There are currently two school of thoughts of how to interact with huge complex API surfaces such as the Webapi and I want to highlight both, so you get a sense of the up- and downsides of both approaches.

Approach 1

There is the bs-webapi repository that tries to map to all kinds of webapi bindings in one cohesive binding library.

Upside: You get bindings for a subset of the WebAPI (Notifications are not covered yet it seems).
Downside: It’s a pretty big codebase with quite some nuances and more complex concepts that newcomers are not familiar with right away. Also you will eventually hit edge cases where the types don’t cover your use-cases, so you need to redefine / write your own bindings to get around the limitations.

Approach 2

Learn the basics of writing bindings (something you will need 90% of the time whenever you interact with existing JS), get inspired by how bs-webapi does things (it’s okay to copy-paste and adapt externals), and create your own bindings that help you implement the task at hand. It’s very important to get a good understanding on the interop layer of the language, so I highly recommend getting your hands dirty on the topic as soon as possible.

Upside: When familiar with writing externals / doing interop, you’ll be able to write your own externals faster than you can look up any third-party binding docs. You will have more control over the exposed types.

Downsides: It might feel weird, because users tend to build generalized bindings for every use-case of a library / JS api and try to stick to the DRY principle as much as possible. You need to learn how to write bindings and how interop works.

Related Resources:
You might want to read through this very extensive thread that talks about the details regarding the two approaches I just mentioned:

2 Likes

Thank you very much for the insightful feedback. The second approach works surprising well, the documentation is helpful, and writing the externals manually is quite rewarding.

I wish the first approach was more practical to foster code re-use. And if I understand correctly an official Web API binding is not currently planned as the surface is too complex.

Yeah I mean it depends on the JS library / API you want to write bindings to. Sometimes it works out quite nicely, e.g. for ReactJS.

The only thing ReScript officially provides are some abstract Dom types to allow bindings to align on the same base types at least. I think that’s why bs-webapi was started as an incubator project: to test drive potential designs.