Improvement: config registration form settings#3932
Conversation
|
published as "Draft" PR to collect your feedback. Open todo:
|
|
Looks good :-) |
|
Ready for review |
|
Try to run the new |
|
Hum, it looks there is some old code in there. Maybe an old branch? |
|
the failed test I will check later |
|
It is not only the tests: it looks like some files are based on old versions, e.g. |
p/scripts/config.js
Outdated
| function init_maxNumbersOfAccountsStatus() { | ||
| const input = document.getElementById('max-registrations-input'); | ||
| if (input) { | ||
| input.addEventListener('click', function() { onchange_maxNumbersOfAccounts(this); }); |
There was a problem hiding this comment.
There's no need to use anonymous functions for this kind of usage.
| input.addEventListener('click', function() { onchange_maxNumbersOfAccounts(this); }); | |
| input.addEventListener('click', onchange_maxNumbersOfAccounts); |
And then something like:
// I think we use ev rather than e or event? Not 100% sure otoh.
function onchange_maxNumbersOfAccounts(ev) {
const elem = ev.target;There was a problem hiding this comment.
I use the function also for
onchange_maxNumbersOfAccounts(input);
any suggestion to fix it?
There was a problem hiding this comment.
Did you mean to write onchange_selectInputChanger? The same way,
function onchange_selectInputChanger(ev) {
const elem = ev.targetAnother thing is the name btw, but I'll comment about that on another line.
There was a problem hiding this comment.
ev.target does not work for input while calling onchange_maxNumbersOfAccounts(input);
There was a problem hiding this comment.
I think something like an init event might be a touch more elegant, but let's leave it like this then. Any performance/memory related to anonymous functions is completely irrelevant here.
input.dispatchEvent(new Event('change', {
bubbles: true,
cancelable: true,
}));I would like the event listener and/or the function name to be less deceptive about what's going on, however. ;-) change is probably the right event to use here.
There was a problem hiding this comment.
thanks, it helped me a lot. That are the small little things I cannot google it, I need it told from an expert :)
There was a problem hiding this comment.
The tl;dr is that if you add a lot of event listeners your page's memory usage can balloon, especially combined with anonymous functions. If you ever see a page that's just doing something like document.addEventListener('click', … and then checking if ev.target === some element, that's why. (Or perhaps not on the document, but in any case on the parent container div/form/whatever instead of the individual checkboxes.) While that's definitely not an issue here, one thing I've learned as a maintainer is that letting through something seemingly innocuous will suddenly be copied by someone else several years later as an example to follow, almost guaranteed. :-) Besides the memory usage, all the bubbling through all of the elements that have an event listener attached can also take much longer overall when you have a lot of them.
It's particularly when you're adding anonymous functions programmatically, like here in a for loop, that it can become problematic. For one-offs it probably doesn't make an appreciable difference, at least in memory usage, although it can affect performance in slightly unexpected ways (e.g., a regularly declared function might be JIT-compiled better than an anonymous one, at least in the past).
I'm not sure if what I steered this towards is necessarily the best way to approach this particular problem; you could also write something like, at its simplest:
const elem = ev.target || ev;I just happen to find it more elegant to use the event listener itself instead of hacking the function to take either an event or an element. Another, probably clearer and better approach would be some indirection, i.e., a function that always takes an element (your original function) and a second non-anonymous function for the event listener that simply calls the first function with ev.target.
So much for tl;dr… but in any case, I hope that explains why adding anonymous functions in a for loop triggered my radar. I don't like pages that waste a lot of memory. ;-)
p/scripts/config.js
Outdated
| } | ||
| } | ||
|
|
||
| function onchange_selectInputChanger(elem) { |
There was a problem hiding this comment.
I would name this something more generic, otherwise it's very easy to get at best strange and at worst misleading function names like we see here. However, the function name is correct that you should probably be listening for change, not for click.
| function onchange_selectInputChanger(elem) { | |
| function updateSelectInput(elem) { |
|
@Alkarex : I do not understand why |
|
I updated main.js from edge manually. Now it works and passed the checks |
|
Let's make a stable release 1.19 before we land this PR in the following 1.20 work (due in particular to new translation strings) |
|
Cannot find the root of this test issue @aledeg Could you please check? |
Regression from FreshRSS#3932 The data-leave-validation mechanism would always say that max-registrations-input has changed when navigating to another page
| <div class="group-controls"> | ||
| <?php $number = count(listUsers()); ?> | ||
| <input type="number" id="max-registrations-input" name="" value="<?= FreshRSS_Context::$system_conf->limits['max_registrations'] > 1 ? FreshRSS_Context::$system_conf->limits['max_registrations'] : $number + 1; ?>" min="2" | ||
| data-leave-validation="<?= FreshRSS_Context::$system_conf->limits['max_registrations'] ?>" data-number="<?= $number ?>"/> |
Regression from #3932 The data-leave-validation mechanism would always say that max-registrations-input has changed when navigating to another page
Closes #981
Changes proposed in this pull request:
Before:

After:

Value "1": No registration form, because minimum 1 account exists
Value "0": unlimited accounts

Value "x": set the number

by default it will give a number of amount of existing accounts + 1, so that the registration form will be enabled
If the set number is bigger than the number of existing account, than the text "form enabled" will be shown.

Else: "form disabled".
The text is calculated "live"
Pull request checklist: