put<S> method

S put<S>(
  1. S builder(), {
  2. String? tag,
  3. bool permanent = false,
})

Instantiates and registers a dependency instance in this scope.

The builder is executed immediately. If an instance of type S with the same tag already exists, it is replaced and the old instance is disposed.

// Example usage:

scope.put(() => AuthService());

Parameters:

  • builder: A function that creates the instance.
  • tag: Optional unique identifier for the instance.
  • permanent: If true, the instance survives a non-forced reset.

Returns the created instance of type S.

Implementation

S put<S>(S Function() builder, {String? tag, bool permanent = false}) {
  final key = _getKey<S>(tag);

  if (_registry.containsKey(key)) {
    delete<S>(tag: tag, force: true);
  }

  final info = LevitDependency<S>(permanent: permanent);

  // Instance creation logic shifted here to allow hook access to 'info'
  info.instance = _createInstance<S>(builder, key, info);

  _registerBinding(key, info, 'put', tag: tag);

  _initializeInstance(info.instance, key, info);

  return info.instance as S;
}