Class: Listen::QueueOptimizer
- Inherits:
-
Object
- Object
- Listen::QueueOptimizer
- Defined in:
- lib/listen/queue_optimizer.rb
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
private
Returns the value of attribute config.
Instance Method Summary collapse
- #_calculate_add_remove_difference(actions, path, default_if_exists) ⇒ Object private
- #_logical_action_for(path, actions) ⇒ Object private
-
#_reinterpret_related_changes(cookies) ⇒ Object
private
remove extraneous rb-inotify events, keeping them only if it's a possible editor rename() call (e.g. Kate and Sublime).
-
#_squash_changes(changes) ⇒ Object
private
groups changes into the expected structure expected by clients.
- #editor_modified?(changes) ⇒ Boolean private
-
#initialize(config) ⇒ QueueOptimizer
constructor
A new instance of QueueOptimizer.
- #smoosh_changes(changes) ⇒ Object
Constructor Details
#initialize(config) ⇒ QueueOptimizer
Returns a new instance of QueueOptimizer.
32 33 34 |
# File 'lib/listen/queue_optimizer.rb', line 32 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly, private)
Returns the value of attribute config.
38 39 40 |
# File 'lib/listen/queue_optimizer.rb', line 38 def config @config end |
Instance Method Details
#_calculate_add_remove_difference(actions, path, default_if_exists) ⇒ Object (private)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/listen/queue_optimizer.rb', line 69 def _calculate_add_remove_difference(actions, path, default_if_exists) added = actions.count { |x| x == :added } removed = actions.count { |x| x == :removed } diff = added - removed # TODO: avoid checking if path exists and instead assume the events are # in order (if last is :removed, it doesn't exist, etc.) if config.exist?(path) if diff > 0 :added elsif diff.zero? && added > 0 :modified else default_if_exists end else diff < 0 ? :removed : nil end end |
#_logical_action_for(path, actions) ⇒ Object (private)
61 62 63 64 65 66 67 |
# File 'lib/listen/queue_optimizer.rb', line 61 def _logical_action_for(path, actions) actions << :added if actions.delete(:moved_to) actions << :removed if actions.delete(:moved_from) modified = actions.find { |x| x == :modified } _calculate_add_remove_difference(actions, path, modified) end |
#_reinterpret_related_changes(cookies) ⇒ Object (private)
remove extraneous rb-inotify events, keeping them only if it's a possible editor rename() call (e.g. Kate and Sublime)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/listen/queue_optimizer.rb', line 91 def () table = { moved_to: :added, moved_from: :removed } .flat_map do |_, changes| if (editor_modified = editor_modified?(changes)) [[:modified, *editor_modified]] else not_silenced = changes.reject do |type, _, _, path, _| config.silenced?(Pathname(path), type) end not_silenced.map do |_, change, dir, path, _| [table.fetch(change, change), dir, path] end end end end |
#_squash_changes(changes) ⇒ Object (private)
groups changes into the expected structure expected by clients
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/listen/queue_optimizer.rb', line 42 def _squash_changes(changes) # We combine here for backward compatibility # Newer clients should receive dir and path separately changes = changes.map { |change, dir, path| [change, dir + path] } actions = changes.group_by(&:last).map do |path, action_list| [_logical_action_for(path, action_list.map(&:first)), path.to_s] end config.debug("listen: raw changes: #{actions.inspect}") { modified: [], added: [], removed: [] }.tap do |squashed| actions.each do |type, path| squashed[type] << path unless type.nil? end config.debug("listen: final changes: #{squashed.inspect}") end end |
#editor_modified?(changes) ⇒ Boolean (private)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/listen/queue_optimizer.rb', line 107 def editor_modified?(changes) return unless changes.size == 2 from_type = from = nil to_type = to_dir = to = nil changes.each do |data| case data[1] when :moved_from from_type, _from_change, _, from, = data when :moved_to to_type, _to_change, to_dir, to, = data end end # Expect an ignored moved_from and non-ignored moved_to # to qualify as an "editor modify" if from && to && config.silenced?(Pathname(from), from_type) && !config.silenced?(Pathname(to), to_type) [to_dir, to] end end |
#smoosh_changes(changes) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/listen/queue_optimizer.rb', line 24 def smoosh_changes(changes) # TODO: adapter could be nil at this point (shutdown) = changes.group_by do |_, _, _, _, | ( || {})[:cookie] end _squash_changes(()) end |