Skip to content

fix(main-push): 支持 mainPushCallback 返回的异步结果和对象结果#487

Merged
lzx8589561 merged 2 commits into
ZToolsCenter:mainfrom
guopenghui:fix/main-push
May 12, 2026
Merged

fix(main-push): 支持 mainPushCallback 返回的异步结果和对象结果#487
lzx8589561 merged 2 commits into
ZToolsCenter:mainfrom
guopenghui:fix/main-push

Conversation

@guopenghui

Copy link
Copy Markdown
Contributor

变更内容

修复了部分插件 main push 无法生效的问题

为 main push 的 callback 增加了处理两种返回值的能力:

  • promise
  • {type: "list", data: [...]}

动机

有一些插件的main push callback 返回值不是数组,有时候这个 callback 是异步的,有时候返回的是一个对象。之前的处理仅支持同步函数和返回数组,导致很多插件的 main push 代码不起作用

经过这个修改后,很多插件的 main push 都恢复正常了
image

相关 issue

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the lazyMainPushQuery function in resources/preload.js to support asynchronous callbacks and handle result objects that wrap data in a data field. The review feedback suggests simplifying the asynchronous logic by directly awaiting the callback result and improving robustness by verifying the type property before extracting data from the result object.

Comment thread resources/preload.js Outdated
Comment on lines +123 to +127
let results = mainPushCallback.callback(queryData)
// MainPush结果可能是一个 Promise,需要等待结果
if (results && typeof results.then === 'function') {
results = await results
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling both synchronous and asynchronous results can be simplified. Since await works on both Promise and non-Promise values, you can directly await the callback result. This removes the need for the explicit thenable check and the intermediate variable assignment, making the code cleaner and more idiomatic.

Suggested change
let results = mainPushCallback.callback(queryData)
// MainPush结果可能是一个 Promise,需要等待结果
if (results && typeof results.then === 'function') {
results = await results
}
let results = await mainPushCallback.callback(queryData)

Comment thread resources/preload.js Outdated
Comment on lines +129 to +131
if (results && Array.isArray(results.data)) {
results = results.data
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To better align with the contract mentioned in the PR description ({type: 'list', data: [...]}), it is recommended to verify the type property before extracting the data field. This prevents accidental data extraction from other object structures that might contain a data array but are not intended to be result lists, ensuring better robustness.

Suggested change
if (results && Array.isArray(results.data)) {
results = results.data
}
if (results && results.type === 'list' && Array.isArray(results.data)) {
results = results.data
}

@lzx8589561 lzx8589561 merged commit 9aba5dd into ZToolsCenter:main May 12, 2026
@guopenghui guopenghui deleted the fix/main-push branch May 17, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants