Giter Site home page Giter Site logo

chikador's Introduction

chikador

because of chismis, i became a chikador. Chikador is a simple overlay, or abstraction, over fsnotify that brings a simpler way of using fsnotify, which by itself is already a cool tool that does many things very easily.

Demo

package main

import (
	"fmt"
	"github.com/ShindouMihou/chikador/chikador"
	"log"
)

func main() {
	chismis, err := chikador.Watch(".tests/", chikador.WithDedupe)
	if err != nil {
		log.Fatalln(err)
	}
	defer chismis.Close()
	chismis.Listen(func(msg *chikador.Message) {
		fmt.Println("received ", msg.Event, " from ", msg.Filename)
	})
	<-make(chan struct{})
}

installation

go get github.com/ShindouMihou/chikador

file watcher

for an examplified-view of the different methods, please view our examples:

  • async: asynchronously send events to a callback
  • polling: synchronously poll events from the queue

as explained from the above, we have two methods of polling events (async and polling). internally, async is simply polling but ran in another goroutine, but to demonstrate how we can create the two different methods:

async

asynchronous is the easiest way, you can simply use the chismis.Listen method to create an async watcher.

func main() {
    chismis, err := chikador.Watch(".tests/", chikador.WithDedupe)
    if err != nil {
        log.Fatalln(err)
    }
    defer chismis.Close()
	chismis.Listen(func(msg *chikador.Message) {
        fmt.Println("received ", msg.Event, " from ", msg.Filename)
    })
    <-make(chan struct{})
}

polling

when you want to handle your way of asynchronous yourself, or simply want to use the for-loop interface yourself, then you can use the polling method by creating a Chismis instance using chikador.Watch method. unlike the async method, we have the Poll method which returns a *Message that we can use to poll:

func main() {
    chismis, err := chikador.Watch(".tests/", chikador.WithDedupe)
    if err != nil {
        log.Fatalln(err)
    }
    defer chismis.Close()
    for {
        if chismis.IsClosed() {
            break
        }
        message := chismis.Poll()
        if message == nil {
            continue
        }
        fmt.Println("received ", message.Event, " from ", message.Filename)
    }
}

dedupe?

As stated in fsnotify , the operating system may duplicate events, and in one of their examples, they've included a deduplicator which we've brought down to chikador that can be enabled by adding the chikador.WithDedupe option, as seen, in the examples, such as in:

recursive?

fsnotify will listen to the directory's files, but it cannot listen onto the subdirectories, which is why we include a little utility to have chikador scan through all subdirectories and their corresponding subdirectories to listen to changes on them as well. You can enable this behavior by adding the chikador.Recursive option, see example in:

chikador's People

Contributors

shindoumihou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.