Skip to content

Commit 86d5fc5

Browse files
authored
perf - avoid IdleValue for a feature that is not enabled by default (microsoft#159469)
1 parent c7378f7 commit 86d5fc5

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/vs/workbench/contrib/localHistory/browser/localHistory.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@ import { Codicon } from 'vs/base/common/codicons';
88
import { language } from 'vs/base/common/platform';
99
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1010
import { registerIcon } from 'vs/platform/theme/common/iconRegistry';
11-
import { IdleValue } from 'vs/base/common/async';
1211

13-
export const LOCAL_HISTORY_DATE_FORMATTER: IdleValue<{ format: (timestamp: number) => string }> = new IdleValue(() => {
14-
const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
12+
interface ILocalHistoryDateFormatter {
13+
format: (timestamp: number) => string;
14+
}
1515

16-
let formatter: Intl.DateTimeFormat;
17-
try {
18-
formatter = new Intl.DateTimeFormat(language, options);
19-
} catch (error) {
20-
formatter = new Intl.DateTimeFormat(undefined, options); // error can happen when language is invalid (https://github.com/microsoft/vscode/issues/147086)
16+
let localHistoryDateFormatter: ILocalHistoryDateFormatter | undefined = undefined;
17+
18+
export function getLocalHistoryDateFormatter(): ILocalHistoryDateFormatter {
19+
if (!localHistoryDateFormatter) {
20+
const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
21+
22+
let formatter: Intl.DateTimeFormat;
23+
try {
24+
formatter = new Intl.DateTimeFormat(language, options);
25+
} catch (error) {
26+
formatter = new Intl.DateTimeFormat(undefined, options); // error can happen when language is invalid (https://github.com/microsoft/vscode/issues/147086)
27+
}
28+
29+
localHistoryDateFormatter = {
30+
format: date => formatter.format(date)
31+
};
2132
}
2233

23-
return {
24-
format: date => formatter.format(date)
25-
};
26-
});
34+
return localHistoryDateFormatter;
35+
}
2736

2837
export const LOCAL_HISTORY_MENU_CONTEXT_VALUE = 'localHistory:item';
2938
export const LOCAL_HISTORY_MENU_CONTEXT_KEY = ContextKeyExpr.equals('timelineItem', LOCAL_HISTORY_MENU_CONTEXT_VALUE);

src/vs/workbench/contrib/localHistory/browser/localHistoryCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { IModelService } from 'vs/editor/common/services/model';
3030
import { ILanguageService } from 'vs/editor/common/languages/language';
3131
import { ILabelService } from 'vs/platform/label/common/label';
3232
import { firstOrDefault } from 'vs/base/common/arrays';
33-
import { LOCAL_HISTORY_DATE_FORMATTER, LOCAL_HISTORY_ICON_RESTORE, LOCAL_HISTORY_MENU_CONTEXT_KEY } from 'vs/workbench/contrib/localHistory/browser/localHistory';
33+
import { getLocalHistoryDateFormatter, LOCAL_HISTORY_ICON_RESTORE, LOCAL_HISTORY_MENU_CONTEXT_KEY } from 'vs/workbench/contrib/localHistory/browser/localHistory';
3434
import { IPathService } from 'vs/workbench/services/path/common/pathService';
3535

3636
const LOCAL_HISTORY_CATEGORY = { value: localize('localHistory.category', "Local History"), original: 'Local History' };
@@ -646,7 +646,7 @@ export async function findLocalHistoryEntry(workingCopyHistoryService: IWorkingC
646646

647647
const SEP = /\//g;
648648
function toLocalHistoryEntryDateLabel(timestamp: number): string {
649-
return `${LOCAL_HISTORY_DATE_FORMATTER.value.format(timestamp).replace(SEP, '-')}`; // preserving `/` will break editor labels, so replace it with a non-path symbol
649+
return `${getLocalHistoryDateFormatter().format(timestamp).replace(SEP, '-')}`; // preserving `/` will break editor labels, so replace it with a non-path symbol
650650
}
651651

652652
//#endregion

src/vs/workbench/contrib/localHistory/browser/localHistoryTimeline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { SaveSourceRegistry } from 'vs/workbench/common/editor';
2020
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2121
import { COMPARE_WITH_FILE_LABEL, toDiffEditorArguments } from 'vs/workbench/contrib/localHistory/browser/localHistoryCommands';
2222
import { MarkdownString } from 'vs/base/common/htmlContent';
23-
import { LOCAL_HISTORY_DATE_FORMATTER, LOCAL_HISTORY_ICON_ENTRY, LOCAL_HISTORY_MENU_CONTEXT_VALUE } from 'vs/workbench/contrib/localHistory/browser/localHistory';
23+
import { getLocalHistoryDateFormatter, LOCAL_HISTORY_ICON_ENTRY, LOCAL_HISTORY_MENU_CONTEXT_VALUE } from 'vs/workbench/contrib/localHistory/browser/localHistory';
2424
import { Schemas } from 'vs/base/common/network';
2525
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
2626
import { getVirtualWorkspaceAuthority } from 'vs/platform/workspace/common/virtualWorkspace';
@@ -151,7 +151,7 @@ export class LocalHistoryTimeline extends Disposable implements IWorkbenchContri
151151
return {
152152
handle: entry.id,
153153
label: SaveSourceRegistry.getSourceLabel(entry.source),
154-
tooltip: new MarkdownString(`$(history) ${LOCAL_HISTORY_DATE_FORMATTER.value.format(entry.timestamp)}\n\n${SaveSourceRegistry.getSourceLabel(entry.source)}`, { supportThemeIcons: true }),
154+
tooltip: new MarkdownString(`$(history) ${getLocalHistoryDateFormatter().format(entry.timestamp)}\n\n${SaveSourceRegistry.getSourceLabel(entry.source)}`, { supportThemeIcons: true }),
155155
source: LocalHistoryTimeline.ID,
156156
timestamp: entry.timestamp,
157157
themeIcon: LOCAL_HISTORY_ICON_ENTRY,

0 commit comments

Comments
 (0)