mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-09-05 23:47:43 +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 { EventEmitter } from 'events'
|
||||||
|
import crypto from 'crypto'
|
||||||
|
import logger from '../apis/core/picgo/logger'
|
||||||
|
|
||||||
class ClipboardWatcher extends EventEmitter {
|
class ClipboardWatcher extends EventEmitter {
|
||||||
lastImage: Electron.NativeImage | null
|
|
||||||
timer: NodeJS.Timeout | null
|
timer: NodeJS.Timeout | null
|
||||||
|
lastImageHash: string | null
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
super()
|
super()
|
||||||
this.lastImage = null
|
this.lastImageHash = null
|
||||||
this.timer = null
|
this.timer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
startListening (watchDelay = 500) {
|
startListening (watchDelay = 500) {
|
||||||
this.stopListening()
|
this.stopListening(false)
|
||||||
|
|
||||||
const image = clipboard.readImage()
|
|
||||||
if (!image.isEmpty()) {
|
|
||||||
const dataUrl = image.toDataURL()
|
|
||||||
this.lastImage = nativeImage.createFromDataURL(dataUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.timer = setInterval(() => {
|
this.timer = setInterval(() => {
|
||||||
const image = clipboard.readImage()
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataUrl = image.toDataURL()
|
this.lastImageHash = currentImageHash
|
||||||
const currentImage = nativeImage.createFromDataURL(dataUrl)
|
this.emit('change')
|
||||||
|
|
||||||
if (this.lastImage) {
|
|
||||||
const lastDataUrl = this.lastImage.toDataURL()
|
|
||||||
if (lastDataUrl === dataUrl) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.lastImage = currentImage
|
|
||||||
this.emit('change', currentImage)
|
|
||||||
}, watchDelay)
|
}, watchDelay)
|
||||||
|
logger.info('Start to watch clipboard')
|
||||||
}
|
}
|
||||||
|
|
||||||
stopListening () {
|
stopListening (isLog = true) {
|
||||||
if (this.timer) {
|
if (this.timer) {
|
||||||
clearInterval(this.timer)
|
clearInterval(this.timer)
|
||||||
this.timer = null
|
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