-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add onDataChanged method to handle data changes in the plugin #16244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,20 +210,48 @@ export const afterLoadPlugin = (plugin: Plugin) => { | |
| /// #endif | ||
| }; | ||
|
|
||
| export const reloadPlugin = async (app: App, data: { upsertPlugins: string[], removePlugins: string[] }) => { | ||
| data.removePlugins.forEach((item) => { | ||
| export const reloadPlugin = async (app: App, data: { upsertCodePlugins?: string[], upsertDataPlugins?: string[], removePlugins?: string[] } = {}) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. upsertDataPlugins 会有对应的 removeData 么?这个参数是 Data,应该不用加 plugins 后缀了。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 插件删除配置文件也算在 upsertDataPlugins 内 |
||
| const { upsertCodePlugins = [], upsertDataPlugins = [], removePlugins = [] } = data; | ||
| const reloadPlugins: string[] = []; | ||
|
|
||
| removePlugins.forEach((item) => { | ||
| uninstall(app, item, true); | ||
| }); | ||
| data.upsertPlugins.forEach((item) => { | ||
| uninstall(app, item, false); | ||
|
|
||
| upsertCodePlugins.forEach((pluginName) => { | ||
| reloadPlugins.push(pluginName); | ||
| }); | ||
| loadPlugins(app, data.upsertPlugins).then(() => { | ||
| app.plugins.forEach(item => { | ||
| if (data.upsertPlugins.includes(item.name)) { | ||
| afterLoadPlugin(item); | ||
|
|
||
| upsertDataPlugins.forEach((pluginName) => { | ||
| const plugin = app.plugins.find(p => p.name === pluginName); | ||
| // 检查插件是否重写了 onDataChanged 方法(不是基类的默认实现) | ||
| const hasOverriddenOnDataChanged = plugin && | ||
| typeof plugin.onDataChanged === "function" && | ||
| plugin.onDataChanged !== Plugin.prototype.onDataChanged; | ||
| if (hasOverriddenOnDataChanged) { | ||
| try { | ||
| plugin.onDataChanged(); | ||
| return; | ||
| } catch (e) { | ||
| console.error(`plugin ${pluginName} onDataChanged error:`, e); | ||
| } | ||
| }); | ||
| } | ||
| reloadPlugins.push(pluginName); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要去重
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }); | ||
|
|
||
| reloadPlugins.forEach((item) => { | ||
| uninstall(app, item, false); | ||
| }); | ||
| if (reloadPlugins.length > 0) { | ||
| loadPlugins(app, reloadPlugins).then(() => { | ||
| app.plugins.forEach(item => { | ||
| if (reloadPlugins.includes(item.name)) { | ||
| afterLoadPlugin(item); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /// #if !MOBILE | ||
| saveLayout(); | ||
| /// #endif | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upsertCodePlugins 这个为什么要加 Code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件代码有变化就需要重新加载插件