Code Monkey home page Code Monkey logo

dekit's Introduction

DEKit

License Swift Version

Library to watch changes in directory

do {
    // you can ignore value and receive notifications, but DirectoryEvents object will be destroyed only if directory deleted
    try DirectoryEvents(watch: "Downloads/DEKitTest")

    // you can pass a custom clousure to process received events as you wish (by default prints all info)
    try DirectoryEvents(watch: "Downloads/DEKitTest") { event in
        print(event.when)
    }

    let de = try DirectoryEvents(watch: "Downloads/DEKitTest")
    de.stopReceiving() // call this method to explicitly finish watching directory and destroy DirectoryEvents object
} catch {
    print(error.localizedDescription)
}

With this code

do {
    try DirectoryEvents(watch: "Downloads/DEKitTest")
} catch {
    print(error.localizedDescription)
}

you will receive this output

ouput

Installation

macOS app

In Xcode go to File -> Swift Packages -> Add Package Dependency and paste in the repo's url: https://github.com/japanese-goblinn/DEKit.git

Requirments

Please add read permissions for folders that you want to watch in .entitlements file

<key>com.apple.security.app-sandbox</key>
<true/>
<!--This permission is needed to ask for read only once-->
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<!--All permissions below is for specific directories from which you want ot read. Add more premissions here if you want to read not only from Downloads folder-->
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.files.downloads.read-only</key>
<true/>

Swift Package

let package = Package(
    name: "Package",
    dependencies: [
        .package(url: "https://github.com/japanese-goblinn/DEKit.git", from: "1.2.0")
    ],
    targets: [
        .target(
            name: "Package",
            dependencies: ["DEKit"])
    ]
)

dekit's People

Stargazers

 avatar

Watchers

 avatar  avatar

dekit's Issues

Memory leak with DirectoryEvents class

Description

I want a clear API for my class so I've marked initializer with @discardableResult if I only need to watch one directory for events. Only on the next morning (Yes, I've coded this at night ๐Ÿ˜…) I've realized that this code can only work if there a retain cycle. So I've try to dig into memory graph, but I don't find any retain cycles, only kevents pointing to my class.

Screen Shot 2020-05-13 at 01 32 14

Screen Shot 2020-05-13 at 01 32 28

It's strange because they should disappear as soon as all directory files is no longer watched - but they don't. Main problem that I see here is that all watched files possibly will never be closed (because classes deinit is never called) and it's definitely not good for memory usage.

deinit {
    close(kernelQueue)
    closeWatched()
}

private func closeWatched() {
    // keys here is files descriptors 
    watchedFiles.keys.forEach { closeFile($0) }
    close(watchedDirectory)    
}

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.