0

I am trying to perform an action based on changes in the folder which are detected by synrc/fs library. I want to receive this notification every time the changes captured by fs & perform an action e.g. printing the changed filename.

I tried below code but executes only first time!

say_hello() ->
    fs:start_link(fs_watcher, "/Users/foldername"),
    fs:subscribe(fs_watcher),
    receive
        {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
            io:format("~p was ~p ~n",[ChangedFile,File_event])
    end.

Any useful help is appreciated along with link & description if possible! Thanks :)

nikdange_me
  • 2,949
  • 2
  • 16
  • 24
  • @Dogbert you both gave me kinda similar answer!! appreciate a lot!! :) I have upvoted both of your answer!! but m confused whose answer to select (you both answer on exact time too) :| – nikdange_me Mar 21 '17 at 15:03
  • @AsierAzkuenaga you both gave me kinda similar answer!! appreciate a lot!! :) I have upvoted both of your answer!! but m confused whose answer to select (you both answer on exact time too) :| – nikdange_me Mar 21 '17 at 15:04

2 Answers2

2

If you want the function to keep receiving the same kind of messages you could use recursion:

say_hello() ->
  fs:start_link(fs_watcher, "/Users/foldername"),
  fs:subscribe(fs_watcher),
  recur().

recur()->
  receive
     {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
         io:format("~p was ~p ~n",[ChangedFile,File_event]),
         recur()
  end.

You would have to then think about a way to finalise the function.

Asier Azkuenaga
  • 1,199
  • 6
  • 17
2

You need to recursively call receive:

say_hello() ->
    fs:start_link(fs_watcher, "/Users/foldername"),
    fs:subscribe(fs_watcher),
    loop().

loop() ->
    receive
        {Watcher_process, {Fs, File_event}, {ChangedFile, Type}} ->
            io:format("~p was ~p ~n",[ChangedFile,File_event]),
            loop()
    end.
Dogbert
  • 212,659
  • 41
  • 396
  • 397