gobounce

package module
v0.0.0-...-5aa5236 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 10 Imported by: 0

README

Build Status Coverage Status

gobounce

A cross-platform Go file watcher and event debouncer utilizing github.com/radovskyb/watcher

Example Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/robarchibald/gobounce"
)

func main() {
	// Create a debounced file watcher that will:
	// 1. Poll for updates every 100 milliseconds
	// 2. Debounce updates to only notify after no changes have occurred for 200 milliseconds
	// 3. Notify on FileChanged and FolderChanged channels when files and folders are ready for use
	options := gobounce.Options{RootFolders: []string{"folderToWatch"}}
	w, err := gobounce.New(options, 100*time.Millisecond)
	if err != nil {
		log.Fatal(err)
	}

	go handleChanges(w)
	w.Start()
}

func handleChanges(w *gobounce.Filewatcher) {
	for {
		select {
		case filename := <-w.FileChanged:
			fmt.Println("file changed", filename)
		case folder := <-w.FolderChanged:
			fmt.Println("folder changed", folder)
		case <-w.Closed:
			return
		}
	}
}

Documentation

Overview

This is a generalized filesystem watcher with ideas taken from the https://github.com/6degreeshealth/autotest package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filewatcher

type Filewatcher struct {
	FileChanged   chan string
	FolderChanged chan string
	Error         chan error
	Closed        chan struct{}
	// contains filtered or unexported fields
}

func New

func New(options Options, pollDuration time.Duration) (*Filewatcher, error)

New creates a debounced file watcher. It will watch for changes to the filesystem every `pollDuration` duration and notify of changes after no change has been seen in that file or folder in 2x the `pollDuration`. For example, if the pollDuration is set to 1 second, the debounceDuration will automatically be set to 2 seconds. This would be the timeline then for an example file: (0 seconds) poll for changes: none found (0.3 seconds) folder1/file1 updated (1 second) poll for changes: 1 folder1/file1 and 1 folder1 change found

debounce timer for folder1 created for 2 seconds due to change
debounce timer for folder1/file1 created for 2 seconds due to change

(1.1 second) folder1/file2 updated (2 seconds) poll for changes - 1 folder1/file2 and 1 folder1 change found

	               debounce timer for folder1 reset to 2 seconds due to new change to folder1
                debounce timer for folder1/file2 created for 2 seconds due to change

(3 seconds) poll for changes - no new changes found

debounce timer finishes for folder1/file1. FileChanged channel publishes the filename

(4 seconds) poll for changes - no new changes found

debounce timer finishes for folder1/file2. FileChanged channel publishes the filename
debounce timer finishes for folder1. FileChanged channel publishes the folder name

func (*Filewatcher) Close

func (w *Filewatcher) Close()

func (*Filewatcher) Start

func (w *Filewatcher) Start()

func (*Filewatcher) WatchFolders

func (w *Filewatcher) WatchFolders() []string

WatchFolders returns the current list of folders being watched by gobounce

type Options

type Options struct {
	RootFolders      []string
	FolderExclusions []string
	IncludeHidden    bool
	ExcludeSubdirs   bool
	FollowNewFolders bool
	MaxConcurrency   int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL