Giter Site home page Giter Site logo

typicalpixel / file_system Goto Github PK

View Code? Open in Web Editor NEW

This project forked from falood/file_system

0.0 2.0 0.0 9.75 MB

Filesystem monitor for elixir

License: Do What The F*ck You Want To Public License

C 31.25% C# 19.45% Makefile 0.61% Elixir 48.69%

file_system's Introduction

FileSystem

A file change watcher wrapper based on fs

System Support

  • Mac fsevent
  • Linux and FreeBSD inotify
  • Windows inotify-win

NOTE: On Linux and FreeBSD you need to install inotify-tools.

Usage

Put file_system in the deps and application part of your mix.exs

defmodule Excellent.Mixfile do
  use Mix.Project

  def project do
  ...
  end

  defp deps do
    [
      { :file_system, "~> 0.1", only: :test },
    ]
  end
  ...
end

Subscription API

You can spawn a worker and subscribe to events from it:

{:ok, pid} = FileSystem.start_link(dirs: ["/path/to/some/files"])
FileSystem.subscribe(pid)

or

{:ok, pid} = FileSystem.start_link(dirs: ["/path/to/some/files"], name: :my_monitor_name)
FileSystem.subscribe(:my_monitor_name)

The pid you subscribed from will now receive messages like

{:file_event, worker_pid, {file_path, events}}

and

{:file_event, worker_pid, :stop}

Example with GenServer

defmodule Watcher do
  use GenServer

  def start_link(args) do
    GenServer.start_link(__MODULE__, args)
  end

  def init(args) do
    {:ok, watcher_pid} = FileSystem.start_link(args)
    FileSystem.subscribe(watcher_pid)
    {:ok, %{watcher_pid: watcher_pid}}
  end

  def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid}=state) do
    # YOUR OWN LOGIC FOR PATH AND EVENTS
    {:noreply, state}
  end

  def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid}=state) do
    # YOUR OWN LOGIC WHEN MONITOR STOP
    {:noreply, state}
  end
end

Tweaking behaviour via extra arguments

For each platform, you can pass extra arguments to the underlying listener process.

Each backend support different extra arguments, check backend module documentation for more information.

Here is an example to get instant notifications on file changes for Mac OS X:

FileSystem.start_link(dirs: ["/path/to/some/files"], latency: 0, watch_root: true)

file_system's People

Contributors

aclemmensen avatar cursorzz avatar falood avatar josevalim avatar mveytsman avatar pguillory avatar pmartinezalvarez avatar thbar avatar ybycode 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.