FormidableLabs / freactal

Clean and robust state management for React and React-like libs.
MIT License
1.65k stars 46 forks source link

Let middleware know the next state #60

Closed zhengjunxin closed 7 years ago

zhengjunxin commented 7 years ago

In the previous logic, middleware can not know the next state so that it can't add middleware like logger to log the state changes.

provideState({
  middleware: [
    freactalCxt => Object.assign({}, freactalCxt, {
      effects: Object.keys(freactalCxt.effects).reduce((memo, key) => {
        memo[key] = (...args) => {
          console.log("Effect started", key, args);
          return freactalCxt.effects[key](...args).then(result => {
            // result will be [undefined] cause it is call by React's forceUpdate
            console.log("Effect completed", key);
            return result;
          })
        };
        return memo;
      }, {})
    })
  ]
})
divmain commented 7 years ago

This looks reasonable. Closes #62.