Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
elixir__file_system
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bioroboticslab
robofish
electrofish
vendor
elixir__file_system
Commits
1292b1c5
Commit
1292b1c5
authored
7 years ago
by
Xiangrong Hao
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
remove module callback api (#27)
parent
2f7f2c98
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+19
-16
19 additions, 16 deletions
README.md
lib/file_system/module_api.ex
+0
-25
0 additions, 25 deletions
lib/file_system/module_api.ex
test/module_api_test.exs
+0
-23
0 additions, 23 deletions
test/module_api_test.exs
with
19 additions
and
64 deletions
README.md
+
19
−
16
View file @
1292b1c5
...
...
@@ -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/t
es
t
"
],
listener_extra_args:
"--latency=0.0"
FileSystem
.
start_link
(
dirs:
[
"/path/to/some/fil
es"
],
listener_extra_args:
"--latency=0.0"
)
```
See the
[
fs source
](
https://github.com/synrc/fs/tree/master/c_src
)
for more details.
...
...
This diff is collapsed.
Click to expand it.
lib/file_system/module_api.ex
deleted
100644 → 0
+
0
−
25
View file @
2f7f2c98
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
This diff is collapsed.
Click to expand it.
test/module_api_test.exs
deleted
100644 → 0
+
0
−
23
View file @
2f7f2c98
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment