runAppAsync<T> function

Future<ComponentRef<T>> runAppAsync <T>(
  1. ComponentFactory<T> componentFactory,
  2. {@required Future<void> beforeComponentCreated(
    1. Injector
    ),
  3. InjectorFactory createInjector: _identityInjector}
)

Asynchronous alternative to runApp, supporting beforeComponentCreated.

The provided callback (beforeComponentCreated) is invoked before creating the root component, with a handle to the root injector. The user must return a Future - it will be await-ed before creating the root component.

See runApp for additional details.

Implementation

Future<ComponentRef<T>> runAppAsync<T>(
  ComponentFactory<T> componentFactory, {
  @required Future<void> Function(Injector) beforeComponentCreated,
  InjectorFactory createInjector = _identityInjector,
}) {
  if (isDevMode) {
    if (componentFactory == null) {
      throw ArgumentError.notNull('componentFactory');
    }
    if (beforeComponentCreated == null) {
      throw ArgumentError.notNull('beforeComponentCreated');
    }
  }
  final injector = appInjector(createInjector);
  final appRef = injector.provideType<ApplicationRef>(ApplicationRef);
  final ngZone = injector.provideType<NgZone>(NgZone);
  return ngZone.run(() {
    final Future<void> future = beforeComponentCreated(injector);
    assert(future != null, 'beforeComponentCreated must return a Future');
    return future.then((_) => appRef.bootstrap(componentFactory));
  });
}