-
Notifications
You must be signed in to change notification settings - Fork 2
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
ayoayco
wants to merge
18
commits into
main
Choose a base branch
from
feat/smart-diffing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
63cbe53
feat: print only dynamic node objects
ayoayco e63bdd1
update example templating
ayoayco fba4a86
feat: implement DOM watch list
ayoayco 6e4adc9
refactor: remove console.logs
ayoayco 09d4f59
feat: replace node
ayoayco e162003
initial work for multiple found
ayoayco 9bed501
remove obsolete example
ayoayco 1ff68b0
remove obsolete example
ayoayco 6484401
update toggle example
ayoayco 668f866
update templating example
ayoayco 8c9c4af
refactor: separate render function
ayoayco 685c01f
fix: wrong import
ayoayco a27515f
fix: typo in filename
ayoayco e8feef5
initial todo app example
ayoayco 6258460
initial handling of different watch list lengths
ayoayco 16b1f02
Merge branch 'main' into feat/smart-diffing
ayoayco ac34267
Merge branch 'main' of github.com:ayoayco/web-component-base into fea…
ayoayco 83432ca
delete unused prop #prevDom
ayoayco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { html, render } from "../../src/index.js"; | ||
|
||
class VanillaTest extends HTMLElement { | ||
connectedCallback() { | ||
const el = html`<button>hello</button>`; | ||
render(el, this); | ||
} | ||
} | ||
|
||
customElements.define("vanilla-test", VanillaTest); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>TODO App</title> | ||
<script type="module" src="./todo.js"></script> | ||
</head> | ||
<body> | ||
<todo-app></todo-app> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { WebComponent, html } from "../../src/index.js"; | ||
|
||
class TodoApp extends WebComponent { | ||
#tasks = []; | ||
addTask() { | ||
const newTask = this.querySelector("#task-field")?.value; | ||
if (newTask) { | ||
this.#tasks.push(newTask); | ||
console.log(this.#tasks); | ||
// manual render because #tasks is private + unobserved | ||
this.render(); | ||
} | ||
} | ||
#existingTimer; | ||
removeTask(index, checkbox) { | ||
clearTimeout(this.#existingTimer); | ||
// users can change their mind within 1 second :) | ||
this.#existingTimer = setTimeout(() => { | ||
if (checkbox.checked) { | ||
this.#tasks.splice(index, 1); | ||
this.render(); | ||
} | ||
}, 1000); | ||
} | ||
get template() { | ||
return html` | ||
<form | ||
onSubmit=${(e) => { | ||
e.preventDefault(); | ||
this.addTask(); | ||
}} | ||
> | ||
<input placeholder="Buy milk" id="task-field" name="task-field" /> | ||
<button type="submit">Add</button> | ||
</form> | ||
<ul style="list-style:none"> | ||
${this.#tasks.map( | ||
(t, i) => html` | ||
<li> | ||
<input | ||
type="checkbox" | ||
name="task-${i}" | ||
id="task-${i}" | ||
onChange=${(e) => this.removeTask(i, e.target)} | ||
style="cursor:pointer" | ||
/> | ||
<label for="task-${i}" style="cursor:pointer">${t}</label> | ||
</li> | ||
` | ||
)} | ||
</ul> | ||
<footer>just to make it more difficult</footer> | ||
`; | ||
} | ||
} | ||
|
||
customElements.define("todo-app", TodoApp); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
* @author Ayo Ayco <https://ayo.ayco.io> | ||
*/ | ||
|
||
import { render as r } from "./render.js"; | ||
import { | ||
createElement, | ||
getKebabCase, | ||
getCamelCase, | ||
serialize, | ||
|
@@ -17,7 +17,6 @@ import { | |
*/ | ||
export class WebComponent extends HTMLElement { | ||
#host; | ||
#prevDOM; | ||
#props; | ||
#typeMap = {}; | ||
#effectsMap = {}; | ||
|
@@ -119,15 +118,17 @@ export class WebComponent extends HTMLElement { | |
this[camelCaps] = this[property]; | ||
|
||
this.#handleUpdateProp(camelCaps, this[property]); | ||
|
||
this.render(); | ||
|
||
this.onChanges({ property, previousValue, currentValue }); | ||
} | ||
} | ||
|
||
#handleUpdateProp(key, stringifiedValue) { | ||
const restored = deserialize(stringifiedValue, this.#typeMap[key]); | ||
if (restored !== this.props[key]) this.props[key] = restored; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary |
||
if (restored !== this.props[key]) { | ||
this.props[key] = restored; | ||
} | ||
} | ||
|
||
#handler(setter, meta) { | ||
|
@@ -162,13 +163,13 @@ export class WebComponent extends HTMLElement { | |
|
||
return true; | ||
}, | ||
get(obj, prop) { | ||
get(obj, prop, receiver) { | ||
// TODO: handle non-objects | ||
if (obj[prop] !== null && obj[prop] !== undefined) { | ||
Object.getPrototypeOf(obj[prop]).proxy = meta.#props; | ||
Object.getPrototypeOf(obj[prop]).proxy = receiver; | ||
Object.getPrototypeOf(obj[prop]).prop = prop; | ||
} | ||
return obj[prop]; | ||
return Reflect.get(...arguments); | ||
}, | ||
}; | ||
} | ||
|
@@ -195,20 +196,6 @@ export class WebComponent extends HTMLElement { | |
} | ||
|
||
render() { | ||
if (typeof this.template === "string") { | ||
this.innerHTML = this.template; | ||
} else if (typeof this.template === "object") { | ||
const tree = this.template; | ||
|
||
// TODO: smart diffing | ||
if (JSON.stringify(this.#prevDOM) !== JSON.stringify(tree)) { | ||
const el = createElement(tree); | ||
if (el) { | ||
if (Array.isArray(el)) this.#host.replaceChildren(...el); | ||
else this.#host.replaceChildren(el); | ||
} | ||
this.#prevDOM = tree; | ||
} | ||
} | ||
r(this.template, this.#host); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary