Created
June 27, 2022 09:32
-
-
Save victorporof/54fd7f5b9b2f06515acc71aa3ff2f0dd to your computer and use it in GitHub Desktop.
Framework templating example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const queue = []; | |
const elements = new Map(); | |
let counter = 0; | |
export function addWork(work) { | |
const id = counter++; | |
queue.push({ ...work, id }); | |
return id; | |
} | |
export function runWork() { | |
for (const work of queue) { | |
const element = document.createElement(work.type); | |
elements.set(work.id, element); | |
if ("textContent" in work) { | |
element.textContent = work.textContent; | |
} | |
if (typeof work.appendTo === "number") { | |
elements.get(work.appendTo).appendChild(element); | |
} else { | |
work.appendTo.appendChild(element); | |
} | |
} | |
queue.length = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<script type="module" src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { addWork, runWork } from "./alina-framework.js"; | |
function createParent() { | |
return addWork({ | |
type: "div", | |
appendTo: document.body, | |
}); | |
} | |
function createChild(parent) { | |
addWork({ | |
type: "p", | |
textContent: "hello world", | |
appendTo: parent, | |
}); | |
} | |
const parent = createParent(); | |
createChild(parent); | |
runWork(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment