Possible command listener performance improvements #1412
Description
I'm working on adding command listeners into iOS, and found the below a little confusing as a n00b.
Issue
We allow engineers to add logic for different command types into the same command listener callbacks, forcing them to check for command type, whilst also allowing them to call listener callbacks out of order with different priorities.
editor.addListener(
'command',
(type, ...payload) => {
if (type === 'insertImage') {
// ...
return true;
} else if (type === 'insertVideo') {
// ...
return true;
}
return false;
},
0,
);
// The below is called first.
editor.addListener(
'command',
(type, ...payload) => {
if (type === 'insertImage') {
// ...
return true;
} else if (type === 'insertVideo') {
// ...
return true;
}
return false;
},
1,
);
I don't think it's a major problem but on a larger application, it could make the command the logic a little hard to follow.
Proposal
i. Remove the need to check for command type from the function, by specifying it as an argument to addListener
:
editor.addListener(
'command',
'insertImage'
(payload) => {
// ...;
},
);
This should reduce the number of times every command listener is called, remove the overhead for developers to always need to check the command type, and it should allow us to better type the payload for native Lexical command types (i.e. we can remove this Flow any
in favour of a conditional type).
ii. Remove priority ordering for command listeners in favour of ordering by when the listener is attached to the editor, or abstract it out of addListener
by triggering Lexical native command listeners before user defined ones.
In the existing user plugins (i.e. ImageNode
, HorizontalRulePlugin
, MentionsPlugin
), each command listener always has a very low priority, so these listeners are already called last.
Honestly, I just added it into HorizontalRulePlugin
because I didn't really know what would happen if I set it to be high, which makes me wonder if we could just remove it altogether?