Skip to content

Commit a6ec32b

Browse files
committed
fix(pat navigation): Improve the performance when the navigation elements change.
Debounce the mutation observer callback which initialized the markings for 100ms for better performance.
1 parent 0b099cc commit a6ec32b

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/pat/navigation/navigation.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Base from "../../core/base";
22
import Parser from "../../core/parser";
33
import logging from "../../core/logging";
44
import events from "../../core/events";
5+
import utils from "../../core/utils";
56

67
const log = logging.getLogger("navigation");
78

@@ -18,14 +19,7 @@ export default Base.extend({
1819
this.options = parser.parse(this.el, this.options);
1920

2021
this.init_listeners();
21-
22-
if (this.el.querySelector(this.options.currentClass)) {
23-
log.debug("Mark navigation items based on existing current class");
24-
this.mark_current();
25-
} else {
26-
log.debug("Mark navigation items based on URL pattern.");
27-
this.mark_items_url();
28-
}
22+
this.init_markings();
2923
},
3024

3125
/**
@@ -68,15 +62,13 @@ export default Base.extend({
6862
this.el.querySelector(`a.${current}, .${current} a`)?.click();
6963
}
7064

65+
const debounced_init_markings = utils.debounce(
66+
this.init_markings.bind(this),
67+
100
68+
);
7169
// Re-init when navigation changes.
7270
const observer = new MutationObserver(() => {
73-
if (this.el.querySelector(this.options.currentClass)) {
74-
log.debug("Mark navigation items based on existing current class");
75-
this.mark_current();
76-
} else {
77-
log.debug("Mark navigation items based on URL pattern.");
78-
this.mark_items_url();
79-
}
71+
debounced_init_markings();
8072
});
8173
observer.observe(this.el, {
8274
childList: true,
@@ -86,6 +78,19 @@ export default Base.extend({
8678
});
8779
},
8880

81+
/**
82+
* Initial run to mark the current item and its parents.
83+
*/
84+
init_markings() {
85+
if (this.el.querySelector(this.options.currentClass)) {
86+
log.debug("Mark navigation items based on existing current class");
87+
this.mark_current();
88+
} else {
89+
log.debug("Mark navigation items based on URL pattern.");
90+
this.mark_items_url();
91+
}
92+
},
93+
8994
/**
9095
* Get a matching parent or stop at stop_el.
9196
*

src/pat/navigation/navigation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe("Navigation pattern tests - no predefined structure", function () {
183183
const load_nav = document.querySelector(".load-nav");
184184
load_nav.click();
185185

186-
await utils.timeout(1); // wait for MutationObserver
186+
await utils.timeout(120); // wait for MutationObserver
187187

188188
const w1 = nav.querySelector(".w1");
189189
const a1 = nav.querySelector(".a1");

0 commit comments

Comments
 (0)