Organise the prompt service code a bit better

Note that instance fields are evaluated during instance creation, at the start of the constructor.
This commit is contained in:
Mattias Granlund 2024-03-20 11:58:07 +01:00
parent 14def6e616
commit 0d5dbc25b7

View File

@ -6,6 +6,7 @@ export type SystemPrompt = {
id: string;
prompt: string;
context?: {
// TODO: camelCase this field
branch_id?: string;
action?: string;
};
@ -20,19 +21,19 @@ type PromptResponse = {
export class PromptService {
prompt$ = new Subject<SystemPrompt>();
constructor() {
this.subscribe(async (payload) => {
if (payload.context?.action == 'auto') {
this.cancel(payload.id).then(() => console.log('cancelled auto askpass', payload));
} else {
this.prompt$.next(payload);
}
});
}
private unlisten = listen<SystemPrompt>('git_prompt', async (e) => {
// You can send an action token to e.g. `fetch_target_data` and it will be echoed in
// these events. The action `auto` is used by the `BaseBranchService` so we can not
// respond to them.
if (e.payload.context?.action != 'auto') {
this.prompt$.next(e.payload);
} else {
// Always cancel actions that are marked "auto", e.g. periodic sync
await this.cancel(e.payload.id);
}
});
private subscribe(callback: (params: SystemPrompt) => Promise<void> | void) {
return listen<SystemPrompt>('git_prompt', (e) => callback(e.payload));
}
constructor() {}
async respond(payload: PromptResponse) {
return await invoke('submit_prompt_response', payload);
@ -41,4 +42,8 @@ export class PromptService {
async cancel(id: string) {
return await invoke('submit_prompt_response', { id: id, response: null });
}
destroy() {
this.unlisten();
}
}