From 9ee4fbd56a8ee085f7dce148e8bb776b20901ccf Mon Sep 17 00:00:00 2001 From: Greg Pabian <35925521+grzpab@users.noreply.github.com> Date: Fri, 1 Dec 2023 12:46:49 +0100 Subject: [PATCH] wip --- src/components/messageBus.ts | 5 ++++ src/data/readHomeDirectoryCases.ts | 37 ++++++++++++++++++++++++++++++ src/extension.ts | 35 ++++++++++------------------ 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/components/messageBus.ts b/src/components/messageBus.ts index 721693ef..59aee180 100644 --- a/src/components/messageBus.ts +++ b/src/components/messageBus.ts @@ -45,6 +45,8 @@ export const enum MessageKind { mainWebviewViewVisibilityChange = 38, executionQueueChange = 39, + + loadHomeDirectoryData = 40, } export type Command = @@ -161,6 +163,9 @@ export type Message = | Readonly<{ kind: MessageKind.executionQueueChange; queuedCodemodHashes: ReadonlyArray; + }> + | Readonly<{ + kind: MessageKind.loadHomeDirectoryData; }>; type EmitterMap = { diff --git a/src/data/readHomeDirectoryCases.ts b/src/data/readHomeDirectoryCases.ts index fe6f7d0d..a4dea460 100644 --- a/src/data/readHomeDirectoryCases.ts +++ b/src/data/readHomeDirectoryCases.ts @@ -12,6 +12,8 @@ import { } from './schemata/surfaceAgnosticJobSchema'; import { CodemodEntry } from '../codemods/types'; import EventEmitter from 'events'; +import { MessageBus, MessageKind } from '../components/messageBus'; +import { Store } from '.'; interface HomeDirectoryEventEmitter extends EventEmitter { emit(event: 'start'): boolean; @@ -205,3 +207,38 @@ export const readHomeDirectoryCases = async ( return eventEmitter; }; + +export class HomeDirectoryService { + public constructor( + private readonly __messageBus: MessageBus, + private readonly __store: Store, + private readonly __rootUri: Uri | null, + ) { + __messageBus.subscribe(MessageKind.loadHomeDirectoryData, async () => { + if (!this.__rootUri) { + return; + } + + const eventEmitter = await readHomeDirectoryCases( + this.__rootUri, + this.__store.getState().codemod.entities, + ); + + const jobHandler = (kase: Case, jobs: ReadonlyArray) => { + this.__messageBus.publish({ + kind: MessageKind.upsertCase, + kase, + jobs, + }); + }; + + eventEmitter?.once('end', () => { + eventEmitter.off('job', jobHandler); + }); + + eventEmitter?.on('job', jobHandler); + + eventEmitter?.emit('start'); + }); + } +} diff --git a/src/extension.ts b/src/extension.ts index d46afca2..e92f1da8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -45,7 +45,10 @@ import { } from './data/codemodConfigSchema'; import { parsePrivateCodemodsEnvelope } from './data/privateCodemodsEnvelopeSchema'; import { GlobalStateTokenStorage, UserService } from './components/userService'; -import { readHomeDirectoryCases } from './data/readHomeDirectoryCases'; +import { + HomeDirectoryService, + readHomeDirectoryCases, +} from './data/readHomeDirectoryCases'; import { Job } from './jobs/types'; export const enum SEARCH_PARAMS_KEYS { @@ -1261,27 +1264,13 @@ export async function activate(context: vscode.ExtensionContext) { kind: MessageKind.bootstrapEngine, }); - // load the cases and jobs from the homedir - if (rootUri !== null) { - const eventEmitter = await readHomeDirectoryCases( - rootUri, - store.getState().codemod.entities, - ); - - const jobHandler = (kase: Case, jobs: ReadonlyArray) => { - messageBus.publish({ - kind: MessageKind.upsertCase, - kase, - jobs, - }); - }; - - eventEmitter?.once('end', () => { - eventEmitter.off('job', jobHandler); - }); - - eventEmitter?.on('job', jobHandler); + const homeDirectoryService = new HomeDirectoryService( + messageBus, + store, + rootUri, + ); - eventEmitter?.emit('start'); - } + messageBus.publish({ + kind: MessageKind.loadHomeDirectoryData, + }); }