From 1292b1c570bbaf9b206b04c02b8ddf5d0567c9f8 Mon Sep 17 00:00:00 2001
From: Xiangrong Hao <falood@gmail.com>
Date: Sun, 23 Jul 2017 10:49:22 -0700
Subject: [PATCH] remove module callback api (#27)

---
 README.md                     | 35 +++++++++++++++++++----------------
 lib/file_system/module_api.ex | 25 -------------------------
 test/module_api_test.exs      | 23 -----------------------
 3 files changed, 19 insertions(+), 64 deletions(-)
 delete mode 100644 lib/file_system/module_api.ex
 delete mode 100644 test/module_api_test.exs

diff --git a/README.md b/README.md
index e35b5c2..d188ad8 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/lib/file_system/module_api.ex b/lib/file_system/module_api.ex
deleted file mode 100644
index 3139ba6..0000000
--- a/lib/file_system/module_api.ex
+++ /dev/null
@@ -1,25 +0,0 @@
-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
diff --git a/test/module_api_test.exs b/test/module_api_test.exs
deleted file mode 100644
index 5f568eb..0000000
--- a/test/module_api_test.exs
+++ /dev/null
@@ -1,23 +0,0 @@
-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
-- 
GitLab