withLifecycleEffect<T extends Object> method

T withLifecycleEffect<T extends Object>({
  1. T? data,
  2. T factory()?,
  3. Launcher<T>? launchOnFirstCreate,
  4. Launcher<T>? launchOnFirstStart,
  5. Launcher<T>? launchOnFirstResume,
  6. Launcher<T>? repeatOnStarted,
  7. Launcher<T>? repeatOnResumed,
  8. Launcher<T>? launchOnDestroy,
})

直接使用生命周期对类对象进行操作

Implementation

T withLifecycleEffect<T extends Object>({
  T? data,
  T Function()? factory,
  Launcher<T>? launchOnFirstCreate,
  Launcher<T>? launchOnFirstStart,
  Launcher<T>? launchOnFirstResume,
  Launcher<T>? repeatOnStarted,
  Launcher<T>? repeatOnResumed,
  Launcher<T>? launchOnDestroy,
}) {
  assert(() {
    if (this is LifecycleRegistryState) {
      assert(currentLifecycleState > LifecycleState.initialized,
          'In LifecycleRegistryState, the currentLifecycleState must be greater than LifecycleState.initialized');
    } else {
      assert(currentLifecycleState > LifecycleState.destroyed,
          'The currentLifecycleState state must be greater than LifecycleState.destroyed.');
    }
    return true;
  }());

  if (currentLifecycleState <= LifecycleState.destroyed) {
    throw 'The currentLifecycleState state must be greater than LifecycleState.destroyed.';
  }
  assert(data != null || factory != null,
      'data and factory cannot be null at the same time');
  if (data == null && factory == null) {
    throw 'data and factory cannot be null at the same time';
  }
  T value = data ?? factory!.call();
  if (launchOnFirstCreate == null &&
      launchOnFirstStart == null &&
      launchOnFirstResume == null &&
      repeatOnStarted == null &&
      repeatOnResumed == null &&
      launchOnDestroy == null) {
    return value;
  }

  Lifecycle life = toLifecycle();

  Map<Object, _LauncherLifecycleObserver> _lifecycleEffectObservers =
      life.lifecycleExtData.putIfAbsent(
          TypedKey<Map<Object, _LauncherLifecycleObserver>>(
              _withLifecycleEffectToken),
          () => weak.WeakMap());

  _LauncherLifecycleObserver<T> observer =
      _lifecycleEffectObservers.putIfAbsent(value as Object, () {
    final o = _LauncherLifecycleObserver<T>(value);
    o.launchOnFirstCreate = launchOnFirstCreate;
    o.launchOnFirstStart = launchOnFirstStart;
    o.launchOnFirstResume = launchOnFirstResume;
    o.launchOnDestroy = launchOnDestroy;

    if (repeatOnStarted != null) {
      o._repeatOn[LifecycleState.started] = repeatOnStarted;
    }
    if (repeatOnResumed != null) {
      o._repeatOn[LifecycleState.resumed] = repeatOnResumed;
    }

    //加入销毁的逻辑
    life.addLifecycleObserver(LifecycleObserver.eventDestroy(
        () => _lifecycleEffectObservers.remove(value)));

    life.addLifecycleObserver(o);
    return o;
  }) as _LauncherLifecycleObserver<T>;

  observer.launchOnDestroy = launchOnDestroy;
  if (repeatOnStarted != null)
    observer._repeatOn[LifecycleState.started] = repeatOnStarted;
  if (repeatOnResumed != null)
    observer._repeatOn[LifecycleState.resumed] = repeatOnResumed;
  return value;
}