unregister<T extends Object?> method

bool unregister<T extends Object?>([
  1. String? id
])

Removes a builder function registed of T type with an id optional.

Returns true when instance has been unregistered.

Implementation

bool unregister<T extends Object?>([String? id]) {
  final reactterInstance = _getReactterInstance<T?>(id);
  final typeLabel =
      _ReactterInstanceBuilder.fromStore(reactterInstance)?.mode.label ??
          InstanceManageMode.builder.label;

  if (reactterInstance == null) {
    final reactterInstance = ReactterInstance<T>(id);

    Reactter.log('The "$reactterInstance" $typeLabel already deregistered.');

    return false;
  }

  if (reactterInstance._instance != null) {
    final idParam = id != null ? "id: '$id, '" : '';

    Reactter.log(
      'The "$T" builder couldn\'t deregister '
      'because the "$reactterInstance" instance is active.\n'
      'You should delete the instance before with:\n'
      '`Reactter.delete<$T>(${id ?? ''});` or \n'
      '`Reactter.destroy<$T>($idParam, onlyInstance: true);`\n',
      isError: true,
    );

    return false;
  }

  _reactterInstance.remove(reactterInstance);

  Reactter.emit(reactterInstance, Lifecycle.unregistered);
  Reactter.offAll(reactterInstance);
  Reactter.log('The "$reactterInstance" $typeLabel has been deregistered.');

  return true;
}