Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active April 11, 2020 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/fdeb95b5a7659eee93e77d793e08c8a9 to your computer and use it in GitHub Desktop.
Save myobie/fdeb95b5a7659eee93e77d793e08c8a9 to your computer and use it in GitHub Desktop.
Prototyping the dream code for a new frontend framework
import { createStorage } from './bassics/storage'
import { html, until, render } from './bassics/html'
const storage = createStorage()
const likes = storage.state('likes', fetchLikes())
function reloadLikes () {
return likes.update(fetchLikes())
}
function fetchLikes () {
return new Promise((resolve, reject) => {
// fake server call
setTimeout(() => {
resolve(Math.floor(Math.random() * 1000))
}, 3000)
})
}
const likesTemplate = async () => {
const amount = await likes.value
return html`
<p>${amount} likes</p>
`
}
const template = (props) => html`
<div>
${until(
likesTemplate,
html`<p>Loading likes...</p>`
)}
<a href="#" @click=${() => { reloadLikes() }}>Reload</a>
</div>
`
const el = document.body
render(template, el)
storage.every(() => {
// rerender every change
render(template, el)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment