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

Feature request: Web Locks API #15905

Open
sgwilym opened this issue Sep 14, 2022 · 4 comments
Open

Feature request: Web Locks API #15905

sgwilym opened this issue Sep 14, 2022 · 4 comments
Labels
suggestion suggestions for new features (yet to be agreed) web related to Web APIs

Comments

@sgwilym
Copy link
Contributor

sgwilym commented Sep 14, 2022

https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API

The Web Locks API allows scripts running in one tab or worker to asynchronously acquire a lock, hold it while work is performed, then release it. While held, no other script executing in the same origin can acquire the same lock, which allows a web app running in multiple tabs or workers to coordinate work and the use of resources.

I've been looking for some ways to manage access to a single resource, and this seems like it could fill quite a lot of the gap left by shared workers.

@kitsonk kitsonk added web related to Web APIs suggestion suggestions for new features (yet to be agreed) labels Sep 14, 2022
@kitsonk
Copy link
Contributor

kitsonk commented Sep 14, 2022

The biggest problem is we don't have the concept of "same origin" in a lot of cases. It would effectively only work across workers of the same runtime instance.

Can you provide a specific use case you think this would solve? You say "access to a single resource" but that is very vague and a specific use case might help make the need clearer.

@sgwilym
Copy link
Contributor Author

sgwilym commented Sep 15, 2022

The biggest problem is we don't have the concept of "same origin" in a lot of cases. It would effectively only work across workers of the same runtime instance.

That would suit me just fine.

My use case is managing many class instances' access to a single database. The value of new inserts into this database is based on data that came before it, so writes must be strictly sequential. Concurrent access would result in an invalid state.

Additionally, this same codebase is used in the browser where many tabs are accessing the same IndexedDB. That I can use Deno to develop and test these features and confidently ship them to the browser is a huge deal.

I've been told I can also use SharedBufferArray and Atomics for this purpose, but I'd love a higher-level API for this job.

@iuioiua
Copy link
Collaborator

iuioiua commented Feb 1, 2023

This would be great for distributed locks in Redis.

@pubkey
Copy link

pubkey commented Oct 29, 2023

I think the weblocks API in deno should behave similar to the Deno BroadcastChannel.
There the messaging works accross instances and so should the web locks API. This would allow to run things exactly once, like database migrations or other tasks that must not be run multiple times in multiple instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggestion suggestions for new features (yet to be agreed) web related to Web APIs
Projects
None yet
Development

No branches or pull requests

4 participants