Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/kiwi/lib/src/kiwi_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typedef _ProviderName = String;
/// Signature for the generic provider value.
typedef _ProviderValue = Map<Type, _Provider<Object>>;

typedef AssistedInjector<S, P> = S Function(P);

/// A simple service container.
class KiwiContainer {
/// Creates a scoped container.
Expand Down Expand Up @@ -114,6 +116,21 @@ class KiwiContainer {
_setProvider(name, _Provider<S>.factory(factory));
}

/// Registers a factory with assistend injection into the container.
///
/// A factory returning Function that returns an object of type [S] can be registered.
///
/// If [name] is set, the factory will be registered under this name.
/// To retrieve the same factory, the same name should be provided
/// to [KiwiContainer.resolve].

void registerAssistedFactory<S, P>(
FactoryBuilder<AssistedInjector<S, P>> factory, {
String? name,
}) {
_setProvider(name, _Provider<AssistedInjector<S, P>>.factory(factory));
}

/// Registers a factory that will be called only only when
/// accessing it for the first time, into the container.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/kiwi/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository: https://github.com/gbtb16/kiwi/tree/master/packages/kiwi
issue_tracker: https://github.com/gbtb16/kiwi/issues

environment:
sdk: ">=2.14.0 <4.0.0"
sdk: ">=3.0.0"

dependencies:
meta: ^1.8.0
Expand Down
15 changes: 15 additions & 0 deletions packages/kiwi/test/kiwi_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,26 @@ void main() {
(c) => const Sith('Anakin', 'Skywalker', 'DarthVader'),
name: 'named');

container.registerAssistedFactory<Sith, SithAssistedParameters>((c) {
return (SithAssistedParameters args) =>
Sith('Sheev', 'Palpatine', args.id);
});

expect(container.resolve<int>(), 5);
expect(container.resolve<Sith>(),
const Sith('Anakin', 'Skywalker', 'DarthVader'));
expect(container.resolve<Character>(),
const Character('Anakin', 'Skywalker'));
expect(container.resolve<Character>('named'),
const Sith('Anakin', 'Skywalker', 'DarthVader'));

final Sith sith =
container.resolve<AssistedInjector<Sith, SithAssistedParameters>>()(
(id: 'DarthSidious'));

expect(sith.id, equals('DarthSidious'));
expect(sith.firstName, equals('Sheev'));
expect(sith.lastName, equals('Palpatine'));
});

test('builders should always be created', () {
Expand Down Expand Up @@ -499,6 +512,8 @@ class Character {
final String lastName;
}

typedef SithAssistedParameters = ({String id});

class Sith extends Character {
const Sith(
String firstName,
Expand Down