-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Use NIO.2 WatchService for JRuby
- Loading branch information
1 parent
dc1e798
commit 6fb459e
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Listen | ||
module Adapter | ||
# @see https://docs.oracle.com/javase/tutorial/essential/io/notification.html | ||
class Jruby < Base | ||
def self.usable? | ||
RUBY_ENGINE == 'jruby' | ||
end | ||
|
||
private | ||
|
||
def _configure(directory, &_callback) | ||
require 'java' | ||
java_import 'java.nio.file.FileSystems' | ||
java_import 'java.nio.file.Paths' | ||
java_import 'java.nio.file.StandardWatchEventKinds' | ||
|
||
event_kinds = [ | ||
StandardWatchEventKinds::ENTRY_CREATE, | ||
StandardWatchEventKinds::ENTRY_MODIFY, | ||
StandardWatchEventKinds::ENTRY_DELETE | ||
] | ||
|
||
@watcher ||= FileSystems.getDefault.newWatchService | ||
@keys ||= {} | ||
path = Paths.get(directory.to_s) | ||
key = path.register(@watcher, *event_kinds) | ||
@keys[key] = path | ||
end | ||
|
||
def _run | ||
loop do | ||
key = @watcher.take | ||
dir = @keys[key] | ||
unless dir.nil? | ||
key.pollEvents.each do |event| | ||
kind = event.kind | ||
next if kind == StandardWatchEventKinds::OVERFLOW | ||
name = event.context | ||
child = dir.resolve(name) | ||
pathname = Pathname.new(child.to_s).dirname | ||
_queue_change(:dir, pathname, '.', recursive: true) | ||
end | ||
end | ||
valid = key.reset | ||
unless valid | ||
@keys.delete(key) | ||
break if @keys.empty? | ||
end | ||
end | ||
end | ||
|
||
def _process_event(dir, event); end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
RSpec.describe Listen::Adapter::Jruby do | ||
describe 'class' do | ||
subject { described_class } | ||
|
||
if jruby? | ||
it { should be_usable } | ||
else | ||
it { should_not be_usable } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters