mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-03-07 21:22:34 +01:00
♻️ refactor reloadPlugin (#17137)
This commit is contained in:
parent
5e0d1e64de
commit
487e27cb00
6 changed files with 54 additions and 89 deletions
|
|
@ -771,7 +771,7 @@ type="checkbox">
|
|||
app.plugins.find((item: Plugin) => {
|
||||
if (item.name === dataObj.name) {
|
||||
reloadPlugin(app, {
|
||||
upsertCodePlugins: [dataObj.name],
|
||||
reloadPlugins: [dataObj.name],
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -223,12 +223,12 @@ export const afterLoadPlugin = (plugin: Plugin) => {
|
|||
};
|
||||
|
||||
export const reloadPlugin = async (app: App, data: {
|
||||
upsertCodePlugins?: string[],
|
||||
upsertDataPlugins?: string[],
|
||||
unloadPlugins?: string[],
|
||||
uninstallPlugins?: string[],
|
||||
uninstallPlugins?: string[], // 插件卸载
|
||||
unloadPlugins?: string[], // 插件禁用
|
||||
reloadPlugins?: string[], // 插件启用,或插件代码变更
|
||||
dataChangePlugins?: string[], // 插件存储数据变更
|
||||
} = {}) => {
|
||||
const {upsertCodePlugins = [], upsertDataPlugins = [], unloadPlugins = [], uninstallPlugins = []} = data;
|
||||
const {uninstallPlugins = [], unloadPlugins = [], reloadPlugins = [], dataChangePlugins = []} = data;
|
||||
// 禁用
|
||||
unloadPlugins.forEach((item) => {
|
||||
uninstall(app, item, true);
|
||||
|
|
@ -237,12 +237,12 @@ export const reloadPlugin = async (app: App, data: {
|
|||
uninstallPlugins.forEach((item) => {
|
||||
uninstall(app, item, false);
|
||||
});
|
||||
upsertCodePlugins.forEach((item) => {
|
||||
reloadPlugins.forEach((item) => {
|
||||
uninstall(app, item, true);
|
||||
});
|
||||
loadPlugins(app, upsertCodePlugins, false).then(() => {
|
||||
loadPlugins(app, reloadPlugins, false).then(() => {
|
||||
app.plugins.forEach(item => {
|
||||
if (upsertCodePlugins.includes(item.name)) {
|
||||
if (reloadPlugins.includes(item.name)) {
|
||||
afterLoadPlugin(item);
|
||||
getAllEditor().forEach(editor => {
|
||||
editor.protyle.toolbar.update(editor.protyle);
|
||||
|
|
@ -251,7 +251,7 @@ export const reloadPlugin = async (app: App, data: {
|
|||
});
|
||||
});
|
||||
app.plugins.forEach(item => {
|
||||
if (upsertDataPlugins.includes(item.name)) {
|
||||
if (dataChangePlugins.includes(item.name)) {
|
||||
try {
|
||||
item.onDataChanged();
|
||||
} catch (e) {
|
||||
|
|
|
|||
|
|
@ -67,10 +67,10 @@ func setPetalEnabled(c *gin.Context) {
|
|||
app = arg["app"].(string)
|
||||
}
|
||||
if enabled {
|
||||
upsertPluginCodeSet := hashset.New(packageName)
|
||||
model.PushReloadPlugin(upsertPluginCodeSet, nil, nil, nil, app)
|
||||
reloadPluginSet := hashset.New(packageName)
|
||||
model.PushReloadPlugin(nil, nil, reloadPluginSet, nil, app)
|
||||
} else {
|
||||
unloadPluginSet := hashset.New(packageName)
|
||||
model.PushReloadPlugin(nil, nil, unloadPluginSet, nil, app)
|
||||
model.PushReloadPlugin(nil, unloadPluginSet, nil, nil, app)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -267,7 +267,7 @@ func UninstallBazaarPlugin(pluginName, frontend string) error {
|
|||
savePetals(petals)
|
||||
|
||||
uninstallPluginSet := hashset.New(pluginName)
|
||||
PushReloadPlugin(nil, nil, nil, uninstallPluginSet, "")
|
||||
PushReloadPlugin(uninstallPluginSet, nil, nil, nil, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,85 +43,50 @@ func PushReloadSnippet(snippet *conf.Snpt) {
|
|||
util.BroadcastByType("main", "setSnippet", 0, "", snippet)
|
||||
}
|
||||
|
||||
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)
|
||||
func PushReloadPlugin(uninstallPluginNameSet, unloadPluginNameSet, reloadPluginSet, dataChangePluginSet *hashset.Set, excludeApp string) {
|
||||
// 按优先级从高到低排列,同一插件只保留在优先级最高的集合中
|
||||
orderedSets := []*hashset.Set{uninstallPluginNameSet, unloadPluginNameSet, reloadPluginSet, dataChangePluginSet}
|
||||
slices := make([][]string, len(orderedSets))
|
||||
// 按顺序遍历所有集合
|
||||
for i, set := range orderedSets {
|
||||
if nil != set {
|
||||
// 遍历当前集合的所有插件名称
|
||||
for _, n := range set.Values() {
|
||||
name := n.(string)
|
||||
// 将该插件从所有后续集合中移除
|
||||
for _, lowerSet := range orderedSets[i+1:] {
|
||||
if nil != lowerSet {
|
||||
lowerSet.Remove(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if nil != unloadPluginNameSet {
|
||||
for _, n := range unloadPluginNameSet.Values() {
|
||||
pluginName := n.(string)
|
||||
if nil != upsertCodePluginSet {
|
||||
upsertCodePluginSet.Remove(pluginName)
|
||||
}
|
||||
if nil != upsertDataPluginSet {
|
||||
upsertDataPluginSet.Remove(pluginName)
|
||||
}
|
||||
}
|
||||
}
|
||||
if nil != upsertCodePluginSet {
|
||||
for _, n := range upsertCodePluginSet.Values() {
|
||||
pluginName := n.(string)
|
||||
if nil != upsertDataPluginSet {
|
||||
upsertDataPluginSet.Remove(pluginName)
|
||||
|
||||
// 将当前集合转换为字符串切片
|
||||
if nil == set {
|
||||
slices[i] = []string{}
|
||||
} else {
|
||||
strs := make([]string, 0, set.Size())
|
||||
for _, n := range set.Values() {
|
||||
strs = append(strs, n.(string))
|
||||
}
|
||||
slices[i] = strs
|
||||
}
|
||||
}
|
||||
|
||||
upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins := []string{}, []string{}, []string{}, []string{}
|
||||
if nil != upsertCodePluginSet {
|
||||
for _, n := range upsertCodePluginSet.Values() {
|
||||
upsertCodePlugins = append(upsertCodePlugins, n.(string))
|
||||
}
|
||||
}
|
||||
if nil != upsertDataPluginSet {
|
||||
for _, n := range upsertDataPluginSet.Values() {
|
||||
upsertDataPlugins = append(upsertDataPlugins, 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))
|
||||
}
|
||||
logging.LogInfof("reload plugins, uninstalls=%v, unloads=%v, reloads=%v, dataChanges=%v", slices[0], slices[1], slices[2], slices[3])
|
||||
payload := map[string]interface{}{
|
||||
"uninstallPlugins": slices[0], // 插件卸载
|
||||
"unloadPlugins": slices[1], // 插件禁用
|
||||
"reloadPlugins": slices[2], // 插件启用,或插件代码变更
|
||||
"dataChangePlugins": slices[3], // 插件存储数据变更
|
||||
}
|
||||
|
||||
pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, unloadPlugins, uninstallPlugins, excludeApp)
|
||||
}
|
||||
|
||||
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,
|
||||
"unloadPlugins": unloadPlugins,
|
||||
"uninstallPlugins": uninstallPlugins,
|
||||
})
|
||||
util.BroadcastByType("main", "reloadPlugin", 0, "", payload)
|
||||
return
|
||||
}
|
||||
|
||||
util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", map[string]interface{}{
|
||||
"upsertCodePlugins": upsertCodePlugins,
|
||||
"upsertDataPlugins": upsertDataPlugins,
|
||||
"unloadPlugins": unloadPlugins,
|
||||
"uninstallPlugins": uninstallPlugins,
|
||||
})
|
||||
util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", payload)
|
||||
}
|
||||
|
||||
func refreshDocInfo(tree *parse.Tree) {
|
||||
|
|
|
|||
|
|
@ -1596,8 +1596,8 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
|
|||
var upsertTrees int
|
||||
// 可能需要重新加载部分功能
|
||||
var needReloadFlashcard, needReloadOcrTexts, needReloadPlugin, needReloadSnippet bool
|
||||
upsertCodePluginSet := hashset.New() // 插件代码变更 data/plugins/
|
||||
upsertDataPluginSet := hashset.New() // 插件存储数据变更 data/storage/petal/
|
||||
reloadPluginSet := hashset.New() // 插件代码变更 data/plugins/
|
||||
dataChangePluginSet := hashset.New() // 插件存储数据变更 data/storage/petal/
|
||||
needUnindexBoxes, needIndexBoxes := map[string]bool{}, map[string]bool{}
|
||||
for _, file := range mergeResult.Upserts {
|
||||
upserts = append(upserts, file.Path)
|
||||
|
|
@ -1620,7 +1620,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
|
|||
needReloadPlugin = true
|
||||
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
|
||||
if pluginName := parts[3]; "petals.json" != pluginName {
|
||||
upsertDataPluginSet.Add(pluginName)
|
||||
dataChangePluginSet.Add(pluginName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1628,7 +1628,7 @@ 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
|
||||
upsertCodePluginSet.Add(parts[2])
|
||||
reloadPluginSet.Add(parts[2])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1667,7 +1667,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
|
|||
needReloadPlugin = true
|
||||
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
|
||||
if pluginName := parts[3]; "petals.json" != pluginName {
|
||||
upsertDataPluginSet.Add(pluginName)
|
||||
dataChangePluginSet.Add(pluginName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1698,7 +1698,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
|
|||
|
||||
for _, upsertPetal := range mergeResult.UpsertPetals {
|
||||
needReloadPlugin = true
|
||||
upsertCodePluginSet.Add(upsertPetal)
|
||||
reloadPluginSet.Add(upsertPetal)
|
||||
}
|
||||
for _, removePetal := range mergeResult.RemovePetals {
|
||||
needReloadPlugin = true
|
||||
|
|
@ -1715,7 +1715,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
|
|||
}
|
||||
|
||||
if needReloadPlugin {
|
||||
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginSet, uninstallPluginSet, "")
|
||||
PushReloadPlugin(uninstallPluginSet, unloadPluginSet, reloadPluginSet, dataChangePluginSet, "")
|
||||
}
|
||||
|
||||
if needReloadSnippet {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue