Custom callbacks for filters #36

Closed
scottdavis opened this Issue Jun 4, 2012 · 6 comments

Projects

None yet

3 participants

FSSM had a way you could filter on globs with different callback methods then the global scope.

see:

https://github.com/ttilley/fssm/blob/master/lib/fssm/monitor.rb#L7-14

implementation in compass

https://github.com/chriseppstein/compass/blob/stable/lib/compass/commands/watch_project.rb#L99-118

Im fine with implementing this as a pull request but I want to start the discussion that it is something listen wants to support.

So in listen something like

listen.filter("som/glob/**/*")  do | modified, added, removed |
  ... do something cool
end
Owner

Hi Scott,

Listen use the Find Ruby class to traverse all paths, so I'm not sure that global scope could be easily added. What do you think @Maher4Ever?

Member

As @thibaudgg stated, this can be a little challenging to implement. Listen was not designed to handle this usecase. Adding it now is possible, but I'm a little bit concerned with the semantics of the API. Listen already has a filter method which takes regex patterns, but that's only a method name and can easily be changed.
Listen also has two ways to register a "global" callback, namely through the change method or by passing a block to the initializer. The change method can be used to change the callback if needed.

Adding a method which adds a callback (via a passed block) for a specific pattern would mean one of two things for multiple calls:

  • There can only be one callback for a specific pattern, because subsequent calls would replace the old callback with the new one. This is how the global callback works now.
  • Subsequent calls with the same pattern would keep adding callbacks for that pattern. This means a callback could not be replaced and/or removed.

Maybe a new class could be introduced to handle this situation. Something like:

listener = Listen.to('dir') { ... }

# Simple usecases can be implmented like:
listener.filtered_callback(%r{your_filter}) { ... } 

# Advanced usecases:
callbacks = Listen::CallbacksSet.new(%r{your_filter})
callbacks.add(&callback1).add(&callback2)

# Replace a callback
callbacks.replace(&callback2, &callback3)

# Remove a callback
callbacks.remove(&callback1)

# Register the callbacks with the listener
listener.filter_callbacks(callbacks)

Even the current change method could take an object (maybe the same Listen::CallbacksSet without a filter) to allow registering multiple "global" callbacks.

These are all ideas after all, so I'm interested in what you think about them.

I was more wanting to start the question of is liten going to support feature sets of FSSM as part of its goal of becoming the replacement. I can have around and fake this functionally using a global call back i just wanted to make sure that this wasn't something that was missed.

Owner

I'm not sure that all features (or way of doing things) of FSSM would fit properly in Listen, the more import ant I think is that the same achievement can be done (even if it needs to be implemented differently on the client side).

@ttilley seems ok for you?

i ended up hacking around this you can check it out here if your interested https://github.com/chriseppstein/compass/blob/listen/lib/compass/watcher/project_watcher.rb

@scottdavis scottdavis closed this Oct 20, 2012
Owner

@scottdavis great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment