Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

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

Jeffrey Chenec881a76afcc
diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts
index e2447d448c..d3dfc2f776 100644
--- a/app/src/plugin/loader.ts
+++ b/app/src/plugin/loader.ts
@@ -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) => {
diff --git a/kernel/api/petal.go b/kernel/api/petal.go
index 706a669a56..b9e45a4884 100644
--- a/kernel/api/petal.go
+++ b/kernel/api/petal.go
@@ -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)
}
}
diff --git a/kernel/model/bazzar.go b/kernel/model/bazzar.go
index 31e56eb4d0..a597c0d573 100644
--- a/kernel/model/bazzar.go
+++ b/kernel/model/bazzar.go
@@ -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
}
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 2a85eb100a..0b5e54dd7d 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -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,
})
}
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index 084072452a..d316134726 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -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() {