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 TEXT_ELEMENT = "TEXT"; | |
/** | |
* @param {string} type - the node type | |
* @param {?object} configObject - the props | |
* @param {?...any} args - the children array | |
* @returns {object} - to be called by tevreact.render | |
*/ | |
export function createElement(type, configObject, ...args) { | |
const props = Object.assign({}, configObject); |
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 { updateDomProperties } from "./dom-utils"; | |
import { TEXT_ELEMENT } from "./element"; | |
/** | |
* | |
* @param {object} element - vdom representation | |
* @param {HTMLElement} parentDom - element where children will be appended | |
*/ | |
export function render(element, parentDom) { | |
const { type, props } = element; |
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 { TEXT_ELEMENT } from "./element"; | |
/** | |
* @param {HTMLElement} dom - the html element where props get applied to | |
* @param {object} props - consists of both attributes and event listeners. | |
*/ | |
export function updateDomProperties(dom, props) { | |
const isListener = name => name.startsWith("on"); | |
Object.keys(props) | |
.filter(isListener) |
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"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Didact</title> | |
</head> | |
<body> | |
<div id="app"></div> |
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 { render } from "./reconciler"; | |
import { createElement } from "./element"; | |
export { createElement, render }; | |
export default { | |
render, | |
createElement | |
}; |
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
<script type="text/jsx"> | |
/** @jsx tevreact.createElement */ | |
/** In the comment above we are telling babel which function it should | |
use the default is React.createElement and we want to use | |
our own createElement function*/ | |
const appElement = ( | |
<div> | |
<h1>Hello Tev, Have you watched John Wick</h1> | |
</div> | |
) |
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 instance = { | |
dom: HTMLElement, // the rendered dom element | |
element: {type: String, props: object}, | |
childInstances: Array<instance> // array of child instances | |
} |
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 { updateDomProperties } from "./dom-utils"; | |
import { TEXT_ELEMENT } from "./element"; | |
let rootInstance = null; // will keep the reference to the instance rendered on the dom | |
export function render(element, parentDom) { | |
const prevInstance = rootInstance; | |
const nextInstance = reconcile(parentDom, prevInstance, element); | |
rootInstance = nextInstance; | |
} |
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
export function updateDomProperties(dom, prevProps, nextProps) { | |
const isEvent = name => name.startsWith("on"); | |
const isAttribute = name => !isEvent(name) && name != "children"; | |
// Remove event listeners | |
Object.keys(prevProps) | |
.filter(isEvent) | |
.forEach(name => { | |
const eventType = name.toLowerCase().substring(2); | |
dom.removeEventListener(eventType, prevProps[name]); |
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
function reconcile(parentDom, instance, element) { | |
/** code... */ | |
else if (instance.element.type === element.type) { | |
// perform props update here | |
updateDomProperties(instance.dom, instance.element.props, element.props); | |
instance.childInstances = reconcileChildren(instance, element); | |
instance.element = element; | |
return instance; | |
} | |
/** code... */ |