Skip to content

Add a callback for closing the window #9128

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

Closed
WingDr opened this issue Sep 7, 2023 · 13 comments
Closed

Add a callback for closing the window #9128

WingDr opened this issue Sep 7, 2023 · 13 comments
Assignees
Milestone

Comments

@WingDr
Copy link

WingDr commented Sep 7, 2023

In what scenarios do you need this feature?

思源现在tab关闭的回调逻辑应该是直接异步执行,但是关闭窗口的时候这个进程会直接被关闭,也就是说onDestroy中的异步语句会直接被跳过,然而openWindow本身没有提供任何的关闭回调或者监听,因此想要在关闭窗口的时候进行一些操作(例如检测打开的笔记是否有添加内容,没有内容就直接删除临时文件)目前来说是不可能的

Describe the optimal solution

我感觉像关闭思源的时候会等待自动同步一样。关闭新窗口的时候也同步等待一下onDestroy完成会比较好。

Describe the candidate solution

当然也可以单独给openWindow加一个关闭的回调,但是这个的话如果中间又操作关闭打开了别的东西也会很混乱。

Other information

No response

@Vanessa219
Copy link
Member

监听这个事件试试 destroy-protyle

@WingDr
Copy link
Author

WingDr commented Sep 8, 2023

试了下,分别用不同的方法尝试了都有问题:

  1. 在新开的窗口里监听eventbus:函数的运行会再新窗口的线程里,结果就是和上面的onDestroy一样运行不出来(我看了console里确实是监听上了,也确实没运行
  2. 在调用openWindow的主线程里监听:完全监听不到新窗口里的protyle关闭

@Vanessa219
Copy link
Member

看了下代码,目前没有办法把 Event bus 做成同步的。但是关闭 Electron 时,内核还在运行的,应该可以在回调里面直接调用内核 API 对数据进行保存。

if (isWindow()) {
    getCurrentWindow().destroy();
    return;
}

@WingDr
Copy link
Author

WingDr commented Sep 9, 2023

看了下代码,目前没有办法把 Event bus 做成同步的。但是关闭 Electron 时,内核还在运行的,应该可以在回调里面直接调用内核 API 对数据进行保存。

不太行,我试了在addTab里的destroy里调用内核API(删除指定的一个文档),但是关闭窗口之后该文档并没有删除,应该就是没有执行。与此对比的是,如果我往新窗口中拖进一个新的tab,然后再关闭原先的tab,就成功删除了文档,说明函数运行的没问题,就是函数没执行就给截断了

destroy就是这么写的

destroy() {
    removeDoc(_this.newWindowBlock.box, _this.newWindowBlock.path)
}

也就是说调用内核api都没用,直接给截断了

有没有可能通过内核给新窗口和创建它的主线程之间加个通信管道?感觉会比较麻烦但是似乎没有更好的方法了,毕竟内核是一直开的,只要在关闭前能往主线程发一些东西就行了

@Vanessa219
Copy link
Member

自定义页签目前在新窗口中打开会报错,你那里是如何在新窗口中回调 destroy 的?
image

@WingDr
Copy link
Author

WingDr commented Sep 10, 2023

自定义页签目前在新窗口中打开会报错,你那里是如何在新窗口中回调 destroy 的?

我是在addTab里写destroy的,具体的代码如下:

image

image

第一张图是在插件onload调用的代码,第二张图是快捷键触发的代码。是用svelte挂载的

@Vanessa219
Copy link
Member

我试了一下,断点都可以,removeDoc 里面具体是怎么写的?在 beforeDestroy 里面试试?
image

@WingDr
Copy link
Author

WingDr commented Sep 10, 2023

removeDoc就是正常的用fetchSyncPost请求后端

image

image

我也测试到图里这一步了,这里应该是关闭tab能触发,但是如果直接右上角关闭窗口的话就不会到这。此外,如果beforeDestroy或者destroy本身是异步函数的话,只会运行前面所有的同步过程(一旦有await就会直接退出,也就是说在这里不可能和内核通信)

@Vanessa219
Copy link
Member

关闭窗口现在直接调用的 Electron 接口,没有做其他判断。

@Vanessa219 Vanessa219 reopened this Sep 10, 2023
@Vanessa219 Vanessa219 changed the title 关闭独立窗口时同步执行完打开的tab的onDestroy或者单独给一个回调 Add a callback for closing the window Sep 10, 2023
@Vanessa219 Vanessa219 self-assigned this Sep 10, 2023
@Vanessa219 Vanessa219 added this to the 2.10.5 milestone Sep 10, 2023
@WingDr
Copy link
Author

WingDr commented Sep 10, 2023

关闭窗口现在直接调用的 Electron 接口,没有做其他判断。

嗯嗯,就是说能不能在调用接口之前先同步执行一个回调函数?

@Vanessa219
Copy link
Member

目前修改为在关闭窗口的时候执行一个同步的 await plugin.onunload(); 我提交了你再看看。

Vanessa219 added a commit that referenced this issue Sep 16, 2023
Vanessa219 added a commit that referenced this issue Sep 16, 2023
Vanessa219 added a commit that referenced this issue Sep 16, 2023
@WingDr
Copy link
Author

WingDr commented Sep 16, 2023

目前修改为在关闭窗口的时候执行一个同步的 await plugin.onunload(); 我提交了你再看看。

okk,等dev版本我试试,看代码应该是可以

@AugustTuan
Copy link

目前修改为在关闭窗口的时候执行一个同步的 await plugin.onunload(); 我提交了你再看看。

okk,等dev版本我试试,看代码应该是可以

太棒了,感觉可以用上了。 再也不用天天切屏记笔记了😭

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

No branches or pull requests

4 participants