Skip to content

Commit 9bfe00c

Browse files
authored
🧑‍💻 Add onDataChanged method to handle data changes in the plugin (#16244)
1 parent 319cdbb commit 9bfe00c

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
@@ -743,7 +743,8 @@ export const bazaar = {
743743
app.plugins.find((item: Plugin) => {
744744
if (item.name === dataObj.name) {
745745
reloadPlugin(app, {
746-
upsertPlugins: [dataObj.name],
746+
upsertCodePlugins: [dataObj.name],
747+
upsertDataPlugins: [],
747748
removePlugins: []
748749
});
749750
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
@@ -1594,7 +1594,8 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
15941594
var upsertTrees int
15951595
// 可能需要重新加载部分功能
15961596
var needReloadFlashcard, needReloadOcrTexts, needReloadPlugin bool
1597-
upsertPluginSet := hashset.New()
1597+
upsertCodePluginSet := hashset.New() // 插件代码变更 data/plugins/
1598+
upsertDataPluginSet := hashset.New() // 插件存储数据变更 data/storage/petal/
15981599
needUnindexBoxes, needIndexBoxes := map[string]bool{}, map[string]bool{}
15991600
for _, file := range mergeResult.Upserts {
16001601
upserts = append(upserts, file.Path)
@@ -1617,15 +1618,15 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16171618
needReloadPlugin = true
16181619
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
16191620
if pluginName := parts[3]; "petals.json" != pluginName {
1620-
upsertPluginSet.Add(pluginName)
1621+
upsertDataPluginSet.Add(pluginName)
16211622
}
16221623
}
16231624
}
16241625

16251626
if strings.HasPrefix(file.Path, "/plugins/") {
16261627
if parts := strings.Split(file.Path, "/"); 2 < len(parts) {
16271628
needReloadPlugin = true
1628-
upsertPluginSet.Add(parts[2])
1629+
upsertCodePluginSet.Add(parts[2])
16291630
}
16301631
}
16311632

@@ -1655,7 +1656,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16551656
needReloadPlugin = true
16561657
if parts := strings.Split(file.Path, "/"); 3 < len(parts) {
16571658
if pluginName := parts[3]; "petals.json" != pluginName {
1658-
removePluginSet.Add(pluginName)
1659+
upsertDataPluginSet.Add(pluginName)
16591660
}
16601661
}
16611662
}
@@ -1676,7 +1677,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16761677

16771678
for _, upsertPetal := range mergeResult.UpsertPetals {
16781679
needReloadPlugin = true
1679-
upsertPluginSet.Add(upsertPetal)
1680+
upsertCodePluginSet.Add(upsertPetal)
16801681
}
16811682
for _, removePetal := range mergeResult.RemovePetals {
16821683
needReloadPlugin = true
@@ -1692,7 +1693,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16921693
}
16931694

16941695
if needReloadPlugin {
1695-
pushReloadPlugin(upsertPluginSet, removePluginSet, "")
1696+
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, removePluginSet, "")
16961697
}
16971698

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

0 commit comments

Comments
 (0)