Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Nov 15, 2023
1 parent ba02c03 commit e56aa22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/client/pythonEnvironments/common/externalDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import * as fsapi from 'fs-extra';
import * as path from 'path';
import { Worker } from 'worker_threads';
import * as vscode from 'vscode';
import { IWorkspaceService } from '../../common/application/types';
import { ExecutionResult, IProcessServiceFactory, ShellOptions, SpawnOptions } from '../../common/process/types';
Expand Down Expand Up @@ -200,3 +201,25 @@ export function onDidChangePythonSetting(name: string, callback: () => void, roo
}
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export async function executeWorkerFile(workerFileName: string, workerData: any): Promise<any> {
return new Promise((resolve, reject) => {
const worker = new Worker(workerFileName, { workerData });
worker.on('message', (res: { err: Error; res: unknown }) => {
if (res.err) {
reject(res.err);
}
resolve(res.res);
});
worker.on('error', (ex: Error) => {
traceError(`Error in worker ${workerFileName}`, ex);
reject(ex);
});
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker ${workerFileName} stopped with exit code ${code}`));
}
});
});
}
28 changes: 3 additions & 25 deletions src/client/pythonEnvironments/common/windowsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import { HKCU, HKLM, Options, REG_SZ, Registry, RegistryItem } from 'winreg';
import * as path from 'path';
import { Worker } from 'worker_threads';
import { createDeferred } from '../../common/utils/async';
import { traceError } from '../../logging';
import { executeWorkerFile } from './externalDependencies';

export { HKCU, HKLM, REG_SZ, Options };

Expand Down Expand Up @@ -40,7 +39,7 @@ export async function readRegistryValues(options: Options, useWorkerThreads: boo
});
return deferred.promise;
}
return executeWorkerFile('registryValuesWorker.js', options);
return executeWorkerFile(path.join(__dirname, 'registryValuesWorker.js'), options);
}

export async function readRegistryKeys(options: Options, useWorkerThreads: boolean): Promise<IRegistryKey[]> {
Expand All @@ -57,26 +56,5 @@ export async function readRegistryKeys(options: Options, useWorkerThreads: boole
});
return deferred.promise;
}
return executeWorkerFile('registryKeysWorker.js', options);
}

async function executeWorkerFile(workerFileName: string, workerData: any): Promise<any> {
return new Promise((resolve, reject) => {
const worker = new Worker(path.join(__dirname, workerFileName), { workerData });
worker.on('message', (res: { err: Error; res: unknown }) => {
if (res.err) {
reject(res.err);
}
resolve(res.res);
});
worker.on('error', (ex: Error) => {
traceError(`Error in worker ${workerFileName}`, ex);
reject(ex);
});
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker ${workerFileName} stopped with exit code ${code}`));
}
});
});
return executeWorkerFile(path.join(__dirname, 'registryKeysWorker.js'), options);
}

0 comments on commit e56aa22

Please sign in to comment.