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

feat: smart diffing #40

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft

feat: smart diffing #40

wants to merge 18 commits into from

Conversation

ayoayco
Copy link
Owner

@ayoayco ayoayco commented Dec 24, 2023

fixes #24

This implements diffing by

  1. creating a "watch list" of nodes
  2. checks elements changed by comparing previous watch list to new watch list
  3. applies changes to changed nodes only

Left todo

  • only include dynamic nodes in the watch list
  • handle "add" new nodes scenario (using todo app example)

Copy link

vercel bot commented Dec 24, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
web-component-base ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 26, 2023 11:39pm

import { createElement, handleProp } from "./utils/index.js";

export function render(template, host) {
if (!host.__prev_watch_list__) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can combine this check in line 15 🤔

? (f(), (t = 2))
: (u += e)),
3 === t && "!--" === u && ((t = 4), (o = o[0]));
var n = function (t, s, r, e) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if htm/mini is okay to bring back

this.onChanges({ property, previousValue, currentValue });
}
}

#handleUpdateProp(key, stringifiedValue) {
const restored = deserialize(stringifiedValue, this.#typeMap[key]);
if (restored !== this.props[key]) this.props[key] = restored;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary

@@ -111,15 +111,17 @@ export class WebComponent extends HTMLElement {
this[camelCaps] = this[property];

this.#handleUpdateProp(camelCaps, this[property]);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary

const newNode =
change.type === "replace" ? change.node?.parentNode : change.node;

if (newNode instanceof Node) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is child is in parent 🤔

@@ -1,44 +1,82 @@
import { serialize } from "./serialize.mjs";
export function createElement(tree) {
export function createElement(tree, watchList = []) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add JSDoc

@@ -1,44 +1,82 @@
import { serialize } from "./serialize.mjs";
export function createElement(tree) {
export function createElement(tree, watchList = []) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Require watchList and remove default


function h(type, props, ...children) {
return { type, props, children };
const r = { type, props, children };
return r;
}

/**
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark dynamic DOM (lookout for tree === false)

Comment on lines +71 to +74
} else if (prop in el) {
el[prop] = value;
} else if (domProp in el) {
el[domProp] = value;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if setAttribute is enough. Look out for

  • class & className
  • for
  • onclick & onClick

"include": [
"src/*",
"src/utils/*",
"src/render.js",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: smart diffing
1 participant