Skip to content

Commit

Permalink
use FSEventStreamSetDispatchQueue instead of deprecated FSEventStream…
Browse files Browse the repository at this point in the history
…ScheduleWithRunLoop
  • Loading branch information
emanuel-skrenkovic committed May 4, 2024
1 parent ce9aad4 commit e67c25c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
3 changes: 1 addition & 2 deletions fsevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func DeviceForPath(path string) (int32, error) {
// ...
type EventStream struct {
stream fsEventStreamRef
rlref cfRunLoopRef
hasFinalizer bool
registryID uintptr
uuid string
Expand Down Expand Up @@ -164,7 +163,7 @@ func (es *EventStream) Flush(sync bool) {
// Stop stops listening to the event stream.
func (es *EventStream) Stop() {
if es.stream != nil {
stop(es.stream, es.rlref)
stop(es.stream)
es.stream = nil
}

Expand Down
39 changes: 12 additions & 27 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@ func copyCFString(cfs C.CFStringRef) C.CFStringRef {
return C.CFStringCreateCopy(C.kCFAllocatorDefault, cfs)
}

// cfRunLoopRef wraps C.CFRunLoopRef
type cfRunLoopRef C.CFRunLoopRef

// EventIDForDeviceBeforeTime returns an event ID before a given time.
func EventIDForDeviceBeforeTime(dev int32, before time.Time) uint64 {
tm := C.CFAbsoluteTime(before.Unix())
Expand Down Expand Up @@ -429,26 +426,16 @@ func (es *EventStream) start(paths []string, callbackInfo uintptr) error {

es.stream = setupStream(paths, es.Flags, callbackInfo, since, es.Latency, es.Device)

started := make(chan error)

go func() {
runtime.LockOSThread()
es.rlref = cfRunLoopRef(C.CFRunLoopGetCurrent())
C.CFRetain(C.CFTypeRef(es.rlref))
C.FSEventStreamScheduleWithRunLoop(es.stream, C.CFRunLoopRef(es.rlref), C.kCFRunLoopDefaultMode)
if C.FSEventStreamStart(es.stream) == 0 {
// cleanup stream and runloop
C.FSEventStreamInvalidate(es.stream)
C.FSEventStreamRelease(es.stream)
C.CFRelease(C.CFTypeRef(es.rlref))
es.stream = nil
started <- fmt.Errorf("failed to start eventstream")
close(started)
return
}
close(started)
C.CFRunLoopRun()
}()
q := C.dispatch_queue_create(nil, nil)
C.FSEventStreamSetDispatchQueue(es.stream, q)

if C.FSEventStreamStart(es.stream) == 0 {
// cleanup stream
C.FSEventStreamInvalidate(es.stream)
C.FSEventStreamRelease(es.stream)
es.stream = nil
return fmt.Errorf("failed to start eventstream")
}

if !es.hasFinalizer {
// TODO: There is no guarantee this run before program exit
Expand All @@ -457,7 +444,7 @@ func (es *EventStream) start(paths []string, callbackInfo uintptr) error {
es.hasFinalizer = true
}

return <-started
return nil
}

func finalizer(es *EventStream) {
Expand All @@ -476,10 +463,8 @@ func flush(stream fsEventStreamRef, sync bool) {
}

// stop requests fsevents stops streaming events
func stop(stream fsEventStreamRef, rlref cfRunLoopRef) {
func stop(stream fsEventStreamRef) {
C.FSEventStreamStop(stream)
C.FSEventStreamInvalidate(stream)
C.FSEventStreamRelease(stream)
C.CFRunLoopStop(C.CFRunLoopRef(rlref))
C.CFRelease(C.CFTypeRef(rlref))
}

0 comments on commit e67c25c

Please sign in to comment.