Created
September 16, 2017 10:48
-
-
Save goto-bus-stop/b2ee669b5dec0cd38e62bbcfdf9aa715 to your computer and use it in GitHub Desktop.
choo-async-route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var html = require('bel') | |
var log = require('nanologger')('async-route') | |
function defaultLoading () { | |
return html`<div>Loading ...</div>` | |
} | |
module.exports = function asyncRoute (opts) { | |
var load = opts.load | |
var loading = opts.loading || defaultLoading | |
var promise | |
var cache | |
function onload (route) { | |
cache = route | |
} | |
return function (state, emit) { | |
if (cache) { | |
return cache(state, emit) | |
} | |
var route = state.route | |
if (!promise) { | |
log.info('loading', route) | |
promise = Promise.resolve(load()) | |
.then(onload) | |
.then(function () { | |
log.info('loaded', route) | |
emit('render') | |
}) | |
} else { | |
log.debug('still loading', route) | |
} | |
return loading(state, emit) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "choo-async-route", | |
"description": "dynamically load choo routes on use", | |
"version": "0.0.0", | |
"license": "MIT", | |
"dependencies": { | |
"bel": "^5.1.1", | |
"nanologger": "^1.3.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment