Fix executing the uninstall method when closing the plugin (#16522)

* 🐛 Fix executing the uninstall method when closing the plugin

* 后端推送区分插件禁用与卸载
This commit is contained in:
Jeffrey Chen 2025-12-13 10:37:05 +08:00 committed by GitHub
parent ab62a13cb0
commit ec881a76af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 26 deletions

View file

@ -225,10 +225,16 @@ export const afterLoadPlugin = (plugin: Plugin) => {
export const reloadPlugin = async (app: App, data: {
upsertCodePlugins?: string[],
upsertDataPlugins?: string[],
removePlugins?: string[]
unloadPlugins?: string[],
uninstallPlugins?: string[],
} = {}) => {
const {upsertCodePlugins = [], upsertDataPlugins = [], removePlugins = []} = data;
removePlugins.forEach((item) => {
const {upsertCodePlugins = [], upsertDataPlugins = [], unloadPlugins = [], uninstallPlugins = []} = data;
// 禁用
unloadPlugins.forEach((item) => {
uninstall(app, item, true);
});
// 卸载
uninstallPlugins.forEach((item) => {
uninstall(app, item, false);
});
upsertCodePlugins.forEach((item) => {

View file

@ -68,9 +68,9 @@ func setPetalEnabled(c *gin.Context) {
}
if enabled {
upsertPluginCodeSet := hashset.New(packageName)
model.PushReloadPlugin(upsertPluginCodeSet, nil, nil, app)
model.PushReloadPlugin(upsertPluginCodeSet, nil, nil, nil, app)
} else {
removePluginSet := hashset.New(packageName)
model.PushReloadPlugin(nil, nil, removePluginSet, app)
unloadPluginSet := hashset.New(packageName)
model.PushReloadPlugin(nil, nil, unloadPluginSet, nil, app)
}
}

View file

@ -256,8 +256,8 @@ func UninstallBazaarPlugin(pluginName, frontend string) error {
petals = tmp
savePetals(petals)
removePluginSet := hashset.New(pluginName)
PushReloadPlugin(nil, nil, removePluginSet, "")
uninstallPluginSet := hashset.New(pluginName)
PushReloadPlugin(nil, nil, nil, uninstallPluginSet, "")
return nil
}

View file

@ -38,11 +38,25 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginNameSet *hashset.Set, excludeApp string) {
if nil != removePluginNameSet {
for _, n := range removePluginNameSet.Values() {
func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginNameSet, uninstallPluginNameSet *hashset.Set, excludeApp string) {
// 集合去重
if nil != uninstallPluginNameSet {
for _, n := range uninstallPluginNameSet.Values() {
pluginName := n.(string)
if nil != upsertCodePluginSet {
upsertCodePluginSet.Remove(pluginName)
}
if nil != upsertDataPluginSet {
upsertDataPluginSet.Remove(pluginName)
}
if nil != unloadPluginNameSet {
unloadPluginNameSet.Remove(pluginName)
}
}
}
if nil != unloadPluginNameSet {
for _, n := range unloadPluginNameSet.Values() {
pluginName := n.(string)
// 如果插件在 removePluginSet 中,从其他集合中移除
if nil != upsertCodePluginSet {
upsertCodePluginSet.Remove(pluginName)
}
@ -54,14 +68,13 @@ func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginName
if nil != upsertCodePluginSet {
for _, n := range upsertCodePluginSet.Values() {
pluginName := n.(string)
// 如果插件在 upsertCodePluginSet 中,从 upsertDataPluginSet 中移除
if nil != upsertDataPluginSet {
upsertDataPluginSet.Remove(pluginName)
}
}
}
upsertCodePlugins, upsertDataPlugins, removePlugins := []string{}, []string{}, []string{}
upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins := []string{}, []string{}, []string{}, []string{}
if nil != upsertCodePluginSet {
for _, n := range upsertCodePluginSet.Values() {
upsertCodePlugins = append(upsertCodePlugins, n.(string))
@ -72,22 +85,28 @@ func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginName
upsertDataPlugins = append(upsertDataPlugins, n.(string))
}
}
if nil != removePluginNameSet {
for _, n := range removePluginNameSet.Values() {
removePlugins = append(removePlugins, n.(string))
if nil != unloadPluginNameSet {
for _, n := range unloadPluginNameSet.Values() {
unloadPlugins = append(unloadPlugins, n.(string))
}
}
if nil != uninstallPluginNameSet {
for _, n := range uninstallPluginNameSet.Values() {
uninstallPlugins = append(uninstallPlugins, n.(string))
}
}
pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins, excludeApp)
pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins, excludeApp)
}
func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins []string, excludeApp string) {
logging.LogInfof("reload plugins [codeChanges=%v, dataChanges=%v, removes=%v]", upsertCodePlugins, upsertDataPlugins, removePlugins)
func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins []string, excludeApp string) {
logging.LogInfof("reload plugins [codeChanges=%v, dataChanges=%v, unloads=%v, uninstalls=%v]", upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins)
if "" == excludeApp {
util.BroadcastByType("main", "reloadPlugin", 0, "", map[string]interface{}{
"upsertCodePlugins": upsertCodePlugins,
"upsertDataPlugins": upsertDataPlugins,
"removePlugins": removePlugins,
"unloadPlugins": unloadPlugins,
"uninstallPlugins": uninstallPlugins,
})
return
}
@ -95,7 +114,8 @@ func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins []str
util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", map[string]interface{}{
"upsertCodePlugins": upsertCodePlugins,
"upsertDataPlugins": upsertDataPlugins,
"removePlugins": removePlugins,
"unloadPlugins": unloadPlugins,
"uninstallPlugins": uninstallPlugins,
})
}

View file

@ -1635,7 +1635,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
}
}
removeWidgetDirSet, removePluginSet := hashset.New(), hashset.New()
removeWidgetDirSet, unloadPluginSet, uninstallPluginSet := hashset.New(), hashset.New(), hashset.New()
for _, file := range mergeResult.Removes {
removes = append(removes, file.Path)
if strings.HasPrefix(file.Path, "/storage/riff/") {
@ -1664,7 +1664,8 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
if strings.HasPrefix(file.Path, "/plugins/") {
if parts := strings.Split(file.Path, "/"); 2 < len(parts) {
needReloadPlugin = true
removePluginSet.Add(parts[2])
// 删除插件目录:卸载
uninstallPluginSet.Add(parts[2])
}
}
@ -1681,7 +1682,8 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
}
for _, removePetal := range mergeResult.RemovePetals {
needReloadPlugin = true
removePluginSet.Add(removePetal)
// Petal 中删除插件:卸载
uninstallPluginSet.Add(removePetal)
}
if needReloadFlashcard {
@ -1693,7 +1695,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
}
if needReloadPlugin {
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginSet, "")
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginSet, uninstallPluginSet, "")
}
for _, widgetDir := range removeWidgetDirSet.Values() {