diff --git a/README.md b/README.md index d416ed0d517483a8a4bf5f80d1dba5365f3201c5..62599f03091862d46efbb1f7b3e75f977561c40a 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,18 @@ Execute in iex iex > Monitor.start ``` +## Tweaking behaviour via listener extra arguments + +For each platform, you can pass extra arguments to the underlying listener process via the `listener_extra_args` option. + +Here is an example to get instant notifications on file changes for Mac OS X: + +```elixir +use ExFSWatch, dirs: ["/tmp/fswatch"], listener_extra_args: "--latency=0.0" +``` + +See the [fs source](https://github.com/synrc/fs/tree/master/c_src) for more details. + ## List Events from Backend ```shell diff --git a/lib/exfswatch.ex b/lib/exfswatch.ex index d06cbce4a751b3feb3d4f228d7c666bdfea81540..dd097d7b6a04888dbfbaa77c7ad414d17bb9da4a 100644 --- a/lib/exfswatch.ex +++ b/lib/exfswatch.ex @@ -1,9 +1,10 @@ require Logger defmodule ExFSWatch do - defmacro __using__([dirs: dirs]) do + defmacro __using__(options) do quote do - def __dirs__, do: unquote(dirs) + def __dirs__, do: unquote(Keyword.fetch!(options, :dirs)) + def __listener_extra_args__, do: unquote(Keyword.get(options, :listener_extra_args, '')) def start, do: ExFSWatch.Supervisor.start_child __MODULE__ end end diff --git a/lib/exfswatch/worker.ex b/lib/exfswatch/worker.ex index c8ff4bb7fa6706ba600b977669ec80754c4e0b75..fd0b963a6b6afebb3974984532374ac6eb7439f4 100644 --- a/lib/exfswatch/worker.ex +++ b/lib/exfswatch/worker.ex @@ -9,7 +9,7 @@ defmodule ExFSWatch.Worker do def init(module) do backend = ExFSWatch.backend - port = start_port(backend, module.__dirs__) + port = start_port(backend, module.__dirs__, module.__listener_extra_args__) {:ok, %__MODULE__{port: port, backend: backend, module: module}} end @@ -29,15 +29,16 @@ defmodule ExFSWatch.Worker do end - defp start_port(:fsevents, path) do + defp start_port(:fsevents, path, listener_extra_args) do path = path |> format_path + args = [ listener_extra_args, '-F' | path] Port.open({:spawn_executable, :fsevents.find_executable()}, - [:stream, :exit_status, {:line, 16384}, {:args, ['-F' | path]}, {:cd, System.tmp_dir!}] + [:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}] ) end - defp start_port(:inotifywait, path) do + defp start_port(:inotifywait, path, listener_extra_args) do path = path |> format_path - args = [ '-c', 'inotifywait $0 $@ & PID=$!; read a; kill $PID', + args = [ listener_extra_args, '-c', 'inotifywait $0 $@ & PID=$!; read a; kill $PID', '-m', '-e', 'close_write', '-e', 'moved_to', '-e', 'create', '-e', 'delete_self', '-e', 'delete', '-r' | path ] @@ -45,9 +46,9 @@ defmodule ExFSWatch.Worker do [:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}] ) end - defp start_port(:"inotifywait_win32", path) do + defp start_port(:"inotifywait_win32", path, listener_extra_args) do path = path |> format_path - args = ['-m', '-r' | path] + args = [ listener_extra_args, '-m', '-r' | path] Port.open({:spawn_executable, :"inotifywait_win32".find_executable()}, [:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}] )