As discussed at length in patch 606306 - checkboxHack: Trigger 'input' event when checkbox state changes the public API for the checkBox module could be improved by converting it from a model where we have multiple initialization functions to one where we have a single initialization function with an options object and a callback function to hook into the checkbox's change events.
As summarized by @Demian in T254851#6270677 :
In discussion with @Krinkle it came up that the interface of CheckboxHack is not simple enough and the responsibilities of the module and its users aren't clearly separated. The interface now looks like:
module.exports = { updateAriaExpanded: updateAriaExpanded, bindUpdateAriaExpandedOnInput: bindUpdateAriaExpandedOnInput, bindToggleOnClick: bindToggleOnClick, bindDismissOnClickOutside: bindDismissOnClickOutside, bindDismissOnFocusLoss: bindDismissOnFocusLoss, bind: bind, unbind: unbind };
It's the client's responsibility to properly combine these functions.
I've reduced this into:
/** * Public API */ module.exports = CheckboxHack;
Usage:
checkboxHack = new CheckboxHack( window, checkbox, button, { onChange: saveSidebarState } );
Further named parameters can be provided in the options object to disable individual features, such as click and key handling.
Benefit
No need to change Vector when the CheckboxHack's implementation is changed, eg. 608451.
Clean, progressively extensible API, separation of concerns.
Patch dependency graph
[ 608744 new API] ->
[ 606541 Vector patch] ->
[ 608756 "onChange() callback"] -> ... ->
[ 608755 "Cleanup"] ->
[ 606306 "Trigger 'input' event"] (optional for analytics)
This sequence enables SPACE, ENTER and a hook for saving the sidebar state for T255727.
The patches are minimal, focused on just one change to make review easier. If wanted, the patches can be squashed after being reviewed.
The code is augmented with strict type annotations, prepared for all jsduck, eslint and tsc. Briefly: type safe. The difficulty was TS and jsduck understanding a bit different dialect.