From d02644f7c41150709795e57bfd40351b4da35a7b Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 23 Apr 2022 01:59:21 +0200 Subject: Preload safe debug shim (#29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In https://github.com/ferdium/ferdium-app/pull/23 we removed usages of the debug package due to an electron bug. This patch aims to restore some debug functionality by introducing a shim. The shim detect whether if it is being introduced in a preload script where the electron but would be triggered, and falls back to a simple replacement for debug. However, in the main and renderer processes, where a preload script is not being used, we still get full debug functionality. In this way, a module can be used both in a preload script and outside of it, while still preserving debug functionality whenever possible. Signed-off-by: Kristóf Marussy --- src/features/todos/preload.ts | 7 +++---- src/features/todos/store.js | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) (limited to 'src/features/todos') diff --git a/src/features/todos/preload.ts b/src/features/todos/preload.ts index 4ccee39a0..6c8bc1aea 100644 --- a/src/features/todos/preload.ts +++ b/src/features/todos/preload.ts @@ -1,10 +1,9 @@ import { ipcRenderer } from 'electron'; import { IPC } from './constants'; -// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed -// const debug = require('debug')('Ferdium:feature:todos:preload'); +const debug = require('../../preload-safe-debug')('Ferdium:feature:todos:preload'); -console.log('Preloading Todos Webview'); +debug('Preloading Todos Webview'); let hostMessageListener = ({ action }) => { switch (action) { @@ -28,7 +27,7 @@ window['ferdium'] = { }; ipcRenderer.on(IPC.TODOS_HOST_CHANNEL, (event, message) => { - console.log('Received host message', event, message); + debug('Received host message', event, message); hostMessageListener(message); }); diff --git a/src/features/todos/store.js b/src/features/todos/store.js index 41e632b49..9ece76327 100644 --- a/src/features/todos/store.js +++ b/src/features/todos/store.js @@ -18,8 +18,7 @@ import { createActionBindings } from '../utils/ActionBinding'; import { IPC, TODOS_ROUTES } from './constants'; import UserAgent from '../../models/UserAgent'; -// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed -// const debug = require('debug')('Ferdium:feature:todos:store'); +const debug = require('../../preload-safe-debug')('Ferdium:feature:todos:store'); export default class TodoStore extends FeatureStore { @observable stores = null; @@ -97,7 +96,7 @@ export default class TodoStore extends FeatureStore { // ========== PUBLIC API ========= // @action start(stores, actions) { - console.log('TodoStore::start'); + debug('TodoStore::start'); this.stores = stores; this.actions = actions; @@ -134,7 +133,7 @@ export default class TodoStore extends FeatureStore { @action stop() { super.stop(); - console.log('TodoStore::stop'); + debug('TodoStore::stop'); this.reset(); this.isFeatureActive = false; } @@ -163,7 +162,7 @@ export default class TodoStore extends FeatureStore { }; @action _setTodosWebview = ({ webview }) => { - console.log('_setTodosWebview', webview); + debug('_setTodosWebview', webview); if (this.webview !== webview) { this.webview = webview; this.userAgentModel.setWebviewReference(webview); @@ -171,14 +170,14 @@ export default class TodoStore extends FeatureStore { }; @action _handleHostMessage = message => { - console.log('_handleHostMessage', message); + debug('_handleHostMessage', message); if (message.action === 'todos:create') { this.webview.send(IPC.TODOS_HOST_CHANNEL, message); } }; @action _handleClientMessage = ({ channel, message = {} }) => { - console.log('_handleClientMessage', channel, message); + debug('_handleClientMessage', channel, message); switch (message.action) { case 'todos:initialized': this._onTodosClientInitialized(); @@ -187,7 +186,7 @@ export default class TodoStore extends FeatureStore { this._goToService(message.data); break; default: - console.log('Other message received', channel, message); + debug('Other message received', channel, message); if (this.stores.services.isTodosServiceAdded) { this.actions.service.handleIPCMessage({ serviceId: this.stores.services.isTodosServiceAdded.id, @@ -203,7 +202,7 @@ export default class TodoStore extends FeatureStore { }; @action _toggleTodosFeatureVisibility = () => { - console.log('_toggleTodosFeatureVisibility'); + debug('_toggleTodosFeatureVisibility'); this._updateSettings({ isFeatureEnabledByUser: !this.settings.isFeatureEnabledByUser, @@ -211,14 +210,14 @@ export default class TodoStore extends FeatureStore { }; _openDevTools = () => { - console.log('_openDevTools'); + debug('_openDevTools'); const webview = document.querySelector('#todos-panel webview'); if (webview) webview.openDevTools(); }; _reload = () => { - console.log('_reload'); + debug('_reload'); const webview = document.querySelector('#todos-panel webview'); if (webview) webview.reload(); @@ -286,7 +285,7 @@ export default class TodoStore extends FeatureStore { const { pathname } = this.stores.router.location; if (pathname === TODOS_ROUTES.TARGET) { - console.log('Router is on todos route, show todos panel'); + debug('Router is on todos route, show todos panel'); // todosStore.start(stores, actions); this.stores.router.push('/'); -- cgit v1.2.3-54-g00ecf