Files
buuh/examples/counter/app.js
T

41 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

// The classic choo counter. This one module is the whole app, and both
// sides consume it: the browser mounts it (import map resolves
// @uhhm/buuh-html to the DOM renderer), Node stringifies it (same specifier
// resolves to the string renderer). That's the isomorphic contract.
import choo from '@uhhm/buuh'
import html from '@uhhm/buuh-html'
export default function createApp () {
const app = choo()
app.use(countStore)
app.route('/', mainView)
// the demo gets served from arbitrary subpaths (npx serve ., test
// servers); a wildcard fallback makes it mount anywhere
app.route('*', mainView)
return app
}
function mainView (state, emit) {
emit(state.events.DOMTITLECHANGE, `count is ${state.count}`)
return html`
<body>
<h1>count is ${state.count}</h1>
<button onclick=${onclick}>Increment</button>
</body>
`
function onclick () {
emit('increment', 1)
}
}
function countStore (state, emitter) {
state.count = state.count || 0
emitter.on('increment', function (count) {
state.count += count
emitter.emit('render')
})
}