I code, design and build digital stuff.
petoem / inotify.cr Goto Github PK
View Code? Open in Web Editor NEWInotify bindings for Crystal
License: MIT License
Inotify bindings for Crystal
License: MIT License
I code, design and build digital stuff.
Hi,
Dir has no foreach() method anymore (at least in Crystal 0.24), and the check for "." and ".." may be removed if using each_child.
Only occurrence is here: src/inotify/watcher.cr line 103
I don't know if you want to keep backward compatibility or whatever, but would be nice to fix this, otherwise it works well :)
Thanks
Hi,
In crystal 0.25, "File.stat was renamed to File.info and a more portable API was implemented".
Here the only change to be compatible with 0.25 is in fallback.cr, 3rd line (File::Stat ⇒ File::Info). But it will break older versions…
Example code in readme produce below error
crystal build -o /Users/weaming/src/project-name/bin/test src/test.cr
Error target test failed to compile:
Error in src/test.cr:3: undefined method 'watch' for Inotify:Module
watcher = Inotify.watch "./nginx.log" do |event|
^~~~~
Error target log-watcher failed to compile:
Error in lib/inotify/src/inotify/watcher.cr:21: Can't infer the type of instance variable '@event_callbacks' of Inotify::Watcher
The type of a instance variable, if not declared explicitly with
`@event_callbacks : Type`, is inferred from assignments to it across
the whole program.
The assignments must look like this:
1. `@event_callbacks = 1` (or other literals), inferred to the literal's type
2. `@event_callbacks = Type.new`, type is inferred to be Type
3. `@event_callbacks = Type.method`, where `method` has a return type
annotation, type is inferred from it
4. `@event_callbacks = arg`, with 'arg' being a method argument with a
type restriction 'Type', type is inferred to be Type
5. `@event_callbacks = arg`, with 'arg' being a method argument with a
default value, type is inferred using rules 1, 2 and 3 from it
6. `@event_callbacks = uninitialized Type`, type is inferred to be Type
7. `@event_callbacks = LibSome.func`, and `LibSome` is a `lib`, type
is inferred from that fun.
8. `LibSome.func(out @event_callbacks)`, and `LibSome` is a `lib`, type
is inferred from that fun argument.
Other assignments have no effect on its type.
Can't infer the type of instance variable '@event_callbacks' of Inotify::Watcher
@event_callbacks = [] of Proc(Event, Nil)
^~~~~~~~~~~~~~~~
Thanks for making such a useful lib!
I would like to debounce events so that I don't get notified unless no events have happened for at least a second. I came up with the following but it seems hackish, do you have an idea of a better way to do it? In the end, would like to use the code to keep directory synced up.
require "inotify"
class Sync
def initialize
@events = [] of Inotify::Event
@t = Time.now
watch()
end
def push_event(event)
@events.push(event)
@t = Time.now
end
def watch
Inotify::Watcher.new(".") do |event|
push_event(event)
end
end
def process_event(event)
pp event
type = event.event_type
if event.event_type == Inotify::EventType::MOVE
if event.mask == LibInotify::IN_MOVED_FROM
type = "DELETED"
elsif event.mask == LibInotify::IN_MOVED_TO
type = "CREATE"
end
end
puts [event.name, type].join(" => ")
end
def do_loop
loop do
sleep 1
if @events.size > 0 && Time.now - @t > 1.second
seen = {} of String => Bool
while e = @events.shift?
if ! seen["#{e.event_type}-#{e.name}"]?
process_event(e)
seen["#{e.event_type}-#{e.name}"] = true
end
# pp e
end
else
puts "PING"
end
end
end
end
s = Sync.new
s.do_loop()
Release 0.29.0 of crystal-lang included a breaking change that removes Slice#pointer, which is currently used.
I'm the author of https://github.com/j8r/tail
To tail a file, I essentially do:
file = File.new path
Inotify::Watcher.new file.path do |event|
if !(content = file.gets_to_end).empty?
puts content
end
end
Which is not the most efficient for tailing only one file.
I see also there is a @io.read(slice)
on the lurk
method, would it be possible to have a method yielding the appended data?
initialize
of Watcher
and Fallback
not accept a block.Watcher
and Fallback
can even become structs.A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.