{"id":100,"date":"2022-01-13T09:05:17","date_gmt":"2022-01-13T09:05:17","guid":{"rendered":"http:\/\/114.132.224.28\/?p=100"},"modified":"2022-01-13T09:05:17","modified_gmt":"2022-01-13T09:05:17","slug":"vuex%e4%b8%ad%e9%97%b4%e4%bb%b6redux%e4%b8%ad%e9%97%b4%e4%bb%b6%e5%92%8ckoa%e4%b8%ad%e9%97%b4%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/localhost\/2022\/01\/13\/vuex%e4%b8%ad%e9%97%b4%e4%bb%b6redux%e4%b8%ad%e9%97%b4%e4%bb%b6%e5%92%8ckoa%e4%b8%ad%e9%97%b4%e4%bb%b6\/","title":{"rendered":"vuex\u4e2d\u95f4\u4ef6redux\u4e2d\u95f4\u4ef6\u548ckoa\u4e2d\u95f4\u4ef6"},"content":{"rendered":"\n

vuex\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n

\u901a\u8fc7store.subscribe\u6ce8\u5165\u5230vuex\u4e2d\uff0c\u901a\u8fc7_subscriber\u6570\u7ec4\u6536\u96c6\u8d77\u6765\uff0c\u5728\u5185\u7f6e\u51fd\u6570commit\u7684\u65f6\u5019\u5c31\u904d\u5386\u6267\u884c\u90a3\u4e9b\u6570\u7ec4\u91cc\u9762\u7684\u51fd\u6570<\/p>\n\n\n\n

redux\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n

\u901a\u8fc7\u4e0d\u65ad\u5305\u88c5store.dispatch\uff0c\u6765\u5b9e\u73b0\u7ec4\u5408\u8c03\u7528\u3002\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\u63a5\u53d7\u65e7\u7684dispatch\uff0c\u5185\u90e8\u51fd\u6570\u63a5\u53d7action\uff0c\u6267\u884c\u5b8c\u526f\u4f5c\u7528\u540e\u5728\u8c03\u7528\u65e7\u7684dispatch(action)\u3002<\/p>\n\n\n\n

\u901a\u8fc7\u6570\u7ec4\u7684reduce\uff0c\u4e0d\u65ad\u5305\u88c5\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7\u4f20\u9012dispatch\u6765\u5b8c\u6210\u7ec4\u5408\u3002\u6700\u540e\u7ec4\u6210\u7684\u65b0\u7684store.dispatch\u4f1a\u66ff\u4ee3\u539f\u672c\u7684store.dispatch,\u6570\u7ec4\u7b2c\u4e00\u4e2a\u7684store.dispatch\u4f1a\u5148\u88ab\u6267\u884c<\/p>\n\n\n\n

function logDispatch(dispatch) {\n  return function(action) {\n   console.log('dispatch')\n   dispatch(action)\n  }\n}\nfunction errorDispatch(dispatch) {\n  return function(action) {\n   try{\n     dispatch(action)\n   }catch(e){\n    console.log(e)\n   }\n  }\n}\nfunction customDispatch(dispatch) {\n return function(action) {\n   console.log('custom')\n   dispatch(action)\n  }\n}\nconst wrapperDisapatch = [logDispatch, errorDispatch, customDispatch].reduce(\n  (wrapperDispatch,dispatch) => {\n   return (...args) => wrapperDispatch(dispatch(...args))\n})\nstore.dispatch = wrapperDisapatch(store.dispatch)\nstore.dispacth({\n  type: '',\n  payload: ''\n})<\/code><\/pre>\n\n\n\n

koa\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n

\u901a\u8fc7\u904d\u5386\u63d2\u4ef6\uff0c\u63a7\u5236index\u7684\u4f4d\u7f6e\u6765\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u8c03\u7528next\u3002\u5c31\u80fd\u8df3\u8f6c\u5230\u4e0b\u4e00\u4e2a\u63d2\u4ef6\u7684\u4f4d\u7f6e\uff0c\u5b98\u65b9\u7684next\u6ca1\u6709\u63a5\u6536\u53c2\u6570\uff0c\u5176\u5b9e\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u3002<\/p>\n\n\n\n

<\/p>\n\n\n\n

\u4e0b\u9762\u5229\u7528\u4e0a\u9762\u90a3\u79cd\u4e2d\u95f4\u4ef6\u6765\u5b9e\u73b0\u529f\u80fd\uff1a<\/h2>\n\n\n\n

vuex\u53ea\u662f\u5355\u7eaf\u7684\u904d\u5386\u8fd8\u662fnext\u4e0enext\u4e4b\u95f4\u6ca1\u6709\u9012\u8fdb\u5173\u7cfb\uff0c\u6ca1\u6709\u529e\u6cd5\u4e2d\u9014\u7ec8\u6b62dispatch<\/p>\n\n\n\n

\nhttps:\/\/codepen.io\/cyanxxx\/pen\/ZEXmLbK?editors=0011\n<\/div><\/figure>\n\n\n\n
\/\/ redux\n\/\/ function filterString(next) {\n\/\/   return function (file) {\n\/\/ console.log(file)\n\/\/     return typeof file === 'string'? next(file + '1') : file\n\/\/   }\n\/\/ }\n\/\/   function filterEnd(next) {\n\/\/     return function (file) {\n\/\/ console.log(file)\n\/\/       return \/1\/.exec(file)? next(file+'2') : file\n\/\/    }\n\/\/ }\n\n\/\/ function addEnd(next) {\n\/\/   return function (file) {\n\/\/ console.log(file)\n\/\/     next(file + '#EXT-X-ENDLIST' + '\\n')\n\/\/   }\n\/\/ }\n\n\/\/ function setData(next) {\n\/\/   return function(file) {\n\/\/      console.log(file)\n\/\/   }\n\/\/ }\n\n\/\/ ([filterString, filterEnd, addEnd].reduce((pre, next) => {\n\/\/   return (...args)=> pre(next(...args))\n\/\/ }))(setData)('bbb')\n\n\/\/ koa\nfunction filterString(file, next) {\n    console.log('filter', file)\n    return typeof file === 'string'? next(file + '1') : file\n}\n  function filterEnd(file, next) {\n    console.log('filterEnd', file)\n    return \/1\/.exec(file)? next(file+'2') : file\n}\n\nfunction addEnd(file,next) {\n    console.log('addEnd', file)\n    next(file + '#EXT-X-ENDLIST' + '\\n')\n}\n\nfunction setData(file,next) {\n     console.log('data', file)\n}\n\nfunction compose(arr) {\n  let index = -1\n  return function (file) {\n    \/\/  i\u4e0b\u4e2anext\u7684\u4f4d\u7f6e\n     function dispatch(i, ...args) {\n      if(i <= index) {\n        throw('more next')\n      }\n       index = i\n       const fn = arr[index]\n       if(!fn) return;\n       return fn(...args, dispatch.bind(null, i + 1))\n    }\n    return dispatch(0, file)\n  }\n}\n\ncompose([filterString, filterEnd, addEnd, setData])('bbb')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

vuex\u4e2d\u95f4\u4ef6 \u901a\u8fc7store.subscribe …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/100"}],"collection":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":0,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}