使用redux链接操作和构建对象

时间:2018-02-01 00:32:58

标签: javascript node.js redux

下面我有一些redux实现,我想在概念上包围我。

我想在后端使用redux来帮助维护代码的控制流程。我在getRequirerDirgetPathResolvedgetModuleResolved下面有简单的功能。我想使用redux中的操作将这三个函数链接在一起,我想构建一个返回值的对象。其中一个是承诺。我实现了redux thunk。

const start = (moduleString, requirer) => {type: 'START', moduleString, requirer}

const moduleString = './index.js'
const requirer = ''
store.dispatch(start(moduleString, requirer))

const getRequirerDir = (requirer) => path.dirname(requirer)
const getPathResolved = (requirerDir, moduleString) => path.resolve(requirerDir, moduleString)
const getModuleResolved = async (pathResolved) => await ModuleString.resolve(pathResolved)

// starting state
// {}

// ending state
// {moduleString, requirer, requirerDir, pathResolved, moduleResolved}

2 个答案:

答案 0 :(得分:2)

注意:这可能无法完全回答您的问题。

这是一个有趣的问题。我注意到你自己已经完成了预期的实施。对此表示赞赏。但是,在后端维护应用程序状态是一种不好的做法。但有些情况下您可能希望暂时维持状态。如果上述州停留较长时间,我建议您使用redis。

如果没有,有不同的方法来解决您的问题。我能为你的用例考虑的最合适的是SAGA。使用Sagas,您可以将应用程序编写为状态机。这个概念也用于实现Redux-Saga库。但是,您可能希望手动实现Sagas或使用专为NodeJ设计的Saga库。

答案 1 :(得分:1)

这是一个完整的工作示例。

const thunk = require('redux-thunk').default
const { createStore, applyMiddleware } = require('redux')
const ModuleString = require('../ModuleString')
const Path = require('../Path')

function reducer (state = {}, action) {
  const {type, ...actionData} = action
  switch (type) {
    case _getDeps:
      return {...state, ...actionData}
    case _getRequirerDir:
      return {...state, ...actionData}
    case _getPathResolved:
      return {...state, ...actionData}
    case _getModuleResolved:
      return {...state, ...actionData}
    default:
      return state
  }
}

const _getDeps = 'getDeps'
const _getRequirerDir = 'getRequirerDir'
const _getPathResolved = 'getPathResolved'
const _getModuleResolved = 'getModuleResolved'

const actionCreators = {
  [_getDeps]: (moduleString, requirer) => async (dispatch, getState) => {
    await dispatch(({type: _getDeps, moduleString, requirer}))
    await dispatch(actionCreators.getRequirerDir(moduleString, requirer))
  },
  [_getRequirerDir]: (moduleString, requirer) => async (dispatch) => {
    const requirerDir = getRequirerDir(requirer)
    await dispatch(({type: _getRequirerDir, requirerDir}))
    await dispatch(actionCreators.getPathResolved(requirerDir, moduleString))
  },
  [_getPathResolved]: (requirerDir, moduleString) => async (dispatch) => {
    const pathResolved = getPathResolved(requirerDir, moduleString)
    await dispatch(({type: _getPathResolved, pathResolved}))
    await dispatch(actionCreators.getModuleResolved(pathResolved))
  },
  [_getModuleResolved]: (pathResolved) => async (dispatch) => {
    const moduleResolved = await getModuleResolved(pathResolved)
    await dispatch(({type: _getModuleResolved, moduleResolved}))
  }
}

const store = createStore(
  reducer,
  applyMiddleware(thunk)
)

store.subscribe(() => console.log(store.getState()))

const moduleString = './index.js'
const requirer = ''
store.dispatch(actionCreators.getDeps(moduleString, requirer))

const getRequirerDir = (requirer) => Path.dirname(requirer)
const getPathResolved = (requirerDir, moduleString) => Path.resolve(requirerDir, moduleString)
const getModuleResolved = async (pathResolved) => ModuleString.resolve(pathResolved)

反馈/优化欢迎。