Skip to content

Commit b13d11c

Browse files
committed
Resolved issue of event listeners piling up and not being removed after use
1 parent 30b4646 commit b13d11c

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

app/electron/preload.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const { existsSync, writeFileSync, mkdirSync, writeFile } = require('fs');
55
const formatCode = require('./preloadFunctions/format');
66
const {
77
chooseAppDir,
8-
appDirChosen
8+
addAppDirChosenListener,
9+
removeAllAppDirChosenListeners
910
} = require('./preloadFunctions/chooseAppDir');
1011

1112
// Expose protected methods that allow the renderer process to use
@@ -19,7 +20,8 @@ const {
1920
contextBridge.exposeInMainWorld('api', {
2021
formatCode: formatCode,
2122
chooseAppDir: chooseAppDir,
22-
appDirChosen: appDirChosen,
23+
addAppDirChosenListener: addAppDirChosenListener,
24+
removeAllAppDirChosenListeners: removeAllAppDirChosenListeners,
2325
existsSync: existsSync,
2426
writeFileSync: writeFileSync,
2527
mkdirSync: mkdirSync,

app/electron/preloadFunctions/chooseAppDir.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ const chooseAppDir = () => {
66

77
// once an app directory is chosen, the main process will send an "app_dir_selected" event
88
// when this event occurs, exucte the callback passed in by the user
9-
const appDirChosen = callback => {
9+
const addAppDirChosenListener = callback => {
1010
return ipcRenderer.on('app_dir_selected', (event, path) => {
1111
callback(path);
1212
});
1313
};
1414

15-
module.exports = { chooseAppDir, appDirChosen };
15+
// removes all listeners for the app_dir_selected event
16+
// this is important because otherwise listeners will pile up and events will trigger multiple events
17+
const removeAllAppDirChosenListeners = () => {
18+
ipcRenderer.removeAllListeners('app_dir_selected');
19+
};
20+
21+
module.exports = {
22+
chooseAppDir,
23+
addAppDirChosenListener,
24+
removeAllAppDirChosenListeners
25+
};

app/src/containers/LeftContainer.tsx

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ const LeftContainer = (): JSX.Element => {
7979
primBtnAction: null,
8080
primBtnLabel: null,
8181
secBtnAction: () => {
82-
// TODO: Create reducer to delete components from state
8382
closeModal();
8483
}
8584
})
@@ -95,17 +94,6 @@ const LeftContainer = (): JSX.Element => {
9594
key={i}
9695
button
9796
onClick={() => chooseGenOptions(i)}
98-
// onClick={() =>
99-
// createApplication({
100-
// // path,
101-
// // trying this with an absolute path because the electron dialogue box isn't working
102-
// path: '/Users/tylersullberg/',
103-
// components,
104-
// genOption,
105-
// appName: 'reactype_app',
106-
// exportAppBool: null
107-
// })
108-
// }
10997
style={{
11098
border: '1px solid #3f51b5',
11199
marginBottom: '2%',
@@ -117,38 +105,32 @@ const LeftContainer = (): JSX.Element => {
117105
))}
118106
</List>
119107
);
120-
// const chooseAppDir = () => {
121-
// console.log('CALLED CHOOSE APP DIR: ', genOption);
122-
// window.api.chooseAppDir();
123-
// };
124-
108+
125109
// helper function called by showGenerateAppModal
126110
// this function will prompt the user to choose an app directory once they've chosen their export option
127111
const chooseGenOptions = (genOpt: number) => {
128112
// set export option: 0 --> export only components, 1 --> export full project
129-
130-
// setGenOption(genOpt);
131113
genOption = genOpt;
132-
console.log('CALLED CHOOSE GEN OPTION: ', genOption);
133-
// closeModal
134-
// exportProject('/Users', 'NEW PROJECT', genOpt, state.components, state.rootComponents);
135-
// closeModal();
136-
// Choose app dir
137-
// window.api.chooseAppDir;
138-
139114
window.api.chooseAppDir();
140-
141-
// closeModal
142-
143115
closeModal();
144116
};
145117

118+
// removes all listeners for the app_dir_selected event
119+
// this is important because otherwise listeners will pile up and events will trigger multiple events
120+
window.api.removeAllAppDirChosenListeners();
121+
146122
// add listener for when an app directory is chosen
147123
// when a directory is chosen, the callback will export the project to the chosen folder
148124
// Note: this listener is imported from the main process via preload.js
149-
window.api.appDirChosen(path => {
125+
window.api.addAppDirChosenListener(path => {
150126
console.log('CALLED APPDIRCHOSEN: ', genOption);
151-
exportProject(path, 'NEW PROJECT', genOption, state.components, state.rootComponents);
127+
exportProject(
128+
path,
129+
'NEW PROJECT',
130+
genOption,
131+
state.components,
132+
state.rootComponents
133+
);
152134
});
153135

154136
setModal(

0 commit comments

Comments
 (0)