Skip to content

Commit

Permalink
Little refactoring to avoid circular execution when listing for dom m…
Browse files Browse the repository at this point in the history
…utations (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.
  • Loading branch information
stefanbuck authored Sep 14, 2018
1 parent 711e01a commit dea569c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/core/octo-linker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import $ from 'jquery';
import injection from 'github-injection';
import BlobReader from '@octolinker/blob-reader';
import insertLink from '@octolinker/helper-insert-link';
Expand Down Expand Up @@ -65,14 +66,14 @@ async function run(self) {
clickHandler(matches);
}

function observeExpand(self) {
const observer = new MutationObserver(() => {
run(self);
function waitFor(el, cb) {
const viewSpy = new MutationObserver(() => {
cb();
});

for (const el of document.querySelectorAll('.diff-table tbody')) {
observer.observe(el, { childList: true });
}
viewSpy.observe(el, {
childList: true,
});
}

export default class OctoLinkerCore {
Expand All @@ -82,6 +83,9 @@ export default class OctoLinkerCore {

init() {
injection(run.bind(null, this));
observeExpand(this);

$('.js-expand').on('click', event => {
waitFor($(event.target).closest('tbody')[0], run.bind(null, this));
});
}
}

0 comments on commit dea569c

Please sign in to comment.