shared<T extends Object> method

  1. @override
T shared<T extends Object>(
  1. T create(
    1. PlanServices
    )
)
override

This kit's one T for the current document: create runs on the first call, later calls return the same instance. A T that is a Disposable is disposed with its document. Keyed by (kit, T), so two kits asking for the same type get one each.

Implementation

@override
T shared<T extends Object>(T Function(PlanServices) create) {
  if (_disposed) throw StateError('shared<$T> after dispose');
  final found = _shared[T];
  if (found != null) return found as T;
  if (!_making.add(T)) {
    throw StateError('shared<$T> asked for itself while being made');
  }
  try {
    final made = create(this);
    _shared[T] = made;
    _made.add(made);
    return made;
  } finally {
    _making.remove(T);
  }
}