diff --git a/packages/core/src/createPoll.ts b/packages/core/src/createPoll.ts index 42ea1c3..9483732 100644 --- a/packages/core/src/createPoll.ts +++ b/packages/core/src/createPoll.ts @@ -6,7 +6,7 @@ type TimeoutReturn = ReturnType type OnEnd = (options: { times: number, res: Awaited | undefined, maxTimes: number }) => any type onMaxTimes = (options: { times: number, res: Awaited, maxTimes: number }) => any type onTimeout = (options: { times: number, timeout: number, maxTimes: number }) => any -type OnEachCall = (options: { times: number, res: Awaited, maxTimes: number }) => false | void +type OnEachCall = (options: { times: number, res: Awaited, maxTimes: number }) => boolean | void interface CreatePollOptions { taskFn: AnyFn @@ -72,9 +72,9 @@ export function createPoll(options: CreatePollOptions) { const { taskFn, interval = 0, maxTimes = 0, onEachCall, onEnd, onMaxTimes, onTimeout, timeout = 0 } = options /** - * 是否停止轮询 + * 是否正在轮询 */ - let canceled = false + let isPolling = false /** * 轮询次数 */ @@ -90,16 +90,13 @@ export function createPoll(options: CreatePollOptions) { let timeoutId!: TimeoutReturn const stopPoll = (res?: Awaited) => { - canceled = true + isPolling = false timeoutId && clearTimeout(timeoutId) timer && clearTimeout(timer) onEnd?.({ times, res, maxTimes }) } const poll = () => { - if (canceled) - return - const res_ = taskFn() const p = isPromise(res_) ? res_ : Promise.resolve(res_) @@ -122,7 +119,10 @@ export function createPoll(options: CreatePollOptions) { } const startPoll = () => { - canceled = false + if (isPolling) + return + + isPolling = true times = 0 if (isNumber(timeout) && timeout > 0) {