Comments (7)
Hmm let me give it some more thought. Initially it feels like this should be in the diffing library or yo-yo
as bel
doesn't know about previous elements.
from nanohtml.
Yeah, doesn't thunking only become relevant when you're executing the element creation over and over again (a la morphdom)?
from nanohtml.
@timwis yeah you're right, but this type of rendering is quickly becoming the default; e.g.
- react
- yo-yo, choo
- ember
- mithril, mercury, etc.
Probably the only hot new one around that doesn't do virtual-dom
diffing would be Angular 2, but that's about it.
from nanohtml.
Just an update that I've been experimenting with ideas but nothing really to show for yet. But wanted to mention using a feature like this for handling <canvas>
updates.
Currently <canvas>
tags don't work well in the idiomatic flow because the tag never indicates itself as changed. The simple fix would be to always assume a <canvas>
has changed when morphing, replacing with the new one.
But it would interesting if we let the element author choose when to invalidate. Basically the thunk just remembers the last passed arguments/element
and appends to the function. Then any element not marked or first time through the thunk, gets updated. Any previous that are marked will always be ignored:
const html = require('yo-yo')
const thunk = require('yo-yo/thunk')
/* prev arg gets added by thunk to the end of the function call */
module.exports = thunk(function createCanvas (data, prev) {
let canvas = prev.element
if (prev.args[0].width !== data.width || prev.args[0].height !== data.height) {
canvas = html`<canvas width="${data.width}" height="${data.height}" />`
}
const ctx = canvas.getContext('2d')
ctx.fillRect(data.x, data.y, data.width, data.height)
return canvas
})
/* ... */
let data = {x:0,y:0,width:100,height:100}
raf(function loop () {
data.x += .1
if (data.x > 100) data.width *= 2
yo.update(root, canvas(data))
raf(loop)
})
This might simplify the validation check too as we wouldn't need to traverse objects and arrays checking if the arguments have changed. We just let the element author choose the parameters that decide that. Which most of the time would be 1 or 2 if statements.
Or we could provide that API for convenience to the author to apply as needed:
const html = require('yo-yo')
const thunk = require('yo-yo/thunk')
module.exports = thunk(function createMyButton (data, prev) {
let button = prev.element
if (!button || prev.hasChanged(data)) {
button = html`<button>${data.label}</button>`
}
return button
})
from nanohtml.
That looks awesome @shama! Thanks for the writeup. prev
being an object with element
and args
properties (rather than just the previous version of data
) was confusing at first but I don't have a better idea. Maybe if it were called cache
or something...
If I recall correctly, the way react (or at least redux) handles this is by "mapping state to props" when connecting the store to the component, so that react knows whether to update that specific component on a given state change. Right?
from nanohtml.
@yoshuawuyts https://github.com/yoshuawuyts/cache-element/ can be used to thunk
element, or am I confused?
from nanohtml.
from nanohtml.
Related Issues (20)
- `default` alias not defined on server.js HOT 4
- onclick Event Handlers with nanohtml/raw HOT 1
- Support custom elements V1 extension of built-in elements
- Cannot find module 'estraverse' HOT 5
- browserify transform sourcemaps only working partially HOT 1
- SyntaxError: Octal escape sequences are not allowed in template strings
- The qualified name provided ('clipPath"') contains the invalid character '"'. HOT 1
- Support array for multiple roots?
- Svgs elements not created properly HOT 2
- Browserify transform does not work for code transpiled from Typescript HOT 1
- Custom DOM library support on Node.js (e.g. JSDOM) HOT 3
- Text node as a root ignores the following element
- Way to keep whitespaces intact? HOT 2
- Valueless attributes gets unexpected default value
- Unclosed tags insert unexpected text node
- Support for conditional attributes? HOT 6
- bundle.min.js in npm package doesn't add html to window object HOT 1
- autofocus on input HOT 2
- babel + rollup import error HOT 4
- Question on upgrading from [email protected] HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nanohtml.