Skip to content

Commit

Permalink
clean up is same node
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Apr 30, 2018
1 parent 52e3cc1 commit 2291fb0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/js/abstract/base-component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nanomorph from './component-morph';
import { isSameNodeOnce, clearIsSameNode } from './is-same-node-once';
import getAttribute from '../get-attribute';
import toProp from '../to-prop';
import { publish, subscribe } from '../pubsub';
Expand Down Expand Up @@ -244,8 +245,6 @@ export default class BaseComponent extends HTMLElement {

while (this.firstChild) {
lightDOMRefs.push(this.firstChild);
// Another piece of code is managing that part of the DOM tree.
isSameNodeOnce(this.firstChild);
childrenFragment.appendChild(this.firstChild);
}

Expand Down Expand Up @@ -303,6 +302,7 @@ export default class BaseComponent extends HTMLElement {

this._isMorphing = true;
nanomorph(this, wcClone);
clearIsSameNode();
this._isMorphing = false;
}
} catch (err) {
Expand Down Expand Up @@ -497,18 +497,3 @@ export default class BaseComponent extends HTMLElement {
});
}
}

/**
* Make sure that another piece of code is/can managing that part of the DOM tree.
*
* @link https://github.com/choojs/nanomorph#caching-dom-elements
* @param node
*/
// @todo: ideally this code is only attached during morphing phase
function isSameNodeOnce(node) {
node.isSameNode = isSameNodeStopMorph;

function isSameNodeStopMorph() {
return true;
}
}
31 changes: 31 additions & 0 deletions src/js/abstract/is-same-node-once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let sameNodeCache = [];

/**
* Make sure that another piece of code is/can managing that part of the DOM tree.
*
* @link https://github.com/choojs/nanomorph#caching-dom-elements
* @param node
*/
export function isSameNodeOnce(node) {
node.isSameNode = isSameNodeStopMorph;

sameNodeCache.push(node);

function isSameNodeStopMorph() {
return true;
}
}

/**
* Make sure to clear overwritten `isSameNode` API after DOM diffing.
*/
export function clearIsSameNode() {
let node;

// eslint-disable-next-line no-cond-assign
while (node = sameNodeCache.pop()) {
delete node.isSameNode;
}

sameNodeCache = [];
}

0 comments on commit 2291fb0

Please sign in to comment.