From bcc9ff5c1e00b447ab4e7987e8e2e7d65482231a Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com> Date: Tue, 15 Dec 2020 15:25:03 -0800 Subject: [PATCH] avoid IPC sendSync in render() --- src/main/ScratchDesktopTelemetry.js | 10 +++++----- src/renderer/app.jsx | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/ScratchDesktopTelemetry.js b/src/main/ScratchDesktopTelemetry.js index 1dbecf0..802b8f9 100644 --- a/src/main/ScratchDesktopTelemetry.js +++ b/src/main/ScratchDesktopTelemetry.js @@ -93,16 +93,16 @@ class ScratchDesktopTelemetry { // make a singleton so it's easy to share across both Electron processes const scratchDesktopTelemetrySingleton = new ScratchDesktopTelemetry(); +// `handle` works with `invoke` +ipcMain.handle('getTelemetryDidOptIn', () => + scratchDesktopTelemetrySingleton.didOptIn +); +// `on` works with `sendSync` (and `send`) ipcMain.on('getTelemetryDidOptIn', event => { event.returnValue = scratchDesktopTelemetrySingleton.didOptIn; }); ipcMain.on('setTelemetryDidOptIn', (event, arg) => { scratchDesktopTelemetrySingleton.didOptIn = arg; - event.returnValue = null; -}); -ipcMain.on('clearTelemetryDidOptIn', event => { - scratchDesktopTelemetrySingleton.clearDidOptIn(); - event.returnValue = null; }); ipcMain.on('projectDidLoad', (event, arg) => { scratchDesktopTelemetrySingleton.projectDidLoad(arg); diff --git a/src/renderer/app.jsx b/src/renderer/app.jsx index eb1a638..56438c4 100644 --- a/src/renderer/app.jsx +++ b/src/renderer/app.jsx @@ -46,6 +46,10 @@ const ScratchDesktopHOC = function (WrappedComponent) { 'handleTelemetryModalOptOut', 'handleUpdateProjectTitle' ]); + this.state = { + // use `sendSync` because this should be set before first render + telemetryDidOptIn: ipcRenderer.sendSync('getTelemetryDidOptIn') + }; this.props.onLoadingStarted(); ipcRenderer.invoke('get-initial-project-data').then(initialProjectData => { const hasInitialProject = initialProjectData && (initialProjectData.length > 0); @@ -99,18 +103,21 @@ const ScratchDesktopHOC = function (WrappedComponent) { } handleTelemetryModalOptIn () { ipcRenderer.send('setTelemetryDidOptIn', true); - this.forceUpdate(); + ipcRenderer.invoke('getTelemetryDidOptIn').then(telemetryDidOptIn => { + this.setState({telemetryDidOptIn}); + }); } handleTelemetryModalOptOut () { ipcRenderer.send('setTelemetryDidOptIn', false); - this.forceUpdate(); + ipcRenderer.invoke('getTelemetryDidOptIn').then(telemetryDidOptIn => { + this.setState({telemetryDidOptIn}); + }); } handleUpdateProjectTitle (newTitle) { this.setState({projectTitle: newTitle}); } render () { - const currentTelemetryState = ipcRenderer.sendSync('getTelemetryDidOptIn'); - const shouldShowTelemetryModal = (typeof currentTelemetryState !== 'boolean'); + const shouldShowTelemetryModal = (typeof this.state.telemetryDidOptIn !== 'boolean'); const childProps = omit(this.props, Object.keys(ScratchDesktopComponent.propTypes)); @@ -119,7 +126,7 @@ const ScratchDesktopHOC = function (WrappedComponent) { canModifyCloudData={false} canSave={false} isScratchDesktop - isTelemetryEnabled={currentTelemetryState} + isTelemetryEnabled={this.state.telemetryDidOptIn} showTelemetryModal={shouldShowTelemetryModal} onClickAbout={[ {