Image you face the situation:
fetch an interface, the response includes items which have unready status, developer have to filter them and then polling to fetch the items's final status.
But, the question is that:
-
What if it's a pagination situation? After trigger an paging action, the developer have to release all the resources including timer or on the way fetching.
-
What if the developer have some actions, so they have to stop all the polling actions?
BASE ON* [Observer pattern]**(https://en.wikipedia.org/wiki/Observer_pattern)
-
Create an Manager, it's task is to manage all the polling tasks;
-
if any polling task is done (satisfied
done
condition), notify the developer; -
if any polling task is fail, retry polling until reach
retryLimit
, then nofity the developer; -
if accept stop action, clear resources.
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
pollingFetcher | 轮训请求 | (params) => promise | null |
getParams | 轮训请求参数 | object | null |
key | 轮训任务唯一标识 | string | 'id' |
interval | 轮训请求间隔 | number | 500ms |
doneCondition | 轮训任务完成回调 | function(res) | () => true |
tryLimit | 轮训请求失败,重试次数 | number | 3 |
const { addTask, notify, clearTask, clear } = usePollingManager({
pollingFetcher: fetchPollingStatus,
getParams: (task) => ({
id: task.id
}),
doneCondition: (res) => res.progress === 100 || res.progress === -1,
})
// 添加需要轮训的任务
addTask(...tasks)
// 轮训结果通知
notify((task, res) => {
const { id: taskId } = task
// do sth
})