Skip to content

Commit dea569c

Browse files
authored
Little refactoring to avoid circular execution when listing for dom mutations (OctoLinker#514)
For some reason the following PR microdevs/missy#39 causes the browser hang when trying to add a inline comment. I tested the same functionally on a few other PRs and it worked fine for all of them. However, I was able to narrow it down to the `MutationObserver` which is called over and over again. I have no idea what is different there, but I decided to rewrite the related code to only spy for dom mutations when expanding the blob.
1 parent 711e01a commit dea569c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

packages/core/octo-linker.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import $ from 'jquery';
12
import injection from 'github-injection';
23
import BlobReader from '@octolinker/blob-reader';
34
import insertLink from '@octolinker/helper-insert-link';
@@ -65,14 +66,14 @@ async function run(self) {
6566
clickHandler(matches);
6667
}
6768

68-
function observeExpand(self) {
69-
const observer = new MutationObserver(() => {
70-
run(self);
69+
function waitFor(el, cb) {
70+
const viewSpy = new MutationObserver(() => {
71+
cb();
7172
});
7273

73-
for (const el of document.querySelectorAll('.diff-table tbody')) {
74-
observer.observe(el, { childList: true });
75-
}
74+
viewSpy.observe(el, {
75+
childList: true,
76+
});
7677
}
7778

7879
export default class OctoLinkerCore {
@@ -82,6 +83,9 @@ export default class OctoLinkerCore {
8283

8384
init() {
8485
injection(run.bind(null, this));
85-
observeExpand(this);
86+
87+
$('.js-expand').on('click', event => {
88+
waitFor($(event.target).closest('tbody')[0], run.bind(null, this));
89+
});
8690
}
8791
}

0 commit comments

Comments
 (0)