Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribe from handle #17

Closed
seahindeniz opened this issue Jun 4, 2023 · 3 comments · Fixed by #20
Closed

Unsubscribe from handle #17

seahindeniz opened this issue Jun 4, 2023 · 3 comments · Fixed by #20
Assignees

Comments

@seahindeniz
Copy link

Hi, is there a way to unsubscribe from an event?
Let's say, I have the following methods

// ipcs.ts

export const {
  ipcMain,
  ipcRenderer,
  exposeApiToGlobalWindow
} = createInterprocess({
  main: {
    async doSomething() {
      console.log('some logic here and call window');

      const mainWindow = BrowserWindow.getAllWindows().find(
        window => window.title === '[main]'
      );

      if (mainWindow) {
        ipcMain.invoke.sendToWindow(mainWindow, `here's the data`);
      }
    }
  },

  renderer: {
    async sendToWindow(_, data: string) {
      console.log('incoming data', data);
    }
  },
});
// renderer.ts

window.api.handle.sendToWindow(async () => {
  console.log('this is second handler');
  window.api.remove.sendToWindow();
})
window.api.invoke.doSomething();
setTimeout(() => {
  window.api.invoke.doSomething();
}, 5000)

When it runs, the first callback suppose to remove sendToWindow and then after 5 seconds, it should pop an error says, there's no handler available but it doesn't. Because the latest event handler window.api.handle.sendToWindow(async () => { }); has been registered in memory.
I believe this can also cause memory leak issue.

Can a handle call window.api.handle.sendToWindow(async () => {}); return a function to unsubscribe itself later on?

@daltonmenezes
Copy link
Owner

Hi @seahindeniz , in my tests the behavior is working as expected as you can see in this video:

Gravacao.de.Tela.2023-07-25.as.12.34.02.mov

Could you please provide a reproducible example of the issue you are experiencing?

@daltonmenezes
Copy link
Owner

@seahindeniz I think I get it! If I remove the handler from the main process and try to invoke it from the renderer, I get the expected throw behavior, but if I remove the handler from the renderer process and try to invoke it from the main, it doesn't throw as expected! I'll investigate!

@daltonmenezes
Copy link
Owner

@seahindeniz done, please update to v0.2.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants