Skip to content

Commit

Permalink
bugfix: make asyncThrottle more type-safe
Browse files Browse the repository at this point in the history
now infers the type of arguments of `func` correctly
  • Loading branch information
bengry committed Oct 31, 2021
1 parent 0a45d15 commit e3c7601
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/createAsyncStoragePersistor-experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ export const createAsyncStoragePersistor = ({
}
}

function asyncThrottle<T>(
func: (...args: ReadonlyArray<unknown>) => Promise<T>,
function asyncThrottle<Args extends readonly unknown[], Result>(
func: (...args: Args) => Promise<Result>,
{ interval = 1000, limit = 1 }: { interval?: number; limit?: number } = {}
) {
if (typeof func !== 'function') throw new Error('argument is not function.')
const running = { current: false }
let lastTime = 0
let timeout: number
const queue: Array<any[]> = []
return (...args: any) =>
const queue: Array<Args> = []
return (...args: Args) =>
(async () => {
if (running.current) {
lastTime = Date.now()
Expand Down

0 comments on commit e3c7601

Please sign in to comment.