Skip to content

Commit

Permalink
Reduce unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong committed Dec 11, 2024
1 parent 1666887 commit 6c0c7a8
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions WhatCopiedMac/Sources/PasteObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ final class PasteObserver {
return
}

DispatchQueue.main.async {
let newToken = NSPasteboard.changeToken
guard self.changeToken != newToken else {
return
}

self.changeToken = newToken
NotificationCenter.default.post(name: .pasteboardChanged, object: nil)
}
DispatchQueue.main.async(execute: checkChangeToken)
}

self.timer = timer
self.checkChangeToken()
RunLoop.current.add(timer, forMode: .common)
NotificationCenter.default.post(name: .pasteboardChanged, object: nil)
}

func stopObserving() {
Expand All @@ -53,4 +45,14 @@ final class PasteObserver {
private init() {
// no-op
}

private func checkChangeToken() {
let newToken = NSPasteboard.changeToken
guard changeToken != newToken else {
return
}

changeToken = newToken
NotificationCenter.default.post(name: .pasteboardChanged, object: nil)
}
}

0 comments on commit 6c0c7a8

Please sign in to comment.