Code Monkey home page Code Monkey logo

filewatcher's Introduction

πŸ‘‹ About

πŸ“ Recent articles:

🌱 Open-source:

Package Description Tests CodeBeat Stars
DarkMode DarkMode made simple Tests codebeat badge platform
Spatial Layout manager Tests codebeat badge platform
FileWatcher Observe file changes Tests codebeat badge platform
HybridCam Photo and Video camera .github/workflows/swift.yml codebeat badge platform
TestRunner Run UI-tests in sequence Tests codebeat badge platform
ParallelLoop Parallel + functional operations in swift Tests codebeat badge platform
FileStreamer Continuous data to disk Tests codebeat badge platform
FlowLayout Bi-directional layout framework for iOS Tests codebeat badge platform
Popover-label Popover label for iOS Swift codebeat badge platform

πŸ“Š GitHub Stats:


πŸ“š Stackoverflow stats:

πŸ† Awards:

FlowLayout - A bi-directional layout framework for iOS | Product Hunt

filewatcher's People

Contributors

alioguzhan avatar eonist avatar mohakapt avatar paperlib avatar uksusoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

filewatcher's Issues

SPM Release for 0.2.2 not tagged

Modifications seem to have been made to support SPM but the latest release (0.2.2) is not tagged as such to properly pull via Xcode. A warning is displayed in getting the latest tagged release (0.2.1) in Xcode 11.4 as it doesn't support SPM.

image

image

Callback is getting triggered twice in SwiftUI project

Hi, I'm using this library in a brand new vanilla SwiftUI project and anytime I make a change to a file, the callback is getting triggered twice:

import FileWatcher
class ContentScreenViewModel: ObservableObject {
    let filewatcher = FileWatcher(["/Users/aryamansharda/Documents/test.md"])

    init() {
        filewatcher.start() // start monitoring
        filewatcher.callback = { event in
            print("Something happened here: " + event.path)
        }
    }
}

struct ContentView: View {
    @StateObject var viewModel = ContentScreenViewModel()

    var body: some View {
        VStack {
            Text("Hello, world!")
        }
        .padding()
    }
}

This is the entirety of the program's code and any time I make a change to test.md, I see this in the Debugger:

Something happened here: /Users/aryamansharda/Documents/test.md
Something happened here: /Users/aryamansharda/Documents/test.md

When I use event.description in the print statement, it's showing that the file was renamed even though only its contents were changed:

The file /Users/aryamansharda/Documents/test.md was renamed
The file /Users/aryamansharda/Documents/test.md was created

For other files, both lines mention renamed:

The file /Users/aryamansharda/Documents/calc/.git/HEAD was renamed
The file /Users/aryamansharda/Documents/calc/.git/HEAD was renamed

Is this expected?

FSEventStreamSetDispatchQueue was deprecated

Hello,
when targeting macOS 13, compiler shows warning "FSEventStreamSetDispatchQueue was deprecated" in FileWatcher.swift line 43.

Thanks and with best regards
Petr Pavlicek

Place sources into 'Sources' subfolder to avoid Xcode warnings

Xcode has warnings when add FileWatcher with SPM.

Consider to place all sources into 'Sources' subfolder to silence them.

found 3 file(s) which are unhandled; explicitly declare them as resources or exclude from the target

/Users/.../checkouts/FileWatcher/FileWatcher_macOS/Info.plist
/Users/.../checkouts/FileWatcher/README.md
/Users/.../checkouts/FileWatcher/LICENSE

FileWatcherEvent properties should be public?

If we need check that file created:

import FileWatcher_macOS
let filewatcher = FileWatcher([
  NSString(string: "~/Desktop").expandingTildeInPath
])
        
filewatcher.callback = { event in
  if (event.fileCreated) {
    print("File created here: " + event.path)
  }
}

filewatcher.start()

We got:
'fileCreated' is inaccessible due to 'internal' protection level

Simplify library with DispatchSemaphore

Observing changes in a file with DispatchSource
The final β€œlesser known” feature of GCD that I want to bring up is how it provides a way to observe changes in a file on the file system. Like DispatchSemaphore, this is something which can be super useful in a script or command line tool, if we want to automatically react to a file being edited by the user. This enables us to easily build developer tools that have β€œlive editing” features.

Dispatch sources come in a few different variants, depending on what we want to observe. In this case we’ll use DispatchSourceFileSystemObject, which lets us observe events from the file system.

Let's take a look at an example implementation of a simple FileObserver, that lets us attach a closure to be run every time a given file is changed. It works by creating a dispatch source using a fileDescriptor and a DispatchQueue to perform the observation on, and uses Files to refer to the file to observe:

class FileObserver {
    private let file: File
    private let queue: DispatchQueue
    private var source: DispatchSourceFileSystemObject?

    init(file: File) {
        self.file = file
        self.queue = DispatchQueue(label: "com.myapp.fileObserving")
    }

    func start(closure: @escaping () -> Void) {
        // We can only convert an NSString into a file system representation
        let path = (file.path as NSString)
        let fileSystemRepresentation = path.fileSystemRepresentation

        // Obtain a descriptor from the file system
        let fileDescriptor = open(fileSystemRepresentation, O_EVTONLY)

        // Create our dispatch source
        let source = DispatchSource.makeFileSystemObjectSource(
            fileDescriptor: fileDescriptor,
            eventMask: .write,
            queue: queue
        )

        // Assign the closure to it, and resume it to start observing
        source.setEventHandler(handler: closure)
        source.resume()
        self.source = source
    }
}
We can now use FileObserver like this:

let observer = try FileObserver(file: file)

observer.start {
    print("File was changed")
}

ref: https://www.swiftbysundell.com/articles/a-deep-dive-into-grand-central-dispatch-in-swift/

Optional variables as lazy?

Could we make the instance variable non-optional? Example:

lazy var someSize:CGSize = {
   let x:CGFloat = 0
   let y:CGFloat = 0
   let size = CGSize(x,y,0,0)
   return size
}()

//Now we can use someSize with out force unwrap πŸ‘Œ
//What happens is that the var isn't created until something asks for it.
print(someSize.x)

Deleting files show as "renamed" instead of "removed"

After importing the project and getting it setup, I tested deleting files from a directory. The notifications fire properly but they state that the file was renamed instead of removed. Additionally, adding a file will say that it was modified instead of created.

I tried this on an iCloud and a non-iCloud Drive folder.

This is the output of printing from extension FileWatcherEvent

Swift.print(self.removed)
Swift.print(self.renamed)
Swift.print(result.description)

false
true
The file /Users/nick/Downloads/IMG_1739.jpeg was renamed

Any ideas? Love the library btw, thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.