From bb991d0c862fb8c07205d346a36a2f3c0a8a5d95 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 18 Feb 2023 22:33:46 +0800 Subject: [PATCH] SessionCtl // Temporarily remember the recent 20 clients' Bundle IDs. --- Source/Modules/SessionCtl_Core.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Source/Modules/SessionCtl_Core.swift b/Source/Modules/SessionCtl_Core.swift index 4ba3dbd9..0b7023ad 100644 --- a/Source/Modules/SessionCtl_Core.swift +++ b/Source/Modules/SessionCtl_Core.swift @@ -45,7 +45,23 @@ public class SessionCtl: IMKInputController { /// 用以存儲客體的 bundleIdentifier。 /// 由於每次動態獲取都會耗時,所以這裡直接靜態記載之。 - public var clientBundleIdentifier: String = "" + public var clientBundleIdentifier: String = "" { + willSet { + if newValue.isEmpty { return } + Self.recentClientBundleIdentifiers[newValue] = Int(Date().timeIntervalSince1970) + } + } + + /// 用以記錄最近存取過的十個客體(亂序),相關內容會在客體管理器當中用得到。 + public static var recentClientBundleIdentifiers = [String: Int]() { + didSet { + if recentClientBundleIdentifiers.count < 20 { return } + if recentClientBundleIdentifiers.isEmpty { return } + let x = recentClientBundleIdentifiers.sorted(by: { $0.value < $1.value }).first?.key + guard let x = x else { return } + recentClientBundleIdentifiers[x] = nil + } + } // MARK: -