Skip to content

Commit

Permalink
* simplify starting interface, by giving only the name and path
Browse files Browse the repository at this point in the history
* update README
  • Loading branch information
liveforeverx committed Nov 10, 2015
1 parent 60fbc06 commit 1548007
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ NOTE: On Linux you need to install inotify-tools.
### Subscribe to Notifications

```erlang
> fs:subscribe(). % the pid will receive events as messages
> fs:start_link(fs_watcher, "/Users/5HT/synrc/fs"). % need to start the fs watcher
> fs:subscribe(fs_watcher). % the pid will receive events as messages
> flush().
Shell got {<0.47.0>,
{fs,file_event},
Expand All @@ -23,7 +24,7 @@ Shell got {<0.47.0>,
### List Events from Backend

```erlang
> fs:known_events(). % returns events known by your current backend
> fs:known_events(fs_watcher). % returns events known by your current backend
[mustscansubdirs,userdropped,kerneldropped,eventidswrapped,
historydone,rootchanged,mount,unmount,created,removed,
inodemetamod,renamed,modified,finderinfomod,changeowner,
Expand Down
21 changes: 11 additions & 10 deletions src/fs.erl
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
-module(fs).
-include_lib("kernel/include/file.hrl").
-export([start_link/4, subscribe/1, known_events/0, start_looper/0, path/0, find_executable/2]).
-export([start_link/2, subscribe/1, known_events/1, start_looper/1, find_executable/2]).

% sample subscriber

start_link(SupName, EventHandler, FileHandler, Path) -> fs_sup:start_link(SupName, EventHandler, FileHandler, Path).
start_link(Name, Path) ->
SupName = name(Name, "sup"),
FileHandler = name(Name, "file"),
fs_sup:start_link(SupName, Name, FileHandler, Path).
subscribe(Name) -> gen_event:add_sup_handler(Name, {fs_event_bridge, self()}, [self()]).

subscribe() -> gen_event:add_sup_handler(fs_events, {fs_event_bridge, self()}, [self()]).
known_events() -> gen_server:call(fs_server, known_events).
start_looper() -> spawn(fun() -> subscribe(), loop() end).

path() ->
case application:get_env(fs, path) of
{ok, P} -> filename:absname(P);
undefined -> filename:absname("") end.
known_events(Name) -> gen_server:call(name(Name, "file"), known_events).
start_looper(Name) -> spawn(fun() -> subscribe(Name), loop() end).

loop() ->
receive
Expand Down Expand Up @@ -49,3 +46,7 @@ priv_file(Cmd) ->
false -> false end;
_ ->
false end.

name(Name, Prefix) ->
NameList = erlang:atom_to_list(Name),
list_to_atom(NameList ++ Prefix).

0 comments on commit 1548007

Please sign in to comment.