Skip to content

Commit 2000f36

Browse files
committed
remove memoize.clear
fixes microsoft#123001
1 parent 8c5490f commit 2000f36

File tree

4 files changed

+68
-112
lines changed

4 files changed

+68
-112
lines changed

src/vs/base/common/decorators.ts

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,39 @@ export function createDecorator(mapFn: (fn: Function, key: string) => Function):
2424
};
2525
}
2626

27-
let memoizeId = 0;
28-
export function createMemoizer() {
29-
const memoizeKeyPrefix = `$memoize${memoizeId++}`;
30-
let self: any = undefined;
27+
export function memoize(_target: any, key: string, descriptor: any) {
28+
let fnKey: string | null = null;
29+
let fn: Function | null = null;
3130

32-
const result = function memoize(target: any, key: string, descriptor: any) {
33-
let fnKey: string | null = null;
34-
let fn: Function | null = null;
35-
36-
if (typeof descriptor.value === 'function') {
37-
fnKey = 'value';
38-
fn = descriptor.value;
31+
if (typeof descriptor.value === 'function') {
32+
fnKey = 'value';
33+
fn = descriptor.value;
3934

40-
if (fn!.length !== 0) {
41-
console.warn('Memoize should only be used in functions with zero parameters');
42-
}
43-
} else if (typeof descriptor.get === 'function') {
44-
fnKey = 'get';
45-
fn = descriptor.get;
35+
if (fn!.length !== 0) {
36+
console.warn('Memoize should only be used in functions with zero parameters');
4637
}
47-
48-
if (!fn) {
49-
throw new Error('not supported');
38+
} else if (typeof descriptor.get === 'function') {
39+
fnKey = 'get';
40+
fn = descriptor.get;
41+
}
42+
43+
if (!fn) {
44+
throw new Error('not supported');
45+
}
46+
47+
const memoizeKey = `$memoize$${key}`;
48+
descriptor[fnKey!] = function (...args: any[]) {
49+
if (!this.hasOwnProperty(memoizeKey)) {
50+
Object.defineProperty(this, memoizeKey, {
51+
configurable: false,
52+
enumerable: false,
53+
writable: false,
54+
value: fn!.apply(this, args)
55+
});
5056
}
5157

52-
const memoizeKey = `${memoizeKeyPrefix}:${key}`;
53-
descriptor[fnKey!] = function (...args: any[]) {
54-
self = this;
55-
56-
if (!this.hasOwnProperty(memoizeKey)) {
57-
Object.defineProperty(this, memoizeKey, {
58-
configurable: true,
59-
enumerable: false,
60-
writable: true,
61-
value: fn!.apply(this, args)
62-
});
63-
}
64-
65-
return this[memoizeKey];
66-
};
58+
return this[memoizeKey];
6759
};
68-
69-
result.clear = () => {
70-
if (typeof self === 'undefined') {
71-
return;
72-
}
73-
Object.getOwnPropertyNames(self).forEach(property => {
74-
if (property.indexOf(memoizeKeyPrefix) === 0) {
75-
delete self[property];
76-
}
77-
});
78-
};
79-
80-
return result;
81-
}
82-
83-
export function memoize(target: any, key: string, descriptor: any) {
84-
return createMemoizer()(target, key, descriptor);
8560
}
8661

8762
export interface IDebounceReducer<T> {

src/vs/base/test/common/decorators.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as sinon from 'sinon';
77
import * as assert from 'assert';
8-
import { memoize, createMemoizer, throttle } from 'vs/base/common/decorators';
8+
import { memoize, throttle } from 'vs/base/common/decorators';
99

1010
suite('Decorators', () => {
1111
test('memoize should memoize methods', () => {
@@ -131,28 +131,6 @@ suite('Decorators', () => {
131131
}
132132
});
133133

134-
test('memoize clear', () => {
135-
const memoizer = createMemoizer();
136-
let counter = 0;
137-
class Foo {
138-
@memoizer
139-
get answer() {
140-
return ++counter;
141-
}
142-
}
143-
144-
const foo = new Foo();
145-
assert.strictEqual(foo.answer, 1);
146-
assert.strictEqual(foo.answer, 1);
147-
memoizer.clear();
148-
assert.strictEqual(foo.answer, 2);
149-
assert.strictEqual(foo.answer, 2);
150-
memoizer.clear();
151-
assert.strictEqual(foo.answer, 3);
152-
assert.strictEqual(foo.answer, 3);
153-
assert.strictEqual(foo.answer, 3);
154-
});
155-
156134
test('throttle', () => {
157135
const spy = sinon.spy();
158136
const clock = sinon.useFakeTimers();

src/vs/workbench/contrib/debug/browser/debugEditorContribution.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { ExceptionWidget } from 'vs/workbench/contrib/debug/browser/exceptionWid
3030
import { FloatingClickWidget } from 'vs/workbench/browser/codeeditor';
3131
import { Position } from 'vs/editor/common/core/position';
3232
import { CoreEditingCommands } from 'vs/editor/browser/controller/coreCommands';
33-
import { memoize, createMemoizer } from 'vs/base/common/decorators';
33+
import { memoize } from 'vs/base/common/decorators';
3434
import { IEditorHoverOptions, EditorOption } from 'vs/editor/common/config/editorOptions';
3535
import { DebugHoverWidget } from 'vs/workbench/contrib/debug/browser/debugHover';
3636
import { ITextModel } from 'vs/editor/common/model';
@@ -185,7 +185,6 @@ export class DebugEditorContribution implements IDebugEditorContribution {
185185
private hoverRange: Range | null = null;
186186
private mouseDown = false;
187187
private exceptionWidgetVisible: IContextKey<boolean>;
188-
private static readonly MEMOIZER = createMemoizer();
189188

190189
private exceptionWidget: ExceptionWidget | undefined;
191190
private configurationWidget: FloatingClickWidget | undefined;
@@ -234,7 +233,7 @@ export class DebugEditorContribution implements IDebugEditorContribution {
234233
}));
235234
this.toDispose.push(this.editor.onKeyDown((e: IKeyboardEvent) => this.onKeyDown(e)));
236235
this.toDispose.push(this.editor.onDidChangeModelContent(() => {
237-
DebugEditorContribution.MEMOIZER.clear();
236+
this._wordToLineNumbersMap = undefined;
238237
this.updateInlineValuesScheduler.schedule();
239238
}));
240239
this.toDispose.push(this.debugService.getViewModel().onWillUpdateViews(() => this.updateInlineValuesScheduler.schedule()));
@@ -247,7 +246,7 @@ export class DebugEditorContribution implements IDebugEditorContribution {
247246
this.toggleExceptionWidget();
248247
this.hideHoverWidget();
249248
this.updateConfigurationWidgetVisibility();
250-
DebugEditorContribution.MEMOIZER.clear();
249+
this._wordToLineNumbersMap = undefined;
251250
await this.updateInlineValueDecorations(stackFrame);
252251
}));
253252
this.toDispose.push(this.editor.onDidScrollChange(() => {
@@ -266,9 +265,12 @@ export class DebugEditorContribution implements IDebugEditorContribution {
266265
}));
267266
}
268267

269-
@DebugEditorContribution.MEMOIZER
268+
private _wordToLineNumbersMap: Map<string, number[]> | undefined = undefined;
270269
private get wordToLineNumbersMap(): Map<string, number[]> {
271-
return getWordToLineNumbersMap(this.editor.getModel());
270+
if (!this._wordToLineNumbersMap) {
271+
this._wordToLineNumbersMap = getWordToLineNumbersMap(this.editor.getModel());
272+
}
273+
return this._wordToLineNumbersMap;
272274
}
273275

274276
private applyHoverConfiguration(model: ITextModel, stackFrame: IStackFrame | undefined): void {

src/vs/workbench/contrib/webview/browser/themeing.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { createMemoizer } from 'vs/base/common/decorators';
76
import { Disposable } from 'vs/base/common/lifecycle';
87
import { EDITOR_FONT_DEFAULTS, IEditorOptions } from 'vs/editor/common/config/editorOptions';
98
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
@@ -22,8 +21,7 @@ interface WebviewThemeData {
2221

2322
export class WebviewThemeDataProvider extends Disposable {
2423

25-
26-
private static readonly MEMOIZER = createMemoizer();
24+
private _cachedWebViewThemeData: WebviewThemeData | undefined = undefined;
2725

2826
private readonly _onThemeDataChanged = this._register(new Emitter<void>());
2927
public readonly onThemeDataChanged = this._onThemeDataChanged.event;
@@ -50,38 +48,41 @@ export class WebviewThemeDataProvider extends Disposable {
5048
return this._themeService.getColorTheme();
5149
}
5250

53-
@WebviewThemeDataProvider.MEMOIZER
5451
public getWebviewThemeData(): WebviewThemeData {
55-
const configuration = this._configurationService.getValue<IEditorOptions>('editor');
56-
const editorFontFamily = configuration.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;
57-
const editorFontWeight = configuration.fontWeight || EDITOR_FONT_DEFAULTS.fontWeight;
58-
const editorFontSize = configuration.fontSize || EDITOR_FONT_DEFAULTS.fontSize;
59-
60-
const theme = this._themeService.getColorTheme();
61-
const exportedColors = colorRegistry.getColorRegistry().getColors().reduce((colors, entry) => {
62-
const color = theme.getColor(entry.id);
63-
if (color) {
64-
colors['vscode-' + entry.id.replace('.', '-')] = color.toString();
65-
}
66-
return colors;
67-
}, {} as { [key: string]: string; });
68-
69-
const styles = {
70-
'vscode-font-family': DEFAULT_FONT_FAMILY,
71-
'vscode-font-weight': 'normal',
72-
'vscode-font-size': '13px',
73-
'vscode-editor-font-family': editorFontFamily,
74-
'vscode-editor-font-weight': editorFontWeight,
75-
'vscode-editor-font-size': editorFontSize + 'px',
76-
...exportedColors
77-
};
78-
79-
const activeTheme = ApiThemeClassName.fromTheme(theme);
80-
return { styles, activeTheme, themeLabel: theme.label, };
52+
if (!this._cachedWebViewThemeData) {
53+
const configuration = this._configurationService.getValue<IEditorOptions>('editor');
54+
const editorFontFamily = configuration.fontFamily || EDITOR_FONT_DEFAULTS.fontFamily;
55+
const editorFontWeight = configuration.fontWeight || EDITOR_FONT_DEFAULTS.fontWeight;
56+
const editorFontSize = configuration.fontSize || EDITOR_FONT_DEFAULTS.fontSize;
57+
58+
const theme = this._themeService.getColorTheme();
59+
const exportedColors = colorRegistry.getColorRegistry().getColors().reduce((colors, entry) => {
60+
const color = theme.getColor(entry.id);
61+
if (color) {
62+
colors['vscode-' + entry.id.replace('.', '-')] = color.toString();
63+
}
64+
return colors;
65+
}, {} as { [key: string]: string; });
66+
67+
const styles = {
68+
'vscode-font-family': DEFAULT_FONT_FAMILY,
69+
'vscode-font-weight': 'normal',
70+
'vscode-font-size': '13px',
71+
'vscode-editor-font-family': editorFontFamily,
72+
'vscode-editor-font-weight': editorFontWeight,
73+
'vscode-editor-font-size': editorFontSize + 'px',
74+
...exportedColors
75+
};
76+
77+
const activeTheme = ApiThemeClassName.fromTheme(theme);
78+
this._cachedWebViewThemeData = { styles, activeTheme, themeLabel: theme.label, };
79+
}
80+
81+
return this._cachedWebViewThemeData;
8182
}
8283

8384
private reset() {
84-
WebviewThemeDataProvider.MEMOIZER.clear();
85+
this._cachedWebViewThemeData = undefined;
8586
this._onThemeDataChanged.fire();
8687
}
8788
}

0 commit comments

Comments
 (0)