如何在不同文件中的 epics 之间共享变量?

How to share a variable between epics that are in different files?

我正在使用 redux-observable in my nextjs 网络应用程序。有一个变量必须在 epic1 中设置,稍后在 epic2 中访问:

let sharedVariable;

const epic1 = (action$, state$) => {
    return action$.pipe(
        // check if the dispatched action is of type action1
        filter(action1.match),
        tap((action) => {
            // create a new instance of A and assign
            // it to sharedVariable
            if (!sharedVariable) {
                sharedVariable = new A();
            }
        }),
        mergeMap((action) => {
            ...
        }),
    )
}

const epic2 = (action$, state$) => {
    return action$.pipe(
        // check if the dispatched action is of type action2
        filter(action2.match),
        map((action) => {
            if (sharedVariable) {
                return action3();
            } else {
                return action4();
            }
        }),
    )
}

我想将 epics 放在不同的文件上:epics1.js & epics2.js

当 epics 位于不同文件时,我应该如何启用它们来设置和访问 sharedVariable

提前致谢!

DI 听起来不错:docs

const sharedVariable = {} // can not be primitive type
const epicMiddleware = createEpicMiddleware({
  dependencies: { sharedVariable }
});

const epic2 = (action$, state$, { sharedVariable }) => {
 // Rest of code
}