fix(main-push): 支持 mainPushCallback 返回的异步结果和对象结果#487
Conversation
There was a problem hiding this comment.
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.
| let results = mainPushCallback.callback(queryData) | ||
| // MainPush结果可能是一个 Promise,需要等待结果 | ||
| if (results && typeof results.then === 'function') { | ||
| results = await results | ||
| } |
There was a problem hiding this comment.
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.
| let results = mainPushCallback.callback(queryData) | |
| // MainPush结果可能是一个 Promise,需要等待结果 | |
| if (results && typeof results.then === 'function') { | |
| results = await results | |
| } | |
| let results = await mainPushCallback.callback(queryData) |
| if (results && Array.isArray(results.data)) { | ||
| results = results.data | ||
| } |
There was a problem hiding this comment.
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.
| if (results && Array.isArray(results.data)) { | |
| results = results.data | |
| } | |
| if (results && results.type === 'list' && Array.isArray(results.data)) { | |
| results = results.data | |
| } |
变更内容
修复了部分插件 main push 无法生效的问题
为 main push 的 callback 增加了处理两种返回值的能力:
{type: "list", data: [...]}动机
有一些插件的main push callback 返回值不是数组,有时候这个 callback 是异步的,有时候返回的是一个对象。之前的处理仅支持同步函数和返回数组,导致很多插件的 main push 代码不起作用
经过这个修改后,很多插件的 main push 都恢复正常了

相关 issue