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: { export const reloadPlugin = async (app: App, data: {
upsertCodePlugins?: string[], upsertCodePlugins?: string[],
upsertDataPlugins?: string[], upsertDataPlugins?: string[],
removePlugins?: string[] unloadPlugins?: string[],
uninstallPlugins?: string[],
} = {}) => { } = {}) => {
const {upsertCodePlugins = [], upsertDataPlugins = [], removePlugins = []} = data; const {upsertCodePlugins = [], upsertDataPlugins = [], unloadPlugins = [], uninstallPlugins = []} = data;
removePlugins.forEach((item) => { // 禁用
unloadPlugins.forEach((item) => {
uninstall(app, item, true);
});
// 卸载
uninstallPlugins.forEach((item) => {
uninstall(app, item, false); uninstall(app, item, false);
}); });
upsertCodePlugins.forEach((item) => { upsertCodePlugins.forEach((item) => {

View file

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

View file

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

View file

@ -38,11 +38,25 @@ import (
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
) )
func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginNameSet *hashset.Set, excludeApp string) { func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginNameSet, uninstallPluginNameSet *hashset.Set, excludeApp string) {
if nil != removePluginNameSet { // 集合去重
for _, n := range removePluginNameSet.Values() { 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) pluginName := n.(string)
// 如果插件在 removePluginSet 中,从其他集合中移除
if nil != upsertCodePluginSet { if nil != upsertCodePluginSet {
upsertCodePluginSet.Remove(pluginName) upsertCodePluginSet.Remove(pluginName)
} }
@ -54,14 +68,13 @@ func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginName
if nil != upsertCodePluginSet { if nil != upsertCodePluginSet {
for _, n := range upsertCodePluginSet.Values() { for _, n := range upsertCodePluginSet.Values() {
pluginName := n.(string) pluginName := n.(string)
// 如果插件在 upsertCodePluginSet 中,从 upsertDataPluginSet 中移除
if nil != upsertDataPluginSet { if nil != upsertDataPluginSet {
upsertDataPluginSet.Remove(pluginName) upsertDataPluginSet.Remove(pluginName)
} }
} }
} }
upsertCodePlugins, upsertDataPlugins, removePlugins := []string{}, []string{}, []string{} upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins := []string{}, []string{}, []string{}, []string{}
if nil != upsertCodePluginSet { if nil != upsertCodePluginSet {
for _, n := range upsertCodePluginSet.Values() { for _, n := range upsertCodePluginSet.Values() {
upsertCodePlugins = append(upsertCodePlugins, n.(string)) upsertCodePlugins = append(upsertCodePlugins, n.(string))
@ -72,22 +85,28 @@ func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginName
upsertDataPlugins = append(upsertDataPlugins, n.(string)) upsertDataPlugins = append(upsertDataPlugins, n.(string))
} }
} }
if nil != removePluginNameSet { if nil != unloadPluginNameSet {
for _, n := range removePluginNameSet.Values() { for _, n := range unloadPluginNameSet.Values() {
removePlugins = append(removePlugins, n.(string)) 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) { func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins []string, excludeApp string) {
logging.LogInfof("reload plugins [codeChanges=%v, dataChanges=%v, removes=%v]", upsertCodePlugins, upsertDataPlugins, removePlugins) logging.LogInfof("reload plugins [codeChanges=%v, dataChanges=%v, unloads=%v, uninstalls=%v]", upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins)
if "" == excludeApp { if "" == excludeApp {
util.BroadcastByType("main", "reloadPlugin", 0, "", map[string]interface{}{ util.BroadcastByType("main", "reloadPlugin", 0, "", map[string]interface{}{
"upsertCodePlugins": upsertCodePlugins, "upsertCodePlugins": upsertCodePlugins,
"upsertDataPlugins": upsertDataPlugins, "upsertDataPlugins": upsertDataPlugins,
"removePlugins": removePlugins, "unloadPlugins": unloadPlugins,
"uninstallPlugins": uninstallPlugins,
}) })
return return
} }
@ -95,7 +114,8 @@ func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins []str
util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", map[string]interface{}{ util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", map[string]interface{}{
"upsertCodePlugins": upsertCodePlugins, "upsertCodePlugins": upsertCodePlugins,
"upsertDataPlugins": upsertDataPlugins, "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 { for _, file := range mergeResult.Removes {
removes = append(removes, file.Path) removes = append(removes, file.Path)
if strings.HasPrefix(file.Path, "/storage/riff/") { 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 strings.HasPrefix(file.Path, "/plugins/") {
if parts := strings.Split(file.Path, "/"); 2 < len(parts) { if parts := strings.Split(file.Path, "/"); 2 < len(parts) {
needReloadPlugin = true 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 { for _, removePetal := range mergeResult.RemovePetals {
needReloadPlugin = true needReloadPlugin = true
removePluginSet.Add(removePetal) // Petal 中删除插件:卸载
uninstallPluginSet.Add(removePetal)
} }
if needReloadFlashcard { if needReloadFlashcard {
@ -1693,7 +1695,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
} }
if needReloadPlugin { if needReloadPlugin {
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginSet, "") PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginSet, uninstallPluginSet, "")
} }
for _, widgetDir := range removeWidgetDirSet.Values() { for _, widgetDir := range removeWidgetDirSet.Values() {