Skip to content

Commit

Permalink
Merge pull request #5 from smartprocure/feature/better-types
Browse files Browse the repository at this point in the history
Better types - removed all use of `any`
  • Loading branch information
dubiousdavid committed May 30, 2024
2 parents d9ec6c3 + 5a78b95 commit eaea0a7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.9.0

- Better types - removed all use of `any`.
- `rateLimit` - Changed the return type of the `finish` method to `Promise<void>`.

# 0.8.0

- Added `waitUntil` - Wait until the predicate returns truthy or the timeout expires.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prom-utils",
"version": "0.8.0",
"version": "0.9.0",
"description": "Promise utilities: rate limiting, queueing/batching, defer, etc.",
"author": "GovSpend",
"main": "dist/index.js",
Expand Down
16 changes: 8 additions & 8 deletions src/fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const debug = _debug('prom-utils')
* await limiter.finish()
* ```
*/
export const rateLimit = (limit: number) => {
export const rateLimit = <T = unknown>(limit: number) => {
debug('limit: %d', limit)
const set = new Set<Promise<any>>()
const set = new Set<Promise<T>>()
/**
* Add a promise. Returns immediately if limit has not been
* met. Waits for one promise to resolve if limit is met.
*/
const add = async (prom: Promise<any>) => {
const add = async (prom: Promise<T>) => {
// Add to set
set.add(prom)
debug('set size: %d', set.size)
Expand All @@ -49,9 +49,9 @@ export const rateLimit = (limit: number) => {
/**
* Wait for all promises to resolve
*/
const finish = () => {
const finish = async () => {
debug('finish')
return Promise.all(set)
await Promise.all(set)
}
return { add, finish }
}
Expand Down Expand Up @@ -88,9 +88,9 @@ export function batchQueue<A, B>(
) {
const { batchSize = 500, timeout } = options
debug('options %o', options)
let queue: any[] = []
let queue: A[] = []
let timeoutId: ReturnType<typeof setTimeout>
let prom: Promise<any>
let prom: Promise<unknown>
let bytes = 0

/**
Expand Down Expand Up @@ -229,7 +229,7 @@ export const pacemaker = async <T>(
}
}

export const TimeoutError = makeError("TimeoutError")
export const TimeoutError = makeError('TimeoutError')

/**
* Wait until the predicate returns truthy or the timeout expires.
Expand Down
4 changes: 3 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ describe('rateLimit', () => {
})
test('should finish awaiting remaining promises', async () => {
expect.assertions(3)
const limiter = rateLimit(3)
// Pass type variable
const limiter = rateLimit<string>(3)
const done: string[] = []
const createProm = () =>
setTimeout(1000, 'done').then((p) => {
done.push(p)
return p
})

const startTime = new Date().getTime()
Expand Down

0 comments on commit eaea0a7

Please sign in to comment.