Skip to content

Commit

Permalink
fix(adapter): fix queue types (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Sep 30, 2024
1 parent a417795 commit 055f138
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions workspaces/adapter/src/queued-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export class QueuedAdapter implements QueuedAdapterApi {
async prompt<A extends PromptAnswers = PromptAnswers>(questions: PromptQuestions<A>, initialAnswers?: Partial<A>): Promise<A> {
return this.#queue.add(async () => this.actualAdapter.prompt(questions, initialAnswers), {
priority: PROMPT_PRIORITY + this.delta,
}) as any;
throwOnTimeout: true,
});
}

async onIdle() {
Expand All @@ -115,17 +116,17 @@ export class QueuedAdapter implements QueuedAdapterApi {
* @param fn
* @returns
*/
async queue<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType | void> {
return this.#queue.add(() => function_(this.actualAdapter), { priority: BLOCKING_PRIORITY + this.delta });
async queue<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType> {
return this.#queue.add(() => function_(this.actualAdapter), { priority: BLOCKING_PRIORITY + this.delta, throwOnTimeout: true });
}

/**
* Log has a highest priority and should be not blocking.
* @param fn
* @returns
*/
async queueLog<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType | void> {
return this.#queue.add(() => function_(this.actualAdapter), { priority: LOG_PRIORITY + this.delta });
async queueLog<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType> {
return this.#queue.add(() => function_(this.actualAdapter), { priority: LOG_PRIORITY + this.delta, throwOnTimeout: true });
}

/**
Expand All @@ -134,7 +135,7 @@ export class QueuedAdapter implements QueuedAdapterApi {
* @param options
* @returns
*/
async progress<ReturnType>(function_: ProgressCallback<ReturnType>, options?: ProgressOptions): Promise<void | ReturnType> {
async progress<ReturnType>(function_: ProgressCallback<ReturnType>, options?: ProgressOptions): Promise<ReturnType> {
if (this.#queue.size > 0 || this.#queue.pending > 0 || options?.disabled || this.#ora.isSpinning) {
// Don't show progress if queue is not empty or already spinning.
return Promise.resolve(function_({ step() {} })).finally(() => {
Expand Down
4 changes: 2 additions & 2 deletions workspaces/adapter/src/testing/test-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ export class TestAdapter<LogType extends Logger = Logger, SpyType = any> impleme
}
}

async queue<TaskResultType>(function_: Task<TaskResultType>): Promise<void | TaskResultType> {
async queue<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType> {
return function_(this);
}

async progress<ReturnType>(
function_: (progress: { step: (prefix: string, message: string, ...arguments_: any[]) => void }) => ReturnType,

_options?: { disabled?: boolean | undefined; name?: string | undefined } | undefined,
): Promise<void | ReturnType> {
): Promise<ReturnType> {
return function_({ step() {} });
}

Expand Down
4 changes: 2 additions & 2 deletions workspaces/adapter/types/adapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export type ProgressCallback<ReturnType> = (progress: {
export type ProgressOptions = { disabled?: boolean; name?: string };

export type QueuedAdapter = InputOutputAdapter & {
queue<TaskResultType>(function_: Task<TaskResultType>): Promise<void | TaskResultType>;
progress<ResultType>(function_: ProgressCallback<ResultType>, options?: ProgressOptions): Promise<void | ResultType>;
queue<TaskResultType>(function_: Task<TaskResultType>): Promise<TaskResultType>;
progress<ResultType>(function_: ProgressCallback<ResultType>, options?: ProgressOptions): Promise<ResultType>;
};

0 comments on commit 055f138

Please sign in to comment.