2026-09-08 16:56:54 +02:00
|
|
|
// The classic choo counter. This one module is the whole app, and both
|
|
|
|
|
// sides consume it: the browser mounts it (import map resolves
|
|
|
|
|
// @choojs/html to the DOM renderer), Node stringifies it (same specifier
|
|
|
|
|
// resolves to the string renderer). That's the isomorphic contract.
|
|
|
|
|
|
|
|
|
|
import choo from '@choojs/core'
|
|
|
|
|
import html from '@choojs/html'
|
|
|
|
|
|
|
|
|
|
export default function createApp () {
|
|
|
|
|
const app = choo()
|
|
|
|
|
app.use(countStore)
|
|
|
|
|
app.route('/', mainView)
|
2026-09-08 17:57:27 +02:00
|
|
|
// the demo gets served from arbitrary subpaths (npx serve ., test
|
|
|
|
|
// servers); a wildcard fallback makes it mount anywhere
|
|
|
|
|
app.route('*', mainView)
|
2026-09-08 16:56:54 +02:00
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mainView (state, emit) {
|
|
|
|
|
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')
|
|
|
|
|
})
|
|
|
|
|
}
|