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

remove module callback api (#27)

parent 2f7f2c98
No related branches found
No related tags found
No related merge requests found
......@@ -59,31 +59,34 @@ and
{:file_event, worker_pid, :stop}
```
### Callback API
### Example with GenServer
You can also `use FileSystem` to define a module with a callback that will be called when filesystem events occur. This requires you to specify directories to watch at compile-time.
```elixir
defmodule Watcher do
use GenServer
write `lib/monitor.ex`
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
```elixir
defmodule Monitor do
use FileSystem, dirs: ["/tmp/test"]
def init(args) do
{:ok, watcher_pid} = FileSystem.start_link(args)
FileSystem.subscribe(watcher_pid)
{:ok, %{watcher_pid: watcher_pid}}
end
def callback(:stop) do
IO.puts "STOP"
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 callback(file_path, events) do
IO.inspect {file_path, events}
def handle_info({:file_event, ^watcher_pid, :stop}, %{watcher_pid: watcher_pid}=state) do
# YOUR OWN LOGIC WHEN MONITOR STOP
{:noreply, state}
end
end
```
Execute in iex
```shell
iex > Monitor.start
```
## Tweaking behaviour via listener extra arguments
......@@ -92,7 +95,7 @@ For each platform, you can pass extra arguments to the underlying listener proce
Here is an example to get instant notifications on file changes for Mac OS X:
```elixir
use FileSystem, dirs: ["/tmp/test"], listener_extra_args: "--latency=0.0"
FileSystem.start_link(dirs: ["/path/to/some/files"], listener_extra_args: "--latency=0.0")
```
See the [fs source](https://github.com/synrc/fs/tree/master/c_src) for more details.
......
defmodule FileSystem.ModuleApi do
defmacro __before_compile__(%Macro.Env{module: module}) do
options = Module.get_attribute(module, :file_system_module_options)
quote do
def start do
{:ok, worker_pid} = FileSystem.start_link(unquote(options))
pid = spawn_link(fn ->
FileSystem.subscribe(worker_pid)
await_events(worker_pid)
end)
{:ok, pid}
end
defp await_events(pid) do
receive do
{:file_event, ^pid, :stop} ->
callback(:stop)
{:file_event, ^pid, {file_path, events}} ->
callback(file_path, events)
await_events(pid)
end
end
end
end
end
defmodule FileSystem.ModuleApiTest do
use ExUnit.Case, async: true
test "module api" do
tmp_dir = System.cmd("mktemp", ["-d"]) |> elem(0) |> String.trim
Process.register(self(), :module_api_test)
ref = System.unique_integer
defmodule MyMonitor do
use FileSystem, dirs: [tmp_dir]
@ref ref
def callback(:stop), do: :stop
def callback(path, events), do: send(:module_api_test, {@ref, path, events})
end
MyMonitor.start
:timer.sleep(200)
File.touch("#{tmp_dir}/a")
assert_receive {^ref, _path, _events}, 5000
File.rm_rf!(tmp_dir)
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment