暂停 rootEpic 直到坚持完成

Pause rootEpic until persist completion

我在我的应用程序中使用 redux-persist 和 redux-observable 并注意到我的 API 包装在 observable 中的调用在存储状态恢复之前发生。恢复会用陈旧数据覆盖获取的值。 rootEpic 如何在缓冲时暂停,直到 persist/REHYDRATE 动作到达?

如果您想等到 "event" 发生,您可以使用 .skipUntil。例如:

const action$ = new Rx.Subject();

// In your epic
action$
  .skipUntil(action$.filter(({ type }) => type === 'hydrated'))
  .subscribe(x => console.log(x));

// "Rehydrate"
action$.next({ type: 'hello!' });
action$.next({ type: 'hello?' });
action$.next({ type: 'any body there?' });
action$.next({ type: 'hydrated' });
action$.next({ type: 'do' });
action$.next({ type: 'you' });
action$.next({ type: 'see' });
action$.next({ type: 'me' });
action$.next({ type: 'now' });
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>

我想,为了不将此添加到您的所有 epics,您可以 .mergeMap() 您的根史诗之类的 :)


在下方评论讨论后:

其他中间件也可能会出现此问题。因此,在 re-hydration 完成之前不触发任何操作可能是更好的解决方案。

我不熟悉 redux-persist,但是请注意,将中间件传递给 redux 的顺序可能很重要,也可能不重要。一些中间件需要在其他中间件之前或之后。如果这听起来与您的问题相关,您可以尝试交换它们。