Skip to content
Snippets Groups Projects
Commit d1b1657e authored by Xiangrong Hao's avatar Xiangrong Hao Committed by GitHub
Browse files

Merge pull request #9 from thbar/custom-listener-args

Support optional listener args
parents d457514a 1300de73
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,18 @@ Execute in iex ...@@ -60,6 +60,18 @@ Execute in iex
iex > Monitor.start 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 ## List Events from Backend
```shell ```shell
......
require Logger require Logger
defmodule ExFSWatch do defmodule ExFSWatch do
defmacro __using__([dirs: dirs]) do defmacro __using__(options) do
quote 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__ def start, do: ExFSWatch.Supervisor.start_child __MODULE__
end end
end end
......
...@@ -9,7 +9,7 @@ defmodule ExFSWatch.Worker do ...@@ -9,7 +9,7 @@ defmodule ExFSWatch.Worker do
def init(module) do def init(module) do
backend = ExFSWatch.backend 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}} {:ok, %__MODULE__{port: port, backend: backend, module: module}}
end end
...@@ -29,15 +29,16 @@ defmodule ExFSWatch.Worker do ...@@ -29,15 +29,16 @@ defmodule ExFSWatch.Worker do
end end
defp start_port(:fsevents, path) do defp start_port(:fsevents, path, listener_extra_args) do
path = path |> format_path path = path |> format_path
args = [ listener_extra_args, '-F' | path]
Port.open({:spawn_executable, :fsevents.find_executable()}, 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 end
defp start_port(:inotifywait, path) do defp start_port(:inotifywait, path, listener_extra_args) do
path = path |> format_path 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', '-m', '-e', 'close_write', '-e', 'moved_to', '-e', 'create', '-e',
'delete_self', '-e', 'delete', '-r' | path 'delete_self', '-e', 'delete', '-r' | path
] ]
...@@ -45,9 +46,9 @@ defmodule ExFSWatch.Worker do ...@@ -45,9 +46,9 @@ defmodule ExFSWatch.Worker do
[:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}] [:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}]
) )
end end
defp start_port(:"inotifywait_win32", path) do defp start_port(:"inotifywait_win32", path, listener_extra_args) do
path = path |> format_path path = path |> format_path
args = ['-m', '-r' | path] args = [ listener_extra_args, '-m', '-r' | path]
Port.open({:spawn_executable, :"inotifywait_win32".find_executable()}, Port.open({:spawn_executable, :"inotifywait_win32".find_executable()},
[:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}] [:stream, :exit_status, {:line, 16384}, {:args, args}, {:cd, System.tmp_dir!}]
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment