Skip to content
Draft
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
2 changes: 1 addition & 1 deletion packages/pacer/src/async-debouncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface AsyncDebouncerState<TFn extends AnyAsyncFunction> {
/**
* The result from the most recent successful function execution
*/
lastResult: ReturnType<TFn> | undefined
lastResult: Awaited<ReturnType<TFn>> | undefined
/**
* Number of times maybeExecute has been called (for reduction calculations)
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/pacer/src/async-rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface AsyncRateLimiterState<TFn extends AnyAsyncFunction> {
/**
* The result from the most recent successful function execution
*/
lastResult: ReturnType<TFn> | undefined
lastResult: Awaited<ReturnType<TFn>> | undefined
/**
* Number of function executions that have been rejected due to rate limiting
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/pacer/src/async-throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface AsyncThrottlerState<TFn extends AnyAsyncFunction> {
/**
* The result from the most recent successful function execution
*/
lastResult: ReturnType<TFn> | undefined
lastResult: Awaited<ReturnType<TFn>> | undefined
/**
* Number of times maybeExecute has been called (for reduction calculations)
*/
Expand Down
21 changes: 20 additions & 1 deletion packages/pacer/tests/async-debouncer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest'
import { AsyncDebouncer, asyncDebounce } from '../src/async-debouncer'

describe('AsyncDebouncer', () => {
Expand Down Expand Up @@ -1359,4 +1359,23 @@ describe('asyncDebounce helper function', () => {
expect(debouncer.getAbortSignal()).toBeNull()
})
})

describe('Type Safety', () => {
it('should type lastResult as the awaited return type, not a Promise', () => {
const debouncer = new AsyncDebouncer(async () => 'hello', { wait: 100 })
expectTypeOf(debouncer.store.state.lastResult).toEqualTypeOf<
string | undefined
>()
})

it('should type lastResult correctly for non-primitive return types', () => {
const debouncer = new AsyncDebouncer(
async () => ({ id: 1, name: 'test' }),
{ wait: 100 },
)
expectTypeOf(debouncer.store.state.lastResult).toEqualTypeOf<
{ id: number; name: string } | undefined
>()
})
})
})
24 changes: 23 additions & 1 deletion packages/pacer/tests/async-rate-limiter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest'
import { AsyncRateLimiter, asyncRateLimit } from '../src/async-rate-limiter'

describe('AsyncRateLimiter', () => {
Expand Down Expand Up @@ -775,4 +775,26 @@ describe('asyncRateLimit', () => {
expect(rateLimiter.getAbortSignal()).toBeNull()
})
})

describe('Type Safety', () => {
it('should type lastResult as the awaited return type, not a Promise', () => {
const rateLimiter = new AsyncRateLimiter(async () => 'hello', {
limit: 5,
window: 1000,
})
expectTypeOf(rateLimiter.store.state.lastResult).toEqualTypeOf<
string | undefined
>()
})

it('should type lastResult correctly for non-primitive return types', () => {
const rateLimiter = new AsyncRateLimiter(
async () => ({ id: 1, name: 'test' }),
{ limit: 5, window: 1000 },
)
expectTypeOf(rateLimiter.store.state.lastResult).toEqualTypeOf<
{ id: number; name: string } | undefined
>()
})
})
})
21 changes: 20 additions & 1 deletion packages/pacer/tests/async-throttler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest'
import { AsyncThrottler } from '../src/async-throttler'

describe('AsyncThrottler', () => {
Expand Down Expand Up @@ -998,4 +998,23 @@ describe('AsyncThrottler', () => {
expect(throttler.getAbortSignal()).toBeNull()
})
})

describe('Type Safety', () => {
it('should type lastResult as the awaited return type, not a Promise', () => {
const throttler = new AsyncThrottler(async () => 'hello', { wait: 100 })
expectTypeOf(throttler.store.state.lastResult).toEqualTypeOf<
string | undefined
>()
})

it('should type lastResult correctly for non-primitive return types', () => {
const throttler = new AsyncThrottler(
async () => ({ id: 1, name: 'test' }),
{ wait: 100 },
)
expectTypeOf(throttler.store.state.lastResult).toEqualTypeOf<
{ id: number; name: string } | undefined
>()
})
})
})