forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPCService.swift
More file actions
47 lines (42 loc) · 1.35 KB
/
XPCService.swift
File metadata and controls
47 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
import os.log
import XPCShared
var asyncService: AsyncXPCService?
var shared = XPCService()
public func getService() throws -> AsyncXPCService {
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
struct RunningInPreview: Error {}
throw RunningInPreview()
}
if shared.isInvalidated {
shared = XPCService()
asyncService = nil
}
if let asyncService { return asyncService }
let service = AsyncXPCService(connection: shared.connection)
asyncService = service
return service
}
class XPCService {
var isInvalidated = false
lazy var connection: NSXPCConnection = {
let connection = NSXPCConnection(
machServiceName: Bundle(for: XPCService.self)
.object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String + ".XPCService"
)
connection.remoteObjectInterface =
NSXPCInterface(with: XPCServiceProtocol.self)
connection.invalidationHandler = { [weak self] in
os_log(.info, "XPCService Invalidated")
self?.isInvalidated = true
}
connection.interruptionHandler = { [weak self] in
os_log(.info, "XPCService interrupted")
}
connection.resume()
return connection
}()
deinit {
connection.invalidate()
}
}