Skip to content

Commit

Permalink
feature(scoped-parent): implemented optional named parameter called "…
Browse files Browse the repository at this point in the history
…parent" to reuse its providers in the new instance.
  • Loading branch information
gbtb16 committed Jan 15, 2024
1 parent 8fb8331 commit c446da6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions kiwi/lib/src/kiwi_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ typedef T Factory<T>(KiwiContainer container);
/// A simple service container.
class KiwiContainer {
/// Creates a scoped container.
KiwiContainer.scoped()
: _namedProviders = Map<String?, Map<Type, _Provider<Object>>>();
///
/// If [parent] is set, the new scoped instance will include its providers.
KiwiContainer.scoped({
KiwiContainer? parent,
}) : _namedProviders = <String?, Map<Type, _Provider<Object>>>{
if (parent != null)
...parent._namedProviders.map(
// [Map.from] is needed to create a copy of value and not use its reference,
// because if only value is passed, everything included in the parent will be
// added to the new instance at any time, even after this scoped instance has been created.
(key, value) => MapEntry(key, Map.from(value)),
),
};

static final KiwiContainer _instance = KiwiContainer.scoped();

Expand Down

0 comments on commit c446da6

Please sign in to comment.