mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 05:02:14 +00:00
- Add setEditorText() and getEditorText() to HookUIContext for prompt generator pattern - custom() now accepts async factories for fire-and-forget work - Add CancellableLoader component to tui package - Add BorderedLoader component for hooks with cancel UI - Export HookAPI, HookContext, HookFactory from main package - Update all examples to import from packages instead of relative paths - Update hooks.md and custom-tools.md documentation fixes #350
39 lines
933 B
TypeScript
39 lines
933 B
TypeScript
import { isEscape } from "../keys.js";
|
|
import { Loader } from "./loader.js";
|
|
|
|
/**
|
|
* Loader that can be cancelled with Escape.
|
|
* Extends Loader with an AbortSignal for cancelling async operations.
|
|
*
|
|
* @example
|
|
* const loader = new CancellableLoader(tui, cyan, dim, "Working...");
|
|
* loader.onAbort = () => done(null);
|
|
* doWork(loader.signal).then(done);
|
|
*/
|
|
export class CancellableLoader extends Loader {
|
|
private abortController = new AbortController();
|
|
|
|
/** Called when user presses Escape */
|
|
onAbort?: () => void;
|
|
|
|
/** AbortSignal that is aborted when user presses Escape */
|
|
get signal(): AbortSignal {
|
|
return this.abortController.signal;
|
|
}
|
|
|
|
/** Whether the loader was aborted */
|
|
get aborted(): boolean {
|
|
return this.abortController.signal.aborted;
|
|
}
|
|
|
|
handleInput(data: string): void {
|
|
if (isEscape(data)) {
|
|
this.abortController.abort();
|
|
this.onAbort?.();
|
|
}
|
|
}
|
|
|
|
dispose(): void {
|
|
this.stop();
|
|
}
|
|
}
|