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 // make a singleton so it's easy to share across both Electron processes
const scratchDesktopTelemetrySingleton = new ScratchDesktopTelemetry(); const scratchDesktopTelemetrySingleton = new ScratchDesktopTelemetry();
// `handle` works with `invoke`
ipcMain.handle('getTelemetryDidOptIn', () =>
scratchDesktopTelemetrySingleton.didOptIn
);
// `on` works with `sendSync` (and `send`)
ipcMain.on('getTelemetryDidOptIn', event => { ipcMain.on('getTelemetryDidOptIn', event => {
event.returnValue = scratchDesktopTelemetrySingleton.didOptIn; event.returnValue = scratchDesktopTelemetrySingleton.didOptIn;
}); });
ipcMain.on('setTelemetryDidOptIn', (event, arg) => { ipcMain.on('setTelemetryDidOptIn', (event, arg) => {
scratchDesktopTelemetrySingleton.didOptIn = arg; scratchDesktopTelemetrySingleton.didOptIn = arg;
event.returnValue = null;
});
ipcMain.on('clearTelemetryDidOptIn', event => {
scratchDesktopTelemetrySingleton.clearDidOptIn();
event.returnValue = null;
}); });
ipcMain.on('projectDidLoad', (event, arg) => { ipcMain.on('projectDidLoad', (event, arg) => {
scratchDesktopTelemetrySingleton.projectDidLoad(arg); scratchDesktopTelemetrySingleton.projectDidLoad(arg);

View file

@ -46,6 +46,10 @@ const ScratchDesktopHOC = function (WrappedComponent) {
'handleTelemetryModalOptOut', 'handleTelemetryModalOptOut',
'handleUpdateProjectTitle' 'handleUpdateProjectTitle'
]); ]);
this.state = {
// use `sendSync` because this should be set before first render
telemetryDidOptIn: ipcRenderer.sendSync('getTelemetryDidOptIn')
};
this.props.onLoadingStarted(); this.props.onLoadingStarted();
ipcRenderer.invoke('get-initial-project-data').then(initialProjectData => { ipcRenderer.invoke('get-initial-project-data').then(initialProjectData => {
const hasInitialProject = initialProjectData && (initialProjectData.length > 0); const hasInitialProject = initialProjectData && (initialProjectData.length > 0);
@ -99,18 +103,21 @@ const ScratchDesktopHOC = function (WrappedComponent) {
} }
handleTelemetryModalOptIn () { handleTelemetryModalOptIn () {
ipcRenderer.send('setTelemetryDidOptIn', true); ipcRenderer.send('setTelemetryDidOptIn', true);
this.forceUpdate(); ipcRenderer.invoke('getTelemetryDidOptIn').then(telemetryDidOptIn => {
this.setState({telemetryDidOptIn});
});
} }
handleTelemetryModalOptOut () { handleTelemetryModalOptOut () {
ipcRenderer.send('setTelemetryDidOptIn', false); ipcRenderer.send('setTelemetryDidOptIn', false);
this.forceUpdate(); ipcRenderer.invoke('getTelemetryDidOptIn').then(telemetryDidOptIn => {
this.setState({telemetryDidOptIn});
});
} }
handleUpdateProjectTitle (newTitle) { handleUpdateProjectTitle (newTitle) {
this.setState({projectTitle: newTitle}); this.setState({projectTitle: newTitle});
} }
render () { render () {
const currentTelemetryState = ipcRenderer.sendSync('getTelemetryDidOptIn'); const shouldShowTelemetryModal = (typeof this.state.telemetryDidOptIn !== 'boolean');
const shouldShowTelemetryModal = (typeof currentTelemetryState !== 'boolean');
const childProps = omit(this.props, Object.keys(ScratchDesktopComponent.propTypes)); const childProps = omit(this.props, Object.keys(ScratchDesktopComponent.propTypes));
@ -119,7 +126,7 @@ const ScratchDesktopHOC = function (WrappedComponent) {
canModifyCloudData={false} canModifyCloudData={false}
canSave={false} canSave={false}
isScratchDesktop isScratchDesktop
isTelemetryEnabled={currentTelemetryState} isTelemetryEnabled={this.state.telemetryDidOptIn}
showTelemetryModal={shouldShowTelemetryModal} showTelemetryModal={shouldShowTelemetryModal}
onClickAbout={[ onClickAbout={[
{ {