update
This commit is contained in:
25
lib/poll-guard.js
Normal file
25
lib/poll-guard.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 轮询守卫:决定本 tick 是否进入图片分支。
|
||||
*
|
||||
* 背景:默认 pollClipboard 每 tick 都跑 `clipboard.readImage().getBitmap()`
|
||||
* + sha1,剪贴板上有 4K 截图时约 33 MB memcpy + ~40ms。先用 availableFormats()
|
||||
* 探一下,文本剪贴板直接跳过图片分支,零开销。
|
||||
*
|
||||
* 已知残留:当剪贴板确实有大图时仍然每 tick 重哈希。彻底解决需要启发式
|
||||
* 风险(尺寸相同可能漏抓变更)或订阅 clipboard-sequence-number(Electron
|
||||
* 不暴露)。本次只做最稳妥的格式守卫。
|
||||
*
|
||||
* @param {unknown} formats 来自 clipboard.availableFormats() 的列表
|
||||
* @returns {boolean} 是否需要读图片
|
||||
*/
|
||||
function shouldReadImage(formats) {
|
||||
if (!Array.isArray(formats)) return false;
|
||||
for (const f of formats) {
|
||||
if (typeof f === 'string' && f.startsWith('image/')) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports = { shouldReadImage };
|
||||
Reference in New Issue
Block a user