Repository: marktext/marktext
File: src/main/app/windowManager.js (JavaScript)

Code before:
    })
    ipcMain.on('window-change-file-path', (windowId, pathname, oldPathname) => {
      const editor = this.get(windowId)
      if (!editor) {
        log.error(`Cannot find window id "${windowId}" to change file path.`)
        return
      }
      editor.changeOpenedFilePath(pathname, oldPathname)
    })

    ipcMain.on('window-file-saved', (windowId, pathname) => {
      // A changed event is emitted earliest after the stability threshold.
      const duration = WATCHER_STABILITY_THRESHOLD + (WATCHER_STABILITY_POLL_INTERVAL * 2)
      this._watcher.ignoreChangedEvent(windowId, pathname, duration)
    })

    ipcMain.on('window-close-by-id', id => {
      this.forceCloseById(id)
    })
    ipcMain.on('window-reload-by-id', id => {
      const window = this.get(id)
      if (window) {
        window.reload()
      }
    })
    ipcMain.on('invalidate-image-cache-by-id', id => {
      const window = this.get(id)
      if (window) {
        window.invalidateImageCache()
      }
    })
    ipcMain.on('window-toggle-always-on-top', win => {
      const flag = !win.isAlwaysOnTop()
      win.setAlwaysOnTop(flag)
      this._appMenu.updateAlwaysOnTopMenu(win.id, flag)
    })

    ipcMain.on('broadcast-preferences-changed', prefs => {
      // We can not dynamic change the title bar style, so do not need to send it to renderer.
      if (typeof prefs.titleBarStyle !== 'undefined') {
        delete prefs.titleBarStyle
      }
      if (Object.keys(prefs).length > 0) {
        for (const { browserWindow } of this._windows.values()) {
          browserWindow.webContents.send('mt::user-preference', prefs)
        }
      }
    })

    ipcMain.on('broadcast-user-data-changed', userData => {
      for (const { browserWindow } of this._windows.values()) {

Review comment (performance):
You can directly pass the window (`focusedWindow`) from the menu and send the request without adding `invalidateImageCache` method.

Code after:
    })
    ipcMain.on('window-change-file-path', (windowId, pathname, oldPathname) => {
      const editor = this.get(windowId)
      if (!editor) {
        log.error(`Cannot find window id "${windowId}" to change file path.`)
        return
      }
      editor.changeOpenedFilePath(pathname, oldPathname)
    })

    ipcMain.on('window-file-saved', (windowId, pathname) => {
      // A changed event is emitted earliest after the stability threshold.
      const duration = WATCHER_STABILITY_THRESHOLD + (WATCHER_STABILITY_POLL_INTERVAL * 2)
      this._watcher.ignoreChangedEvent(windowId, pathname, duration)
    })

    ipcMain.on('window-close-by-id', id => {
      this.forceCloseById(id)
    })
    ipcMain.on('window-reload-by-id', id => {
      const window = this.get(id)
      if (window) {
        window.reload()
      }
    })
    ipcMain.on('window-toggle-always-on-top', win => {
      const flag = !win.isAlwaysOnTop()
      win.setAlwaysOnTop(flag)
      this._appMenu.updateAlwaysOnTopMenu(win.id, flag)
    })

    ipcMain.on('broadcast-preferences-changed', prefs => {
      // We can not dynamic change the title bar style, so do not need to send it to renderer.
      if (typeof prefs.titleBarStyle !== 'undefined') {
        delete prefs.titleBarStyle
      }
      if (Object.keys(prefs).length > 0) {
        for (const { browserWindow } of this._windows.values()) {
          browserWindow.webContents.send('mt::user-preference', prefs)
        }
      }
    })

    ipcMain.on('broadcast-user-data-changed', userData => {
      for (const { browserWindow } of this._windows.values()) {
        browserWindow.webContents.send('mt::user-preference', userData)
      }
    })
  }
}