dispose method

Future<void> dispose()
inherited

Disposes the module and releases all resources.

After disposal, the module cannot be used and will throw exceptions if any methods are called. Override onDispose to perform custom cleanup logic.

Note: This method is idempotent - calling it multiple times is safe.

Implementation

Future<void> dispose() async {
  if (_state == ModuleLifecycleState.disposed ||
      _state == ModuleLifecycleState.disposing) {
    return;
  }

  await _transitionTo(ModuleLifecycleState.disposing);

  try {
    info('Disposing module $name');

    clear();

    await onDispose();

    di.clear();

    await _transitionTo(ModuleLifecycleState.disposed);
    info('Module $name disposed successfully');
  } catch (e) {
    error('Failed to dispose module $name: $e');
    await _transitionTo(ModuleLifecycleState.error, error: e);
    rethrow;
  }
}