Skip to content

Commit

Permalink
optimize: only one match observer
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBLT committed Jan 11, 2024
1 parent 3032a26 commit 331bddc
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/@hec.js/ui/lib/src/plugins/data-match.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { route } from "../routing.js";

/** @type { WeakMap<Node, () => void> } */
const nodeUpdates = new WeakMap();

const observer = new MutationObserver((records) => {

for (const record of records) {
if (nodeUpdates.has(record.target)) {
nodeUpdates.get(record.target)();
}
}
});


/** @type { import("../plugins.js").Plugin } */
export const dataMatchPlugin = {
select: '[data-match]',
Expand All @@ -25,7 +38,9 @@ export const dataMatchPlugin = {

route.subscribe({ next: update });

new MutationObserver(update).observe(node, {
nodeUpdates.set(node, update);

observer.observe(node, {
attributes: true,
attributeFilter: ['href', 'data-route']
});
Expand Down

0 comments on commit 331bddc

Please sign in to comment.