Filter chains, event emitter style, with globs and callbacks.
npm install glob-filter
Use Browserify to create a standalone file.
This implementation supports flow in both directions which allows each filter to apply logic before and / or after processing happens on the rest of the chain:
┌────────────┐ ┌────────────┐ ┌────────────┐ │ Filter A │ │ Filter B │ │ Filter C │ │ │ │ │ │ │ emit ──┼───> next ──┼──┼───> next ──┼──┼───> next ──┼───> then │ │ │ │ │ │ │ callback <─── callback <┼──┼─ callback <┼──┼─ callback <┼── callback │ │ │ │ │ │ └────────────┘ └────────────┘ └────────────┘
var GlobFilter = GlobFilter;var gf = ;gf;gf;
A filter which does not block the chain, the next filter will be invoked immediately:
A filter that controls when to invoke the next filter:
A filter that controls when to invoke the callback:
You can pass a
then function to invoke at the end of the filter chain to
emit(event[, then][, callback]): Invokes all filters registered for the given event. Matching rules are applied on the event name as descibed in the glob-tree match expressions. If a callback is passed, it will be invoked after all filters returned. If a
thenfunction is passed, it will be invoked after all filters called
next. It receives a callback as the only argument expecting to be called with
(err, value). Invoking the callback causes the filter callback chain to be invoked.
addFilter(event, fn): Registers a filter for an event
filterOnce(event, fn): Registers a filter for an event that is automatically removed on the first invocation
removeFilter(event, fn): Unregisters a filter for an event
removeAllFilters([event]): Unregisters all filters, or all filters for the given event. Matching rules are not applied.
removeMatchingFilters(event): Unregister all filters matching the given event name as described in the glob-tree match expressions.
filters([event][, options]): Returns all filters, or all filters for the given event. Matching rules are applied on the event name as described in the glob-tree match expressions.
options argument can have these properties:
matchers: Emit to matchers, defaults to
listeners: Emit to listeners, defaults to
The first argument passed to
emit can be an object with an
and any of the above options.
Filters are invoked with a special scope object. If an object is passed to
emit as the event (see Options), that object is used as the scope object.
It is also possible to bind individual filters to specific scope objects:
newFilter: Emitted by
filterOncewith the event name and the new filter function. Matchers will not receive this event.
removeFilter: Emitted by
removeAllFilterswith the event name and the removed filter function. Matchers will not receive this event.