mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-24 17:50:24 +08:00
⚡ Perf(custom): improve the performance of clipboard watching
This commit is contained in:
@@ -1,51 +1,49 @@
|
||||
import { clipboard, nativeImage } from 'electron'
|
||||
import { clipboard } from 'electron'
|
||||
import { EventEmitter } from 'events'
|
||||
import crypto from 'crypto'
|
||||
import logger from '../apis/core/picgo/logger'
|
||||
|
||||
class ClipboardWatcher extends EventEmitter {
|
||||
lastImage: Electron.NativeImage | null
|
||||
timer: NodeJS.Timeout | null
|
||||
lastImageHash: string | null
|
||||
|
||||
constructor () {
|
||||
super()
|
||||
this.lastImage = null
|
||||
this.lastImageHash = null
|
||||
this.timer = null
|
||||
}
|
||||
|
||||
startListening (watchDelay = 500) {
|
||||
this.stopListening()
|
||||
|
||||
const image = clipboard.readImage()
|
||||
if (!image.isEmpty()) {
|
||||
const dataUrl = image.toDataURL()
|
||||
this.lastImage = nativeImage.createFromDataURL(dataUrl)
|
||||
}
|
||||
this.stopListening(false)
|
||||
|
||||
this.timer = setInterval(() => {
|
||||
const image = clipboard.readImage()
|
||||
if (image.isEmpty()) {
|
||||
if (image.isEmpty()) return
|
||||
|
||||
const currentImageHash = this.getImageHash(image)
|
||||
if (this.lastImageHash === null || this.lastImageHash === currentImageHash) {
|
||||
this.lastImageHash = currentImageHash
|
||||
return
|
||||
}
|
||||
|
||||
const dataUrl = image.toDataURL()
|
||||
const currentImage = nativeImage.createFromDataURL(dataUrl)
|
||||
|
||||
if (this.lastImage) {
|
||||
const lastDataUrl = this.lastImage.toDataURL()
|
||||
if (lastDataUrl === dataUrl) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
this.lastImage = currentImage
|
||||
this.emit('change', currentImage)
|
||||
this.lastImageHash = currentImageHash
|
||||
this.emit('change')
|
||||
}, watchDelay)
|
||||
logger.info('Start to watch clipboard')
|
||||
}
|
||||
|
||||
stopListening () {
|
||||
stopListening (isLog = true) {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
this.timer = null
|
||||
this.lastImageHash = null
|
||||
}
|
||||
isLog && logger.info('Stop to watch clipboard')
|
||||
}
|
||||
|
||||
getImageHash (image: Electron.NativeImage): string {
|
||||
const buffer = image.toBitmap()
|
||||
return crypto.createHash('md5').update(buffer).digest('hex')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user