https://www.npmjs.com/package/redux-optimistic-thunk
看个效果( 4.4MB 图片):
实现上面这个效果的核心代码是这样的:
import uid from './uid';
let delay = time => new Promise(resolve => setTimeout(resolve, time));
export let newItem = item => ({type: 'NEW_ITEM', item: item});
export let saveItem = text => [
async (dispatch, getState) => {
await delay(getState().delay);
dispatch(newItem({text: text, id: uid(), pending: false, deleted: false}));
},
dispatch => dispatch(newItem({text: text, id: uid(), pending: true, deleted: false}))
];
export let deleteItem = id => ({type: 'DELETE_ITEM', id: id});
export let setDelay = delay => ({type: 'SET_DELAY', delay: delay});
可以看到所有的代码都是正常的业务逻辑,saveItem
就是返回个 2 段业务逻辑组成的数组,和redux-optimistic-ui这种满世界要写meta
和transactionID
的比起来干净了很多,开发者就专注写逻辑不要关心什么事务、提交、回滚……
自认这是当前 redux 社区里最好的 optimistic 中间件了
更详细的分析可以看我的博客: http://otakustay.com/publish-redux-optimistic-thunk/
1
jsq2627 2017-04-17 14:13:24 +08:00 via iPhone
你这个 gif 让我 ios 的 chrome 崩溃了
|
3
nino 2017-04-17 19:44:01 +08:00
先赞一个再看
|