Giter Club home page Giter Club logo

observer's Introduction

Observer

Go event emitter and listener with builtin file watcher package.

Go Report Card Build Status GoDoc License

Code snippets

// Create a new observer
o := observer.Observer{}
o.Open()

// Register a listener method.
o.AddListener(func(e interface{}) {
  log.Printf("Received: %v.\n", e)
})

// Emit an event.
o.Emit("Hello")

// Watch and emit events on file modifications.
o.Watch([]string{"*.html", "css/*.scss"})

Description

This observer implements event emitter and listener pattern in go, the observer register a list of listener functions and implement an event emitter, once an event is emitted, all listener functions will be called.

This observer also abstracts watching for file changes, users can register a list for files to watch, once a file is watched, events will be emitted automatically on each file modification. Common use cases are watching for changing in config files, and wating for code changes.

This observer is using golang channels for emiting events and fsnotify for watching file changes.

Examples:

Develop

$ go get -u github.com/golang/dep/cmd/dep
$ make vendor
$ make
$ ./observe

Install

go get -u github.com/yaacov/observer

Install using go get will install the package and the CLI tool observer

CLI

observer is a cli tool for watching files and executing shell commands on file modification, it is used to call an action on file change, examples of use can be restart and app when config file changes, recompile code when code updates and send image to server when image change.

inotify-tools, are a set of linux tools that monitor files for changes, they may be better choice for file monitoring on more complex cases.

Get help:

observer -h

Call a server api when config file chage:

observer -r "curl -X POST http://127.0.0.1:8000/api/v1/-/restart" -w "/root/.aws/config"

API

See examples for usage examples.

Method Description
Open() Open the observer channels
Close() Close the observer channels
AddListener(callback Listener) Add a listener function to run on event
Emit(event interface{}) Emit event
Watch(files []string) Watch for file changes, and emit a file change events
SetBufferDuration(d time.Duration) Set the event buffer damping duration
Type Description
WatchEvent struct{ Name string, Op uint32 } Event type emitted by file watcher
Listener func(interface{}) Function type for listeners
Observer struct{ Verbose bool } The observer object

Watching files for modifications

Watching files can be done using exact file name, or shell pattern matching.

Watching for exact file names:

Watch([]string{"./aws/config", "./aws/credentials"})

Watching files using shell pattern matching:

Watch([]string{"./kube/*.yml"})

Note:

We can not expand tilde to home directory, ~/.config will not work as expected. If needed users can use golang's os/user/ package.

Examples

Emit string events

Example of event listener and emitter.

emit.go

// Open an observer and start running
o := observer.Observer{}
o.Open()
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  log.Printf("Received: %s.\n", e.(string))
})

// This event will be picked by the listener
o.Emit("Hello")

Watch files, emit file change events

Example of file watching and listener.

file-watch.go

// Open an observer and start watching for files by file name
o := observer.Observer{}
o.Watch([]string{"../LICENSE"})
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  watchEvent := e.(observer.WatchEvent)
  log.Printf("File modified: %s.\n", e.Name)
})

file-watch-pattern.go

// Open an observer and start watching for file matching shell pattern
o := observer.Observer{}
o.Watch([]string{"*.html", "css/*.scss"})
defer o.Close()

// Add a listener that logs events
o.AddListener(func(e interface{}) {
  log.Printf("File modified: %v.\n", e)
})

Group events by time, event groups will be sent once as an array of events.

emit-buffered.go

o.Open()
o.SetBufferDuration(1 * time.Second)

o.AddListener(func(e interface{}) {
	output = e.([]interface{}) // => ["done", "done", "done", "done"]
})

// All this events will be grouped together, and sent only once to the listener.
o.Emit("done")
o.Emit("done")
o.Emit("done")
o.Emit("done")

observer's People

Contributors

yaacov 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

observer's Issues

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.