Locks that stop asynchronous Dart code from running concurrently
open sourceMITfree
synchronized is a Dart and Flutter package with a lock for asynchronous code: a Lock runs one synchronized block at a time, like a critical section for code that uses await, so a read-modify-write, a login flow or a transaction on a store without transactions can't interleave with another. It has reentrant locks, a MultiLock that holds several at once, an extension that locks on any object, and a GlobalLock that excludes other isolates and processes on the Dart VM, or other tabs of the same origin in the browser. It runs on the Dart VM, Flutter on mobile, desktop and web, the browser and Node.
Problems it solves
- stop two async calls in my Flutter app from running the same code at once
- serialise a read-modify-write that awaits in Dart
- keep two browser tabs or processes from doing the same work at the same time in Dart
Features
- Lock that runs one async block at a time
- Reentrant locks and MultiLock for several locks at once
- Extension to lock on any object without a Lock field
- GlobalLock that excludes other isolates, processes or browser tabs
- Optional timeout on waiting for a lock
