Skip to content

Refactor global init code and add more comments #33755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 3, 2025

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Mar 1, 2025

Follow up #33748 (refactor a lot and let's forget the old code: https://github.com/go-gitea/gitea/pull/33755/files?diff=split&w=0)

Now there are 3 "global" functions:

  • registerGlobalSelectorFunc: for all elements matching the selector, eg: .ui.dropdown
  • registerGlobalInitFunc: for data-global-init="initInputAutoFocusEnd"
  • registerGlobalEventFunc: for data-global-click="onCommentReactionButtonClick"

And introduce initGlobalInput to replace old initAutoFocusEnd and attachDirAuto, use data-global-init to replace fragile .js-autofocus-end selector.

Another benefit is that by the new approach, no matter how many times registerGlobalInitFunc is called, we only need to do one "querySelectorAll" in the last step, it could slightly improve the performance.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 1, 2025
@github-actions github-actions bot added modifies/templates This PR modifies the template files modifies/frontend labels Mar 1, 2025
@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Mar 1, 2025

The "autofocus-end" and "dir=auto" all work as expected:

image

And the performance trace:

performance trace: initHeatmap 14.500
performance trace: initGlobalSelectorObserver .ui.dropdown:not(.custom) 5.600
performance trace: initDashboardRepoList 3.300
performance trace: initRepoPullRequestReview 0.400

@wxiaoguang wxiaoguang force-pushed the refactor-global-init branch from 95000ee to 0b0ff57 Compare March 1, 2025 03:22
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Mar 1, 2025
@wxiaoguang wxiaoguang force-pushed the refactor-global-init branch from 0b0ff57 to c8b753e Compare March 1, 2025 03:26
@wxiaoguang wxiaoguang force-pushed the refactor-global-init branch from c8b753e to 2e42865 Compare March 1, 2025 03:41
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 1, 2025
@lunny lunny added this to the 1.24.0 milestone Mar 1, 2025
@silverwind
Copy link
Member

silverwind commented Mar 1, 2025

I'm not sure I like the "global" name used here. It seems superflous. Why is it named like that?

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Mar 1, 2025

I'm not sure I like the "global" name used here. It seems superflous. Why is it named like that?

I think the meaning is accurate. Because there are "global" event handlers and init function callers (they affect all elements), and it matches data-global-xxx. We can't use data-init or data-click, at least, we need some "namespace" to group the function&attribute names.


If you have better and accurate names, please advise.

And since these names are unique, even we keep them, we could always be able to easily change them by a search&replace in the future.

@wxiaoguang wxiaoguang force-pushed the refactor-global-init branch from 621bd65 to 47244e9 Compare March 1, 2025 10:46
@wxiaoguang wxiaoguang force-pushed the refactor-global-init branch from 47244e9 to 9208f4f Compare March 1, 2025 10:55
Comment on lines 10 to 13
for (let i = 0; i < 20 && i < this.results.length; i++) {
// eslint-disable-next-line no-console
console.log(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let i = 0; i < 20 && i < this.results.length; i++) {
// eslint-disable-next-line no-console
console.log(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
}
for (const [index, {name, dur}] of this.results.entries()) {
if (index >= 20) break;
console.info(`performance trace: ${name} ${dur.toFixed(3)}`);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is performant, by the for ( let ...; i < len; ...) loop, we do not need to create new "entries" object or extra if check

Comment on lines +21 to +25
if (perfTracer) {
for (const func of functions) perfTracer.recordCall(func.name, func);
} else {
for (const func of functions) func();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (perfTracer) {
for (const func of functions) perfTracer.recordCall(func.name, func);
} else {
for (const func of functions) func();
}
for (const func of functions) {
perfTracer?.recordCall(func.name, func) ?? func();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is performant.

By using the if, we do not need to check perfTracer anymore in the loop

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 3, 2025
@wxiaoguang wxiaoguang merged commit 27bf63a into go-gitea:main Mar 3, 2025
26 checks passed
@wxiaoguang wxiaoguang deleted the refactor-global-init branch March 3, 2025 02:57
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 3, 2025
* giteaofficial/main:
  Disable `vet` as part of `go test` (go-gitea#33662)
  Refactor error system (go-gitea#33771)
  Add migrations and doctor fixes (go-gitea#33556)
  Refactor mail code (go-gitea#33768)
  Refactor global init code and add more comments (go-gitea#33755)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/frontend modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants