Events.filter() function
Filter the queued events by merging duplicates, removing null events and reording BlockChange events.
History of this function:
This function was originally added in commit cf257ea5 with the intention of dramatically reduing the total number of dispatched events. Initialy it affected only BlockMove events but others were added over time.
Code was added to reorder BlockChange events added in commit 5578458, for uncertain reasons but most probably as part of an only-partially-successful attemp to fix problems with event ordering during block mutations. This code should probably have been added to the top of the function, before merging and null-removal, but was added at the bottom for now-forgotten reasons. See these bug investigations for a fuller discussion of the underlying issue and some of the failures that arose because of this incomplete/incorrect fix:
https://github.com/google/blockly/issues/8225#issuecomment-2195751783 https://github.com/google/blockly/issues/2037#issuecomment-2209696351
Later, in PR #1205 the original O(n^2) implementation was replaced by a linear-time implementation, though additonal fixes were made subsequently.
In August 2024 a number of significant simplifications were made:
This function was previously called from Workspace.prototype.undo, but the mutation of events by this function was the cause of issue #7026 (note that events would combine differently in reverse order vs. forward order). The originally-chosen fix for this was the addition (in PR #7069) of code to fireNow to post-filter the .undoStack_ and .redoStack_ of any workspace that had just been involved in dispatching events; this apparently resolved the issue but added considerable additional complexity and made it difficult to reason about how events are processed for undo/redo, so both the call from undo and the post-processing code was removed, and forward=true was made the default while calling the function with forward=false was deprecated.
At the same time, the buggy code to reorder BlockChange events was replaced by a less-buggy version of the same functionality in a new function, enqueueEvent, called from fireInternal, thus assuring that events will be in the correct order at the time filter is called.
Additionally, the event merging code was modified so that only immediately adjacent events would be merged. This simplified the implementation while ensuring that the merging of events cannot cause them to be reordered.
Signature:
export declare function filter(queue: Abstract[]): Abstract[];
Parameters
Parameter | Type | Description |
---|---|---|
queue | Abstract[] | Array of events. |
Returns:
Abstract[]
Array of filtered events.