avoid IPC sendSync in render()

This commit is contained in:
Christopher Willis-Ford 2020-12-15 15:25:03 -08:00
parent 6e840825cc
commit bcc9ff5c1e
2 changed files with 17 additions and 10 deletions

View file

@ -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);

View file

@ -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={[
{