dispose method

  1. @override
  2. @mustCallSuper
void dispose()
override

Dipose this ProviderModel. This will call dispose on all ProviderModelVariables created by this ProviderModel. This will also cancel all ProviderModelUseSubscriptions created by this ProviderModel.

This will also cancel all locks created by this ProviderModel asynchronously but as fast as possible. If someone tries to use this model after this point inside a batch, it will throw an exception for new operations and cleanly fail for existing operations by returning false from things like lock.

Implementation

@override
@mustCallSuper
void dispose() {
  assert(!_disposed, 'ProviderModel was already disposed.');

  _disposed = true;

  for (var i in _variables) {
    i.dispose();
  }

  final tUseSubscriptions = _useSubscriptions;

  if (tUseSubscriptions != null) {
    for (var i in tUseSubscriptions.toList(growable: false)) {
      i.cancel();
    }

    _useSubscriptions = null;
  }

  super.dispose();

  // This has no guaruntee to synchronously cancel all locks.
  // Therefore we execute this at the end.
  // If someone tries to use this model after this point inside a batch, it will throw an exception.
  if (_lockQueues != null) {
    for (var i in _lockQueues!.values) {
      i.clear();
    }

    _lockQueues = null;
  }
}