Add the filter to the dispatcher.
Return a handle which can be used in removeFilter.
Remove a filter from the dispatcher.
filter
can be either the filter callback or the handle returned by appendFilter
.
Return true if the filter is removed successfully.
Generated using TypeDoc
MixinFilter allows all events are filtered or modified before dispatching.
MixinFilter.appendFilter(filter)
adds an event filter to the dispatcher. Thefilter
receives an array same as mixinBeforeDispatch receives.The event filters are invoked for all events, and invoked before any listeners are invoked. The event filters can modify the arguments since the arguments are passed in the array.
Event filter is a powerful and useful technology, below is some sample use cases, though the real world use cases are unlimited.
1, Capture and block all interested events. For example, in a GUI window system, all windows can receive mouse events. However, when a window is under mouse dragging, only the window under dragging should receive the mouse events even when the mouse is moving on other window. So when the dragging starts, the window can add a filter. The filter redirects all mouse events to the window and prevent other listeners from the mouse events, and bypass all other events.
2, Setup catch-all event listener. For example, in a phone book system, the system sends events based on the actions, such as adding a phone number, remove a phone number, look up a phone number, etc. A module may be only interested in special area code of a phone number, not the actions. One approach is the module can listen to all possible events (add, remove, look up), but this is very fragile -- how about a new action event is added and the module forgets to listen on it? The better approach is the module add a filter and check the area code in the filter.