Skip to content

Commit 621bd65

Browse files
committed
introduce init.ts
1 parent adaf47e commit 621bd65

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

web_src/js/index.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,14 @@ import {initGlobalFetchAction} from './features/common-fetch-action.ts';
7070
import {initFootLanguageMenu, initGlobalDropdown, initGlobalInput, initGlobalTabularMenu, initHeadNavbarContentToggle} from './features/common-page.ts';
7171
import {initGlobalButtonClickOnEnter, initGlobalButtons, initGlobalDeleteButton} from './features/common-button.ts';
7272
import {initGlobalComboMarkdownEditor, initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.ts';
73+
import {callInitFunctions} from "./modules/init.ts";
7374

7475
initGiteaFomantic();
7576
initSubmitEventPolyfill();
7677

77-
// Start performance trace by accessing a URL by "https://localhost/?_ui_performance_trace=1" or "https://localhost/?key=value&_ui_performance_trace=1"
78-
// It is a quick check, no side effect so no need to do slow URL parsing.
79-
const traceInitPerformance = !window.location.search.includes('_ui_performance_trace=1') ? null : new class {
80-
results: {name: string, dur: number}[] = [];
81-
recordCall(name: string, func: ()=>void) {
82-
const start = performance.now();
83-
func();
84-
this.results.push({name, dur: performance.now() - start});
85-
}
86-
printResults() {
87-
this.results = this.results.sort((a, b) => b.dur - a.dur);
88-
for (let i = 0; i < 20 && i < this.results.length; i++) {
89-
// eslint-disable-next-line no-console
90-
console.log(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
91-
}
92-
}
93-
}();
94-
95-
function callInitFunctions(functions: (() => any)[]) {
96-
if (traceInitPerformance) {
97-
for (const func of functions) traceInitPerformance.recordCall(func.name, func);
98-
} else {
99-
for (const func of functions) func();
100-
}
101-
}
102-
10378
onDomReady(() => {
10479
const initStartTime = performance.now();
105-
callInitFunctions([
80+
const initPerformanceTracer = callInitFunctions([
10681
initGlobalDropdown,
10782
initGlobalTabularMenu,
10883
initGlobalFetchAction,
@@ -199,9 +174,9 @@ onDomReady(() => {
199174
]);
200175

201176
// it must be the last one, then the "querySelectorAll" only needs to be executed once for global init functions.
202-
initGlobalSelectorObserver(traceInitPerformance);
177+
initGlobalSelectorObserver(initPerformanceTracer);
178+
if (initPerformanceTracer) initPerformanceTracer.printResults();
203179

204-
if (traceInitPerformance) traceInitPerformance.printResults();
205180
const initDur = performance.now() - initStartTime;
206181
if (initDur > 500) {
207182
console.error(`slow init functions took ${initDur.toFixed(3)}ms`);

web_src/js/modules/init.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class InitPerformanceTracer {
2+
results: {name: string, dur: number}[] = [];
3+
recordCall(name: string, func: ()=>void) {
4+
const start = performance.now();
5+
func();
6+
this.results.push({name, dur: performance.now() - start});
7+
}
8+
printResults() {
9+
this.results = this.results.sort((a, b) => b.dur - a.dur);
10+
for (let i = 0; i < 20 && i < this.results.length; i++) {
11+
// eslint-disable-next-line no-console
12+
console.log(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
13+
}
14+
}
15+
}
16+
17+
export function callInitFunctions(functions: (() => any)[]): InitPerformanceTracer | null {
18+
// Start performance trace by accessing a URL by "https://localhost/?_ui_performance_trace=1" or "https://localhost/?key=value&_ui_performance_trace=1"
19+
// It is a quick check, no side effect so no need to do slow URL parsing.
20+
const traceInitPerformance = !window.location.search.includes('_ui_performance_trace=1') ? null : new InitPerformanceTracer();
21+
if (traceInitPerformance) {
22+
for (const func of functions) traceInitPerformance.recordCall(func.name, func);
23+
} else {
24+
for (const func of functions) func();
25+
}
26+
return traceInitPerformance;
27+
}

web_src/js/modules/observer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {isDocumentFragmentOrElementNode} from '../utils/dom.ts';
22
import type {Promisable} from 'type-fest';
3+
import type {InitPerformanceTracer} from './init.ts';
34

45
let globalSelectorObserverInited = false;
56

@@ -60,7 +61,7 @@ function attachGlobalEvents() {
6061
});
6162
}
6263

63-
export function initGlobalSelectorObserver(traceInitPerformance: {recordCall: (name: string, func: ()=>void) => void}): void {
64+
export function initGlobalSelectorObserver(traceInitPerformance?: InitPerformanceTracer): void {
6465
if (globalSelectorObserverInited) throw new Error('initGlobalSelectorObserver() already called');
6566
globalSelectorObserverInited = true;
6667

0 commit comments

Comments
 (0)