{"id":764,"date":"2023-05-08T15:11:46","date_gmt":"2023-05-08T07:11:46","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=764"},"modified":"2023-05-08T15:11:46","modified_gmt":"2023-05-08T07:11:46","slug":"redux-saga-debounce-by-key","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=764","title":{"rendered":"Redux-saga Debounce by key"},"content":{"rendered":"\n<p>\u60c5\u5883\uff1apost data \u5230\u4e0d\u540c\u7684 key\uff0c\u4f46\u4f7f\u7528\u76f8\u540c\u7684 API \u548c action<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { cancel, delay, fork, take } from 'redux-saga\/effects';\n\nexport default function* debouncePerKey({\n    pattern,\n    selector,\n    timeout,\n    preFn,\n    callApi,\n}) {\n    const map = new Map();\n\n    while (true) {\n        const action = yield take(pattern);\n        const key = selector(action);\n        const existingTask = map.get(key);\n\n        if (preFn) {\n            yield preFn(action);\n        }\n\n        if (existingTask) {\n            yield cancel(existingTask);\n        }\n\n        const newTask = yield fork(function* () {\n            yield delay(timeout);\n            map.delete(key);\n            yield callApi(action);\n        });\n\n        map.set(key, newTask);\n    }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u52a0\u5165 root saga<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fork(() =>\n    debouncePerKey({\n        pattern: postExamAnswerRequest.type,\n        selector: (action) => action.payload.payload.question_id,\n        timeout: 500,\n        preFn: updateAnswer,\n        callApi: handlePostExamAnswerRequest,\n    }),\n)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u60c5\u5883\uff1apost data \u5230\u4e0d\u540c\u7684 key\uff0c\u4f46\u4f7f\u7528&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[45],"tags":[46],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-redux-and-related","tag-redux-saga"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=764"}],"version-history":[{"count":1,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":765,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions\/765"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}