mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
remove proxy
This commit is contained in:
@@ -30,16 +30,22 @@ export class Terminal implements ITerminalApi {
|
||||
this._core = new TerminalCore(options);
|
||||
this._addonManager = new AddonManager();
|
||||
|
||||
this._publicOptions = new Proxy(this._core.options, {
|
||||
get: (target, propName: string): any => {
|
||||
return target[propName];
|
||||
},
|
||||
set: (target, propName: string, value): any => {
|
||||
this._checkReadonlyOptions(propName);
|
||||
target[propName] = value;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
this._publicOptions = {};
|
||||
const getter = (propName: string): any => {
|
||||
return this._core.options[propName];
|
||||
};
|
||||
const setter = (propName: string, value: any): void => {
|
||||
this._checkReadonlyOptions(propName);
|
||||
this._core.options[propName] = value;
|
||||
};
|
||||
|
||||
for (const propName in this._core.options) {
|
||||
const desc = {
|
||||
get: getter.bind(this, propName),
|
||||
set: setter.bind(this, propName)
|
||||
};
|
||||
Object.defineProperty(this._publicOptions, propName, desc);
|
||||
}
|
||||
}
|
||||
|
||||
private _checkReadonlyOptions(propName: string): void {
|
||||
|
||||
@@ -120,11 +120,13 @@ export class MockLogService implements ILogService {
|
||||
|
||||
export class MockOptionsService implements IOptionsService {
|
||||
public serviceBrand: any;
|
||||
public readonly rawOptions: ITerminalOptions = clone(DEFAULT_OPTIONS);
|
||||
public options: ITerminalOptions = clone(DEFAULT_OPTIONS);
|
||||
public onOptionChange: IEvent<string> = new EventEmitter<string>().event;
|
||||
constructor(testOptions?: Partial<ITerminalOptions>) {
|
||||
if (testOptions) {
|
||||
for (const key of Object.keys(testOptions)) {
|
||||
this.rawOptions = testOptions[key];
|
||||
this.options[key] = testOptions[key];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,10 @@ describe('OptionsService', () => {
|
||||
it('uses default value if invalid constructor option value passed', () => {
|
||||
assert.equal(new OptionsService({ tabStopWidth: 0 }).getOption('tabStopWidth'), DEFAULT_OPTIONS.tabStopWidth);
|
||||
});
|
||||
it('object.keys return the correct number of options', () => {
|
||||
const optionsService = new OptionsService({ cols: 80, rows: 25 });
|
||||
assert.notEqual(Object.keys(optionsService.options).length, 0);
|
||||
});
|
||||
});
|
||||
describe('setOption', () => {
|
||||
let service: OptionsService;
|
||||
|
||||
@@ -60,6 +60,7 @@ const FONT_WEIGHT_OPTIONS: Extract<FontWeight, string>[] = ['normal', 'bold', '1
|
||||
export class OptionsService implements IOptionsService {
|
||||
public serviceBrand: any;
|
||||
|
||||
public readonly rawOptions: ITerminalOptions;
|
||||
public options: ITerminalOptions;
|
||||
|
||||
private _onOptionChange = new EventEmitter<string>();
|
||||
@@ -80,31 +81,39 @@ export class OptionsService implements IOptionsService {
|
||||
}
|
||||
|
||||
// set up getters and setters for each option
|
||||
this.options = this._setupOptions(defaultOptions);
|
||||
this.rawOptions = defaultOptions;
|
||||
this.options = { ... defaultOptions };
|
||||
this._setupOptions();
|
||||
}
|
||||
|
||||
private _setupOptions(options: ITerminalOptions): ITerminalOptions {
|
||||
return new Proxy(options, {
|
||||
get: (target, propName: string): any => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error(`No option with key "${propName}"`);
|
||||
}
|
||||
return target[propName];
|
||||
},
|
||||
set: (target, propName: string, value): any => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error(`No option with key "${propName}"`);
|
||||
}
|
||||
|
||||
value = this._sanitizeAndValidateOption(propName, value);
|
||||
// Don't fire an option change event if they didn't change
|
||||
if (target[propName] !== value) {
|
||||
target[propName] = value;
|
||||
this._onOptionChange.fire(propName);
|
||||
}
|
||||
return true;
|
||||
private _setupOptions(): void {
|
||||
const getter = (propName: string): any => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error(`No option with key "${propName}"`);
|
||||
}
|
||||
});
|
||||
return this.rawOptions[propName];
|
||||
};
|
||||
|
||||
const setter = (propName: string, value: any): void => {
|
||||
if (!(propName in DEFAULT_OPTIONS)) {
|
||||
throw new Error(`No option with key "${propName}"`);
|
||||
}
|
||||
|
||||
value = this._sanitizeAndValidateOption(propName, value);
|
||||
// Don't fire an option change event if they didn't change
|
||||
if (this.rawOptions[propName] !== value) {
|
||||
this.rawOptions[propName] = value;
|
||||
this._onOptionChange.fire(propName);
|
||||
}
|
||||
};
|
||||
|
||||
for (const propName in this.rawOptions) {
|
||||
const desc = {
|
||||
get: getter.bind(this, propName),
|
||||
set: setter.bind(this, propName)
|
||||
};
|
||||
Object.defineProperty(this.options, propName, desc);
|
||||
}
|
||||
}
|
||||
|
||||
public setOption(key: string, value: any): void {
|
||||
|
||||
@@ -188,6 +188,7 @@ export const IOptionsService = createDecorator<IOptionsService>('OptionsService'
|
||||
export interface IOptionsService {
|
||||
serviceBrand: undefined;
|
||||
|
||||
readonly rawOptions: Readonly<ITerminalOptions>;
|
||||
readonly options: ITerminalOptions;
|
||||
|
||||
readonly onOptionChange: IEvent<string>;
|
||||
|
||||
@@ -189,6 +189,10 @@ describe('API Integration Tests', function(): void {
|
||||
assert.equal(await page.evaluate(`window.term.options.fontSize`), 30);
|
||||
assert.equal(await page.evaluate(`window.term.options.fontFamily`), 'Arial');
|
||||
});
|
||||
it('object.keys return the correct number of options', async () => {
|
||||
await openTerminal(page);
|
||||
assert.notEqual(await page.evaluate(`Object.keys(window.term.options).length`), 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderer', () => {
|
||||
|
||||
Reference in New Issue
Block a user