Skip to content

Commit d361fef

Browse files
committed
✨ Add queue
1 parent cd6c87c commit d361fef

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

Sources/Cli/Watch.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ struct Watch: ParsableCommand {
1010
)
1111

1212
mutating func run() {
13-
StdOut.print("111")
14-
print("111a")
15-
let repo = EventRepo()
16-
StdOut.print("222")
17-
print("bbb")
1813
let config = Loader.readConfig()
19-
StdOut.print("\(String(describing: config))")
20-
let hooks = config?.hooks ?? []
21-
repo.registerForEventStoreChanges(hooks: hooks)
14+
EventRepo().registerForEventStoreChanges(hooks: config?.hooks ?? [])
2215
dispatchMain()
2316
}
2417
}

Sources/IO/EventRepo.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ final class EventRepo {
44
private let eventStore = EKEventStore()
55
private var hooks: [Hook] = []
66

7+
let eventQueue = DispatchQueue(label: "com.oschrenk.plan")
8+
79
private func grantAccess() -> EKEventStore {
810
if #available(macOS 14, *) {
911
self.eventStore.requestFullAccessToEvents { granted, maybeError in
@@ -95,26 +97,25 @@ final class EventRepo {
9597
}
9698

9799
func registerForEventStoreChanges(hooks: [Hook]) {
98-
self.hooks = hooks
99100
NotificationCenter.default.addObserver(
100-
self,
101-
selector: #selector(eventStoreChanged(_:)),
102-
name: .EKEventStoreChanged,
103-
object: nil // Listen to all EKEventStore changes
104-
)
105-
}
106-
107-
@objc private func eventStoreChanged(_: Notification) {
108-
for hook in hooks {
109-
hook.trigger()
101+
forName: .EKEventStoreChanged,
102+
object: nil,
103+
queue: nil
104+
) { _ in
105+
print("d")
106+
self.eventQueue.async {
107+
for hook in hooks {
108+
hook.trigger()
109+
}
110+
}
110111
}
111112
}
112113

113114
deinit {
114115
NotificationCenter.default.removeObserver(
115116
self,
116117
name: .EKEventStoreChanged,
117-
object: eventStore
118+
object: nil
118119
)
119120
}
120121
}

0 commit comments

Comments
 (0)