alibaba / fish-redux

An assembled flutter application framework.
https://github.com/alibaba/fish-redux
Apache License 2.0
7.33k stars 843 forks source link

在EffectMiddleware中我要如何在不执行Effect.call(action, ctx)的情况下,判断Effect中是否包含某个Action对应的SubEffect. #552

Closed VintLin closed 1 year ago

VintLin commented 4 years ago

问题

我有一些需求需要拦截Page和Component的SubEffect,但是,Component中的SubEffect会在EffectMiddleware中多次触发,似乎触发一个Component中的SubEffect会逐一尝试同一Page下的所有Component的Effect.

Effect<T> combineEffects<T>(Map<Object, SubEffect<T>> map) =>
    (map == null || map.isEmpty)
        ? null
        : (Action action, Context<T> ctx) {
            final SubEffect<T> subEffect = map.entries
                .firstWhere(
                  (MapEntry<Object, SubEffect<T>> entry) =>
                      action.type == entry.key,
                  orElse: () => null,
                )
                ?.value;

            if (subEffect != null) {
              return subEffect.call(action, ctx) ?? _SUB_EFFECT_RETURN_NULL;
            }

            //skip-lifecycle-actions
            if (action.type is Lifecycle) {
              return _SUB_EFFECT_RETURN_NULL;
            }

            /// no subEffect
            return null;
          };

根据combineEffects写法包含SubEffect的Map也直接封装在Effect方法里了,无法在执行某个Action之前知道该Action是否在Effect中注册。

以下为EffectMiddleware的Log: