SynchronizedLock extension

Add lock ability to any object.

Then you can simple call on any object.

myObject.synchronized(() async {
  // ...uninterrupted action
});

class MyClass {
  /// Perform a long action that won't be called more than once at a time.
  Future<void> performAction() {
    // Lock at the instance level
    return synchronized(() async {
      // ...uninterrupted action
    });
  }
}

Or you can synchronize at the class level

class MyClass {
  /// Perform a long action that won't be called more than once at a time.
  Future<void> performClassAction() {
    // Lock at the class level
    return runtimeType.synchronized(() async {
      // ...uninterrupted action
    });
  }
}

The lock mechanism is based on identity so beware of potential conflicts (for example using String object).

on

Methods

synchronized<T>(FutureOr<T> computation(), {Duration? timeout}) Future<T>
Executes computation when lock is available.