From 321395d96ed8d375b2917d9a0bb2c2d3a8c57676 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 19 Oct 2020 02:24:51 +0200 Subject: [PATCH] refactor: use Map instead of Object for better semantics (#25982) --- lib/asar/fs-wrapper.ts | 8 +++---- lib/browser/api/touch-bar.ts | 18 +++++++-------- lib/browser/guest-view-manager.ts | 38 +++++++++++++++---------------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lib/asar/fs-wrapper.ts b/lib/asar/fs-wrapper.ts index d03db08b2450..34f5a4ecc686 100644 --- a/lib/asar/fs-wrapper.ts +++ b/lib/asar/fs-wrapper.ts @@ -216,16 +216,16 @@ const makePromiseFunction = function (orig: Function, pathArgumentIndex: number) // Override fs APIs. export const wrapFsWithAsar = (fs: Record) => { - const logFDs: Record = {}; + const logFDs = new Map(); const logASARAccess = (asarPath: string, filePath: string, offset: number) => { if (!process.env.ELECTRON_LOG_ASAR_READS) return; - if (!logFDs[asarPath]) { + if (!logFDs.has(asarPath)) { const path = require('path'); const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`; const logPath = path.join(require('os').tmpdir(), logFilename); - logFDs[asarPath] = fs.openSync(logPath, 'a'); + logFDs.set(asarPath, fs.openSync(logPath, 'a')); } - fs.writeSync(logFDs[asarPath], `${offset}: ${filePath}\n`); + fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`); }; const { lstatSync } = fs; diff --git a/lib/browser/api/touch-bar.ts b/lib/browser/api/touch-bar.ts index 1ce8b272f97e..0e6b92eafaa8 100644 --- a/lib/browser/api/touch-bar.ts +++ b/lib/browser/api/touch-bar.ts @@ -300,8 +300,8 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { touchBar._addToWindow(window); } - private windowListeners: Record = {}; - private items: Record> = {}; + private windowListeners = new Map(); + private items = new Map>(); orderedItems: TouchBarItem[] = []; constructor (options: Electron.TouchBarConstructorOptions) { @@ -317,12 +317,10 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { items = []; } - this.windowListeners = {}; - this.items = {}; this.escapeItem = (escapeItem as any) || null; const registerItem = (item: TouchBarItem) => { - this.items[item.id] = item; + this.items.set(item.id, item); item.on('change', this.changeListener); if (item.child instanceof TouchBar) { item.child.orderedItems.forEach(registerItem); @@ -387,7 +385,7 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { const { id } = window; // Already added to window - if (Object.prototype.hasOwnProperty.call(this.windowListeners, id)) return; + if (this.windowListeners.has(id)) return; window._touchBar = this; @@ -402,7 +400,7 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { this.on('escape-item-change', escapeItemListener); const interactionListener = (_: any, itemID: string, details: any) => { - let item = this.items[itemID]; + let item = this.items.get(itemID); if (item == null && this.escapeItem != null && this.escapeItem.id === itemID) { item = this.escapeItem; } @@ -418,7 +416,7 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { window.removeListener('-touch-bar-interaction', interactionListener); window.removeListener('closed', removeListeners); window._touchBar = null; - delete this.windowListeners[id]; + this.windowListeners.delete(id); const unregisterItems = (items: TouchBarItem[]) => { for (const item of items) { item.removeListener('change', this.changeListener); @@ -433,14 +431,14 @@ class TouchBar extends EventEmitter implements Electron.TouchBar { } }; window.once('closed', removeListeners); - this.windowListeners[id] = removeListeners; + this.windowListeners.set(id, removeListeners); window._setTouchBarItems(this.orderedItems); escapeItemListener(this.escapeItem); } _removeFromWindow (window: Electron.BrowserWindow) { - const removeListeners = this.windowListeners[window.id]; + const removeListeners = this.windowListeners.get(window.id); if (removeListeners != null) removeListeners(); } diff --git a/lib/browser/guest-view-manager.ts b/lib/browser/guest-view-manager.ts index 9135a4c442ba..b57fdecad4e6 100644 --- a/lib/browser/guest-view-manager.ts +++ b/lib/browser/guest-view-manager.ts @@ -18,8 +18,8 @@ const webViewManager = process._linkedBinding('electron_browser_web_view_manager const supportedWebViewEvents = Object.keys(webViewEvents); -const guestInstances: Record = {}; -const embedderElementsMap: Record = {}; +const guestInstances = new Map(); +const embedderElementsMap = new Map(); function sanitizeOptionsForGuest (options: Record) { const ret = { ...options }; @@ -37,14 +37,14 @@ const createGuest = function (embedder: Electron.WebContents, params: Record { - if (Object.prototype.hasOwnProperty.call(guestInstances, guestInstanceId)) { + if (guestInstances.has(guestInstanceId)) { detachGuest(embedder, guestInstanceId); } }); @@ -107,7 +107,7 @@ const createGuest = function (embedder: Electron.WebContents, params: Record