Skip to content

Instantly share code, notes, and snippets.

@akiva
Last active July 26, 2017 07:12
Show Gist options
  • Save akiva/fed503aed3a76be6bbf1dada30f9018b to your computer and use it in GitHub Desktop.
Save akiva/fed503aed3a76be6bbf1dada30f9018b to your computer and use it in GitHub Desktop.
const app = require('choo')()
const html = require('choo/html')
app.use(function (state, emitter) {
state.loggedIn = false
emitter.prependListener('DOMContentLoaded', function () {
if (state.route !== '/shazam' && !state.loggedIn) {
emitter.emit('pushState', '/shazam')
emitter.emit('render')
}
})
emitter.prependListener('navigate', function () {
console.log('state.route:', state.route, window.location.pathname)
state.route = window.location.pathname
emitter.once('render', function () {
console.log('state.route:', state.route, window.location.pathname)
})
})
emitter.on('navigate', function () {
console.log('Post-navigate event route', state.route)
})
})
app.route('/', function (state, emit) {
return html`
<body>
<p>Foo bar baz quux</p>
</body>
`
})
app.route('/shazam', function (state, emit) {
return html`
<body>
<p>Shazam</p>
</body>
`
})
if (module.parent) module.exports = app
else app.mount('body')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment