Skip to content

Commit f387e77

Browse files
committed
🧑‍💻 Add onDataChanged method to handle data changes in the plugin
1 parent 33d6312 commit f387e77

File tree

7 files changed

+94
-35
lines changed

7 files changed

+94
-35
lines changed

app/src/config/bazaar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,8 @@ export const bazaar = {
798798
app.plugins.find((item: Plugin) => {
799799
if (item.name === dataObj.name) {
800800
reloadPlugin(app, {
801-
upsertPlugins: [dataObj.name],
801+
upsertCodePlugins: [dataObj.name],
802+
upsertDataPlugins: [],
802803
removePlugins: []
803804
});
804805
return true;

app/src/plugin/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export class Plugin {
113113
// 卸载
114114
}
115115

116+
public onDataChanged() {
117+
// 存储数据变更
118+
}
119+
116120
public async updateCards(options: ICardData) {
117121
return options;
118122
}

app/src/plugin/loader.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,48 @@ export const afterLoadPlugin = (plugin: Plugin) => {
210210
/// #endif
211211
};
212212

213-
export const reloadPlugin = async (app: App, data: { upsertPlugins: string[], removePlugins: string[] }) => {
214-
data.removePlugins.forEach((item) => {
213+
export const reloadPlugin = async (app: App, data: { upsertCodePlugins?: string[], upsertDataPlugins?: string[], removePlugins?: string[] } = {}) => {
214+
const { upsertCodePlugins = [], upsertDataPlugins = [], removePlugins = [] } = data;
215+
const reloadPlugins: string[] = [];
216+
217+
removePlugins.forEach((item) => {
215218
uninstall(app, item, true);
216219
});
217-
data.upsertPlugins.forEach((item) => {
218-
uninstall(app, item, false);
220+
221+
upsertCodePlugins.forEach((pluginName) => {
222+
reloadPlugins.push(pluginName);
219223
});
220-
loadPlugins(app, data.upsertPlugins).then(() => {
221-
app.plugins.forEach(item => {
222-
if (data.upsertPlugins.includes(item.name)) {
223-
afterLoadPlugin(item);
224+
225+
upsertDataPlugins.forEach((pluginName) => {
226+
const plugin = app.plugins.find(p => p.name === pluginName);
227+
// 检查插件是否重写了 onDataChanged 方法(不是基类的默认实现)
228+
const hasOverriddenOnDataChanged = plugin &&
229+
typeof plugin.onDataChanged === "function" &&
230+
plugin.onDataChanged !== Plugin.prototype.onDataChanged;
231+
if (hasOverriddenOnDataChanged) {
232+
try {
233+
plugin.onDataChanged();
234+
return;
235+
} catch (e) {
236+
console.error(`plugin ${pluginName} onDataChanged error:`, e);
224237
}
225-
});
238+
}
239+
reloadPlugins.push(pluginName);
226240
});
241+
242+
reloadPlugins.forEach((item) => {
243+
uninstall(app, item, false);
244+
});
245+
if (reloadPlugins.length > 0) {
246+
loadPlugins(app, reloadPlugins).then(() => {
247+
app.plugins.forEach(item => {
248+
if (reloadPlugins.includes(item.name)) {
249+
afterLoadPlugin(item);
250+
}
251+
});
252+
});
253+
}
254+
227255
/// #if !MOBILE
228256
saveLayout();
229257
/// #endif

kernel/api/petal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ func setPetalEnabled(c *gin.Context) {
6767
app = arg["app"].(string)
6868
}
6969
if enabled {
70-
upsertPluginSet := hashset.New(packageName)
71-
model.PushReloadPlugin(upsertPluginSet, nil, app)
70+
upsertPluginCodeSet := hashset.New(packageName)
71+
model.PushReloadPlugin(upsertPluginCodeSet, nil, nil, app)
7272
} else {
7373
removePluginSet := hashset.New(packageName)
74-
model.PushReloadPlugin(nil, removePluginSet, app)
74+
model.PushReloadPlugin(nil, nil, removePluginSet, app)
7575
}
7676
}

kernel/model/bazzar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func UninstallBazaarPlugin(pluginName, frontend string) error {
257257
savePetals(petals)
258258

259259
removePluginSet := hashset.New(pluginName)
260-
pushReloadPlugin(nil, removePluginSet, "")
260+
PushReloadPlugin(nil, nil, removePluginSet, "")
261261
return nil
262262
}
263263

kernel/model/push_reload.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,38 @@ import (
3838
"github.com/siyuan-note/siyuan/kernel/util"
3939
)
4040

41-
func PushReloadPlugin(upsertPluginSet, removePluginNameSet *hashset.Set, excludeApp string) {
42-
pushReloadPlugin(upsertPluginSet, removePluginNameSet, excludeApp)
43-
}
41+
func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginNameSet *hashset.Set, excludeApp string) {
42+
if nil != removePluginNameSet {
43+
for _, n := range removePluginNameSet.Values() {
44+
pluginName := n.(string)
45+
// 如果插件在 removePluginSet 中,从其他集合中移除
46+
if nil != upsertCodePluginSet {
47+
upsertCodePluginSet.Remove(pluginName)
48+
}
49+
if nil != upsertDataPluginSet {
50+
upsertDataPluginSet.Remove(pluginName)
51+
}
52+
}
53+
}
54+
if nil != upsertCodePluginSet {
55+
for _, n := range upsertCodePluginSet.Values() {
56+
pluginName := n.(string)
57+
// 如果插件在 upsertCodePluginSet 中,从 upsertDataPluginSet 中移除
58+
if nil != upsertDataPluginSet {
59+
upsertDataPluginSet.Remove(pluginName)
60+
}
61+
}
62+
}
4463

45-
func pushReloadPlugin(upsertPluginSet, removePluginNameSet *hashset.Set, excludeApp string) {
46-
upsertPlugins, removePlugins := []string{}, []string{}
47-
if nil != upsertPluginSet {
48-
for _, n := range upsertPluginSet.Values() {
49-
upsertPlugins = append(upsertPlugins, n.(string))
64+
upsertCodePlugins, upsertDataPlugins, removePlugins := []string{}, []string{}, []string{}
65+
if nil != upsertCodePluginSet {
66+
for _, n := range upsertCodePluginSet.Values() {
67+
upsertCodePlugins = append(upsertCodePlugins, n.(string))
68+
}
69+
}
70+
if nil != upsertDataPluginSet {
71+
for _, n := range upsertDataPluginSet.Values() {
72+
upsertDataPlugins = append(upsertDataPlugins, n.(string))
5073
}
5174
}
5275
if nil != removePluginNameSet {
@@ -55,22 +78,24 @@ func pushReloadPlugin(upsertPluginSet, removePluginNameSet *hashset.Set, exclude
5578
}
5679
}
5780

58-
pushReloadPlugin0(upsertPlugins, removePlugins, excludeApp)
81+
pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins, excludeApp)
5982
}
6083

61-
func pushReloadPlugin0(upsertPlugins, removePlugins []string, excludeApp string) {
62-
logging.LogInfof("reload plugins [upserts=%v, removes=%v]", upsertPlugins, removePlugins)
84+
func pushReloadPlugin0(upsertCodePlugins, upsertDataPlugins, removePlugins []string, excludeApp string) {
85+
logging.LogInfof("reload plugins [codeChanges=%v, dataChanges=%v, removes=%v]", upsertCodePlugins, upsertDataPlugins, removePlugins)
6386
if "" == excludeApp {
6487
util.BroadcastByType("main", "reloadPlugin", 0, "", map[string]interface{}{
65-
"upsertPlugins": upsertPlugins,
66-
"removePlugins": removePlugins,
88+
"upsertCodePlugins": upsertCodePlugins,
89+
"upsertDataPlugins": upsertDataPlugins,
90+
"removePlugins": removePlugins,
6791
})
6892
return
6993
}
7094

7195
util.BroadcastByTypeAndExcludeApp(excludeApp, "main", "reloadPlugin", 0, "", map[string]interface{}{
72-
"upsertPlugins": upsertPlugins,
73-
"removePlugins": removePlugins,
96+
"upsertCodePlugins": upsertCodePlugins,
97+
"upsertDataPlugins": upsertDataPlugins,
98+
"removePlugins": removePlugins,
7499
})
75100
}
76101

kernel/model/repository.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,8 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
15921592
var upsertTrees int
15931593
// 可能需要重新加载部分功能
15941594
var needReloadFlashcard, needReloadOcrTexts, needReloadPlugin bool
1595-
upsertPluginSet := hashset.New()
1595+
upsertCodePluginSet := hashset.New() // 插件代码变更 data/plugins/
1596+
upsertDataPluginSet := hashset.New() // 插件存储数据变更 data/storage/petal/
15961597
needUnindexBoxes, needIndexBoxes := map[string]bool{}, map[string]bool{}
15971598
for _, file := range mergeResult.Upserts {
15981599
upserts = append(upserts, file.Path)
@@ -1615,15 +1616,15 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16151616
needReloadPlugin = true
16161617
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
16171618
if pluginName := parts[3]; "petals.json" != pluginName {
1618-
upsertPluginSet.Add(pluginName)
1619+
upsertDataPluginSet.Add(pluginName)
16191620
}
16201621
}
16211622
}
16221623

16231624
if strings.HasPrefix(file.Path, "/plugins/") {
16241625
if parts := strings.Split(file.Path, "/"); 2 < len(parts) {
16251626
needReloadPlugin = true
1626-
upsertPluginSet.Add(parts[2])
1627+
upsertCodePluginSet.Add(parts[2])
16271628
}
16281629
}
16291630

@@ -1653,7 +1654,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16531654
needReloadPlugin = true
16541655
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
16551656
if pluginName := parts[3]; "petals.json" != pluginName {
1656-
removePluginSet.Add(pluginName)
1657+
upsertDataPluginSet.Add(pluginName)
16571658
}
16581659
}
16591660
}
@@ -1674,7 +1675,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16741675

16751676
for _, upsertPetal := range mergeResult.UpsertPetals {
16761677
needReloadPlugin = true
1677-
upsertPluginSet.Add(upsertPetal)
1678+
upsertCodePluginSet.Add(upsertPetal)
16781679
}
16791680
for _, removePetal := range mergeResult.RemovePetals {
16801681
needReloadPlugin = true
@@ -1690,7 +1691,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16901691
}
16911692

16921693
if needReloadPlugin {
1693-
pushReloadPlugin(upsertPluginSet, removePluginSet, "")
1694+
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginSet, "")
16941695
}
16951696

16961697
for _, widgetDir := range removeWidgetDirSet.Values() {

0 commit comments

Comments
 (0)