Skip to content

Commit

Permalink
chore: add support for abortSignals
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Sep 12, 2024
1 parent c57ecee commit a8e4ffa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Profile } from './profile.js'

export interface SearchConfig {
abortController?: AbortController
abortSignal?: AbortSignal
fresh?: boolean
debounce?: number
}
Expand Down Expand Up @@ -61,6 +62,10 @@ export type AnswerSessionParams = {

export { AnswerSession, Message }

function isAbortController(signal: AbortSignal | AbortController | undefined): signal is AbortController {
return signal !== undefined && (signal as AbortController)?.signal !== undefined
}

export class OramaClient {
private readonly id = createId()
private readonly api_key: string
Expand Down Expand Up @@ -149,7 +154,12 @@ export class OramaClient {
const performSearch = async () => {
try {
const timeStart = Date.now()
searchResults = await this.fetch<Results<AnyDocument>>('search', 'POST', { q: query, sst: this.searchToken }, config?.abortController)
searchResults = await this.fetch<Results<AnyDocument>>(
'search',
'POST',
{ q: query, sst: this.searchToken },
config?.abortSignal ?? config?.abortController
)
const timeEnd = Date.now()
searchResults.elapsed = await formatElapsedTime(BigInt(timeEnd * CONST.MICROSECONDS_BASE - timeStart * CONST.MICROSECONDS_BASE))
roundTripTime = timeEnd - timeStart
Expand Down Expand Up @@ -242,7 +252,7 @@ export class OramaClient {
cached = true
} else {
const timeStart = Date.now()
searchResults = await this.fetch<Results<AnyDocument>>('vector-search2', 'POST', { q: query }, config?.abortController)
searchResults = await this.fetch<Results<AnyDocument>>('vector-search2', 'POST', { q: query }, config?.abortSignal ?? config?.abortController)
const timeEnd = Date.now()

searchResults.elapsed = await formatElapsedTime(BigInt(timeEnd * CONST.MICROSECONDS_BASE - timeStart * CONST.MICROSECONDS_BASE))
Expand Down Expand Up @@ -346,10 +356,11 @@ export class OramaClient {
path: Endpoint,
method: Method,
body?: object,
abortController?: AbortController,
abort?: AbortController | AbortSignal,
queryParams?: Record<string, string | undefined>
): Promise<T> {
if (abortController?.signal.aborted === true) {
const abortSignal = isAbortController(abort) ? abort?.signal : abort
if (abortSignal?.aborted === true) {
throw new Error('Request aborted')
}

Expand All @@ -361,7 +372,7 @@ export class OramaClient {
// 'x-orama-instance-id': this.id,
// 'x-orama-version': version
},
signal: abortController?.signal
signal: abortSignal
}

if (method === 'POST' && body !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/manager/index-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type CallWebhookPayload<E extends Endpoint> = E extends EndpointSnapshot
? any[]
: E extends EndpointDeploy
? undefined
: E extends EndpointUpdateSchema
? { schema: { [key: string]: any }, embeddings?: any }
: E extends EndpointUpdateSchema
? { schema: { [key: string]: any }; embeddings?: any }
: never

export class IndexManager {
Expand Down
2 changes: 1 addition & 1 deletion src/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IndexManager } from './index-manager.js'
import { API_V1_BASE_URL } from './constants.js'

type CloudManagerConfig = {
api_key: string,
api_key: string
baseURL?: string
}

Expand Down

0 comments on commit a8e4ffa

Please sign in to comment.