Skip to content

Commit

Permalink
Merge pull request #241154 from mjbvz/comprehensive-deer
Browse files Browse the repository at this point in the history
Align TS ext lazy implementation with core
  • Loading branch information
mjbvz authored Feb 19, 2025
2 parents 4efaaee + 3704d97 commit 8fe3d06
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
9 changes: 5 additions & 4 deletions extensions/typescript-language-features/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import * as vscode from 'vscode';
import { Api, getExtensionApi } from './api';
import { CommandManager } from './commands/commandManager';
import { registerBaseCommands } from './commands/index';
import { ElectronServiceConfigurationProvider } from './configuration/configuration.electron';
import { ExperimentationTelemetryReporter, IExperimentationTelemetryReporter } from './experimentTelemetryReporter';
import { ExperimentationService } from './experimentationService';
import { createLazyClientHost, lazilyActivateClient } from './lazyClientHost';
import { Logger } from './logging/logger';
import { nodeRequestCancellerFactory } from './tsServer/cancellation.electron';
import { NodeLogDirectoryProvider } from './tsServer/logDirectoryProvider.electron';
import { PluginManager } from './tsServer/plugins';
import { ElectronServiceProcessFactory } from './tsServer/serverProcess.electron';
import { DiskTypeScriptVersionProvider } from './tsServer/versionProvider.electron';
import { ActiveJsTsEditorTracker } from './ui/activeJsTsEditorTracker';
import { ElectronServiceConfigurationProvider } from './configuration/configuration.electron';
import { onCaseInsensitiveFileSystem } from './utils/fs.electron';
import { Logger } from './logging/logger';
import { Lazy } from './utils/lazy';
import { getPackageInfo } from './utils/packageInfo';
import { PluginManager } from './tsServer/plugins';
import * as temp from './utils/temp.electron';

export function activate(
Expand Down Expand Up @@ -75,7 +76,7 @@ export function activate(
registerBaseCommands(commandManager, lazyClientHost, pluginManager, activeJsTsEditorTracker);

import('./task/taskProvider').then(module => {
context.subscriptions.push(module.register(lazyClientHost.map(x => x.serviceClient)));
context.subscriptions.push(module.register(new Lazy(() => lazyClientHost.value.serviceClient)));
});

import('./languageFeatures/tsconfig').then(module => {
Expand Down
4 changes: 2 additions & 2 deletions extensions/typescript-language-features/src/lazyClientHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ManagedFileContextManager from './ui/managedFileContext';
import { ServiceConfigurationProvider } from './configuration/configuration';
import * as fileSchemes from './configuration/fileSchemes';
import { standardLanguageDescriptions, isJsConfigOrTsConfigFileName } from './configuration/languageDescription';
import { Lazy, lazy } from './utils/lazy';
import { Lazy } from './utils/lazy';
import { Logger } from './logging/logger';
import { PluginManager } from './tsServer/plugins';

Expand All @@ -37,7 +37,7 @@ export function createLazyClientHost(
},
onCompletionAccepted: (item: vscode.CompletionItem) => void,
): Lazy<TypeScriptServiceClientHost> {
return lazy(() => {
return new Lazy(() => {
const clientHost = new TypeScriptServiceClientHost(
standardLanguageDescriptions,
context,
Expand Down
52 changes: 30 additions & 22 deletions extensions/typescript-language-features/src/utils/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,45 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export interface Lazy<T> {
value: T;
hasValue: boolean;
map<R>(f: (x: T) => R): Lazy<R>;
}
export class Lazy<T> {

class LazyValue<T> implements Lazy<T> {
private _hasValue: boolean = false;
private _didRun: boolean = false;
private _value?: T;
private _error: Error | undefined;

constructor(
private readonly _getValue: () => T
private readonly executor: () => T,
) { }

/**
* True if the lazy value has been resolved.
*/
get hasValue() { return this._didRun; }

/**
* Get the wrapped value.
*
* This will force evaluation of the lazy value if it has not been resolved yet. Lazy values are only
* resolved once. `getValue` will re-throw exceptions that are hit while resolving the value
*/
get value(): T {
if (!this._hasValue) {
this._hasValue = true;
this._value = this._getValue();
if (!this._didRun) {
try {
this._value = this.executor();
} catch (err) {
this._error = err;
} finally {
this._didRun = true;
}
}
if (this._error) {
throw this._error;
}
return this._value!;
}

get hasValue(): boolean {
return this._hasValue;
}

public map<R>(f: (x: T) => R): Lazy<R> {
return new LazyValue(() => f(this.value));
}
/**
* Get the wrapped value without forcing evaluation.
*/
get rawValue(): T | undefined { return this._value; }
}

export function lazy<T>(getValue: () => T): Lazy<T> {
return new LazyValue<T>(getValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { lazy } from './lazy';
import { Lazy } from './lazy';

function makeRandomHexString(length: number): string {
const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
Expand All @@ -18,12 +18,12 @@ function makeRandomHexString(length: number): string {
return result;
}

const rootTempDir = lazy(() => {
const rootTempDir = new Lazy(() => {
const filename = `vscode-typescript${process.platform !== 'win32' && process.getuid ? process.getuid() : ''}`;
return path.join(os.tmpdir(), filename);
});

export const instanceTempDir = lazy(() => {
export const instanceTempDir = new Lazy(() => {
const dir = path.join(rootTempDir.value, makeRandomHexString(20));
fs.mkdirSync(dir, { recursive: true });
return dir;
Expand Down

0 comments on commit 8fe3d06

Please sign in to comment.