register<T> method

void register<T>({
  1. PillarKey? key,
  2. String? token,
  3. PillarFactory<T>? factory,
  4. PillarPureFactory<T>? pureFactory,
  5. T? instance,
  6. PillarScope? scope,
})

Implementation

void register<T>({
  PillarKey? key,
  String? token,
  PillarFactory<T>? factory,
  PillarPureFactory<T>? pureFactory,
  T? instance,
  PillarScope? scope,
}) {
  final PillarKey effectiveKey = _createKey<T>(key, token);
  if (_map.containsKey(effectiveKey)) {
    throw Exception('$effectiveKey already registered');
  }
  _map[effectiveKey] = PillarEntry(
    key: effectiveKey,
    factory: switch ((factory, pureFactory, instance, scope)) {
      (PillarFactory factory, null, null, PillarScope _) => () =>
          ScopeEnforcingPillarAccessor(
            _map[effectiveKey]!,
            _get,
          ).wrapFactory(factory),
      (PillarFactory factory, null, null, null) => () =>
          ScopeTrackingPillarAccessor(_get).wrapFactory(factory),
      (null, PillarPureFactory factory, null, _) => () => (
            value: factory(),
            scope: null,
          ),
      (null, null, T _, PillarScope _) => () {
          throw Exception(
            'Instance should have been used instead of calling factory method',
          );
        },
      (null, null, T _, null) => throw Exception(
          'Instance should have scope specified',
        ),
      _ => throw Exception(
          'You should supply factory, pureFactory, or instance',
        ),
    },
    value: instance,
    instanceScope: scope,
  );
}