Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better types - removed all use of any #5

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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