load method

Future<Null> load()

Public method to trigger the loading of a Module.

Calls the onLoad() method, which can be implemented on a Module. Executes the willLoad and didLoad event streams.

Initiates the loading process when the module is in the instantiated state. If the module is in the loaded or loading state a warning is logged and the method is a noop. If the module is in any other state, a StateError is thrown.

If an Exception is thrown during the call to onLoad it will be emitted on the didLoad lifecycle stream. The returned Future will also resolve with this exception.

Note that LifecycleModule only supports one load/unload cycle. If load is called after a module has been unloaded, a StateError is thrown.

Implementation

Future<Null> load() {
  if (isOrWillBeDisposed) {
    return _buildDisposedOrDisposingResponse(methodName: 'load');
  }

  if (isLoading || isLoaded) {
    return _buildNoopResponse(
        isTransitioning: isLoading,
        methodName: 'load',
        currentState:
            isLoading ? LifecycleState.loading : LifecycleState.loaded);
  }

  if (!isInstantiated) {
    return _buildIllegalTransitionResponse(
        reason: 'A module can only be loaded once.');
  }

  _activeSpan = _startTransitionSpan('load');
  _loadContext = _activeSpan?.context;
  _startLoadTime = _activeSpan?.startTime;

  _state = LifecycleState.loading;

  // Keep track of this load's completer
  final transition = Completer<Null>();

  // because this one can get overwritten
  _transition = transition;

  _load().then(transition.complete).catchError((error, trace) {
    transition.completeError(error, trace);
    _activeSpan?.setTag('error', true);
  }).whenComplete(() {
    _activeSpan?.finish();
    _activeSpan = null;
  });

  return transition.future;
}