Skip to content

Commit

Permalink
feat(hiccup-dom): start(), add optional spans arg
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 27, 2018
1 parent 9b2c160 commit 8a070ff
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/hiccup-dom/src/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isString } from "@thi.ng/checks/is-string";
import { diffElement, normalizeTree } from "./diff";
import { diffElement } from "./diff";
import { normalizeTree } from "./normalize";

/**
* Takes a parent DOM element (or ID) and hiccup tree
Expand All @@ -10,23 +11,25 @@ import { diffElement, normalizeTree } from "./diff";
* Important: The parent element given is assumed to have NO
* children at the time when `start()` is called. Since
* hiccup-dom does NOT track the real DOM, the resulting
* changes will result in potentially undefined behavior.
* changes will result in potentially undefined behavior
* if the parent element wasn't empty.
*
* Returns a function, which when called, immediately
* cancels the update loop.
*
* @param parent
* @param tree
* @param parent root element or ID
* @param tree hiccup DOM tree
* @param spans true (default), if text should be wrapped in `<span>`
*/
export function start(parent: Element | string, tree: any) {
export function start(parent: Element | string, tree: any, spans = true) {
let prev = [];
let isActive = true;
parent = isString(parent) ?
document.getElementById(parent) :
parent;
function update() {
if (isActive) {
diffElement(<any>parent, prev, prev = normalizeTree(tree));
diffElement(<Element>parent, prev, prev = normalizeTree(tree, [0], true, spans));
// check again in case one of the components called cancel
isActive && requestAnimationFrame(update);
}
Expand Down

0 comments on commit 8a070ff

Please sign in to comment.