Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement use state #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createElement, render, useRenderCounter } from "./reakt";
import { createElement, render, useTick } from "./reakt";

declare namespace JSX {
interface IntrinsicElements {
Expand All @@ -11,18 +11,20 @@ let update = (ev: any) => {
};

function Test({ name, color }: { name: string; color?: string }) {
const renderCount = useRenderCounter();
const tick = useTick(2000);

return (
<div style={`color: ${color}`}>
<p>
Hello <strong>{name}</strong>
<input type="text" onkeydown={update} /> (rendered {renderCount} times)
Hello <strong>{name}</strong> {tick}
<input type="text" onkeydown={update} />
</p>
</div>
);
}

function App({ tick }: { tick: number }) {
function App() {
const tick = useTick(1000);
return (
<div>
<p>{tick}</p>
Expand All @@ -33,8 +35,8 @@ function App({ tick }: { tick: number }) {
);
}

setInterval(() => {
let component = <App tick={Math.floor(Date.now() / 1000)} />;
const root = document.querySelector("#app")!;
render(component, root);
}, 1000);
// setInterval(() => {
let component = <App />;
const root = document.querySelector("#app")!;
render(component, root);
// }, 2000);
40 changes: 20 additions & 20 deletions src/reakt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ type ShadowElement = ShadowPrimitiveElement | ShadowUserElement;

let currentElement: ShadowUserElement | null = null;
function getComponentOutput(node: ShadowUserElement): ShadowNode {
if (node.output) {
return node.output;
} else {
currentElement = node;
const output = node.type(node.props);
currentElement = null;
node.output = output;
return output;
}
currentElement = node;
const output = node.type(node.props);
currentElement = null;
node.output = output;
return output;
}

export type ShadowNode =
Expand Down Expand Up @@ -76,13 +72,18 @@ export function createElement<P>(
}
}

export function useRenderCounter() {
export function useTick(delay: number) {
if (typeof currentElement!.stateSlot === "undefined") {
// newly mounted component
currentElement!.stateSlot = 0;
currentElement!.stateSlot = { value: 0 };
let elem = currentElement!;
setInterval(() => {
((elem.stateSlot as any).value as number)++;
render(prevShadow, currentRoot);
}, delay);
}
(currentElement!.stateSlot as number)++;
return currentElement!.stateSlot;

return (currentElement!.stateSlot as any).value;
}

export type Patch =
Expand All @@ -101,12 +102,7 @@ export function reconcile(
prev: ShadowNode,
next: ShadowNode
): Patch | undefined {
if (prev === next) {
// This case is an optimization. We know ShadowNode are immutable
// so if they objects are identical, we don't need to check
// children at all. This is useful if a component is memoized.
return { action: "noop" };
} else if (isNil(prev) && isNil(next)) {
if (isNil(prev) && isNil(next)) {
return undefined;
} else if (isNil(prev)) {
return { action: "insert", element: next };
Expand Down Expand Up @@ -165,7 +161,9 @@ export function reconcile(
prev.type == next.type
) {
next.stateSlot = prev.stateSlot;
return reconcile(getComponentOutput(prev), getComponentOutput(next));
// The previous element must have been rendered already, so we
// take the output.
return reconcile(prev.output, getComponentOutput(next));
} else {
return { action: "replace", element: next };
}
Expand Down Expand Up @@ -236,7 +234,9 @@ function applyPatchInto(patch: Patch, parent: Node, target?: Node): void {
}

let prevShadow: ShadowNode;
let currentRoot: Node;
export function render(elem: ShadowNode, root: Node) {
currentRoot = root;
const diff = reconcile(prevShadow, elem);
prevShadow = elem;
if (diff) {
Expand Down