Skip to content

Commit

Permalink
Merge pull request #1 from mattmassicotte/main
Browse files Browse the repository at this point in the history
Adopt DispatchQueue API
  • Loading branch information
Frizlab authored Jul 14, 2023
2 parents e0c59a2 + 6f23118 commit bf33986
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// swift-tools-version:5.1
// swift-tools-version:5.5
import PackageDescription


let package = Package(
name: "FSEventsWrapper",
platforms: [.macOS(.v10_13)],
products: [
.library(name: "FSEventsWrapper", targets: ["FSEventsWrapper"]),
],
Expand Down
6 changes: 3 additions & 3 deletions Sources/FSEventsWrapper/FSEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import Foundation



public enum FSEvent {
public enum FSEvent : Sendable {

public enum MustScanSubDirsReason {
public enum MustScanSubDirsReason : Sendable {

case userDropped
case kernelDropped
Expand All @@ -22,7 +22,7 @@ public enum FSEvent {

}

public enum ItemType {
public enum ItemType : Sendable {

case file
case dir
Expand Down
28 changes: 14 additions & 14 deletions Sources/FSEventsWrapper/FSEventStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import Foundation

public class FSEventStream {

public let callback: (FSEventStream, FSEvent) -> Void
public typealias Callback = @Sendable (FSEventStream, FSEvent) -> Void

public let callback: Callback

internal let eventStream: FSEventStreamRef
internal let eventStreamFlags: FSEventStreamCreateFlags

public let runLoop: CFRunLoop
public let runLoopMode: RunLoop.Mode
public let queue: DispatchQueue

private var isScheduled = false
private(set) var isStarted = false

public convenience init?(
path: String,
since startId: FSEventStreamEventId? = nil, updateInterval: CFTimeInterval = 0, fsEventStreamFlags flags: FSEventStreamCreateFlags = FSEventStreamCreateFlags(kFSEventStreamCreateFlagNone),
runLoop rl: RunLoop = .current, runLoopMode rlm: RunLoop.Mode = .default,
callback: @escaping (FSEventStream, FSEvent) -> Void
queue: DispatchQueue = .global(),
callback: @escaping Callback
) {
self.init(paths: [path], since: startId, updateInterval: updateInterval, fsEventStreamFlags: flags, runLoop: rl, runLoopMode: rlm, callback: callback)
self.init(paths: [path], since: startId, updateInterval: updateInterval, fsEventStreamFlags: flags, queue: queue, callback: callback)
}

/**
Expand All @@ -54,7 +54,7 @@ public class FSEventStream {
- parameter fsEventStreamFlags:
The flags to use to create the `FSEvent` stream.

**Note**: The `...UseCFTypes` flag will always be added to the flags used to create the stream.
- Note: The `...UseCFTypes` flag will always be added to the flags used to create the stream.

- parameter callbackHandler: Your handler object.

Expand All @@ -69,17 +69,16 @@ public class FSEventStream {
paths: [String],
since startId: FSEventStreamEventId? = nil, updateInterval: CFTimeInterval = 0,
fsEventStreamFlags: FSEventStreamCreateFlags = FSEventStreamCreateFlags(kFSEventStreamCreateFlagNone),
runLoop: RunLoop = .current, runLoopMode: RunLoop.Mode = .default,
callback: @escaping (FSEventStream, FSEvent) -> Void
queue: DispatchQueue = .global(),
callback: @escaping Callback
) {
let cfpaths: CFArray = paths as CFArray
let actualStartId = startId ?? FSEventStreamEventId(kFSEventStreamEventIdSinceNow)
let actualFlags = FSEventStreamCreateFlags(kFSEventStreamCreateFlagUseCFTypes | Int(fsEventStreamFlags))

self.callback = callback

self.runLoopMode = runLoopMode
self.runLoop = runLoop.getCFRunLoop()
self.queue = queue

let objcWrapper = FSEventStreamObjCWrapper()
var context = FSEventStreamContext(
Expand All @@ -102,20 +101,20 @@ public class FSEventStream {
self.eventStream = s
self.eventStreamFlags = actualFlags

FSEventStreamSetDispatchQueue(s, queue)

objcWrapper.swiftStream = self
}

deinit {
stopWatching()
FSEventStreamUnscheduleFromRunLoop(eventStream, runLoop, runLoopMode as CFString)
FSEventStreamInvalidate(eventStream)
FSEventStreamRelease(eventStream) /* I thought the release would be automatic, it seems it is not. */
}

public func startWatching() {
if isStarted {return}

if !isScheduled {FSEventStreamScheduleWithRunLoop(eventStream, runLoop, runLoopMode as CFString); isScheduled = true}
FSEventStreamStart(eventStream)
isStarted = true
}
Expand Down Expand Up @@ -258,4 +257,5 @@ private func eventStreamCallback(
}
}
}

}
2 changes: 1 addition & 1 deletion Tests/FSEventsWrapperTests/FSEventsWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FSEventsWrapperTests: XCTestCase {
}

func testBasicMonitoring() {
let handler = { (stream: FSEventStream, event: FSEvent) in
let handler: FSEventStream.Callback = { (stream: FSEventStream, event: FSEvent) in
NSLog("%@", String(describing: event))
stream.stopWatching()
}
Expand Down

0 comments on commit bf33986

Please sign in to comment.