fix: add a "set" trap to the "screen" module proxy (#26818)

This commit is contained in:
Alexey Kuzmin 2020-12-07 20:20:50 +03:00 коммит произвёл GitHub
Родитель d3b1566181
Коммит e89b3ca1d1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 35 добавлений и 21 удалений

Просмотреть файл

@ -2,37 +2,39 @@ const { createScreen } = process._linkedBinding('electron_common_screen');
let _screen: Electron.Screen;
const createScreenIfNeeded = () => {
if (_screen === undefined) {
_screen = createScreen();
}
};
// We can't call createScreen until after app.on('ready'), but this module
// exposes an instance created by createScreen. In order to avoid
// side-effecting and calling createScreen upon import of this module, instead
// we export a proxy which lazily calls createScreen on first access.
export default new Proxy({}, {
get: (target, prop: keyof Electron.Screen) => {
if (_screen === undefined) {
_screen = createScreen();
get: (target, property: keyof Electron.Screen) => {
createScreenIfNeeded();
const value = _screen[property];
if (typeof value === 'function') {
return value.bind(_screen);
}
const v = _screen[prop];
if (typeof v === 'function') {
return v.bind(_screen);
}
return v;
return value;
},
set: (target, property: string, value: unknown) => {
createScreenIfNeeded();
return Reflect.set(_screen, property, value);
},
ownKeys: () => {
if (_screen === undefined) {
_screen = createScreen();
}
createScreenIfNeeded();
return Reflect.ownKeys(_screen);
},
has: (target, prop: string) => {
if (_screen === undefined) {
_screen = createScreen();
}
return prop in _screen;
has: (target, property: string) => {
createScreenIfNeeded();
return property in _screen;
},
getOwnPropertyDescriptor: (target, prop: string) => {
if (_screen === undefined) {
_screen = createScreen();
}
return Reflect.getOwnPropertyDescriptor(_screen, prop);
getOwnPropertyDescriptor: (target, property: string) => {
createScreenIfNeeded();
return Reflect.getOwnPropertyDescriptor(_screen, property);
}
});

Просмотреть файл

@ -2,6 +2,18 @@ import { expect } from 'chai';
import { screen } from 'electron/main';
describe('screen module', () => {
describe('methods reassignment', () => {
it('works for a selected method', () => {
const originalFunction = screen.getPrimaryDisplay;
try {
(screen as any).getPrimaryDisplay = () => null;
expect(screen.getPrimaryDisplay()).to.be.null();
} finally {
screen.getPrimaryDisplay = originalFunction;
}
});
});
describe('screen.getCursorScreenPoint()', () => {
it('returns a point object', () => {
const point = screen.getCursorScreenPoint();