From 0d5dbc25b74dbfdd97bbeabea25939943eb4341c Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Wed, 20 Mar 2024 11:58:07 +0100 Subject: [PATCH] Organise the prompt service code a bit better Note that instance fields are evaluated during instance creation, at the start of the constructor. --- gitbutler-ui/src/lib/backend/prompt.ts | 29 +++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/gitbutler-ui/src/lib/backend/prompt.ts b/gitbutler-ui/src/lib/backend/prompt.ts index 6fbc6c293..1565201b1 100644 --- a/gitbutler-ui/src/lib/backend/prompt.ts +++ b/gitbutler-ui/src/lib/backend/prompt.ts @@ -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(); - 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('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) { - return listen('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(); + } }