create stores lazily (#638)

This commit is contained in:
Yoshua Wuyts
2018-03-07 13:20:47 +01:00
committed by GitHub
parent 4251a14015
commit f20deb69e7
+18 -5
View File
@@ -40,6 +40,7 @@ function Choo (opts) {
this._hasWindow = typeof window !== 'undefined'
this._createLocation = nanolocation
this._loaded = false
this._stores = []
this._tree = null
// properties that are part of the API
@@ -74,11 +75,14 @@ Choo.prototype.route = function (route, handler) {
Choo.prototype.use = function (cb) {
assert.equal(typeof cb, 'function', 'choo.use: cb should be type function')
var msg = 'choo.use'
msg = cb.storeName ? msg + '(' + cb.storeName + ')' : msg
var endTiming = nanotiming(msg)
cb(this.state, this.emitter, this)
endTiming()
var self = this
this._stores.push(function () {
var msg = 'choo.use'
msg = cb.storeName ? msg + '(' + cb.storeName + ')' : msg
var endTiming = nanotiming(msg)
cb(self.state, self.emitter, self)
endTiming()
})
}
Choo.prototype.start = function () {
@@ -124,6 +128,10 @@ Choo.prototype.start = function () {
}
}
this._stores.forEach(function (initStore) {
initStore()
})
this._matchRoute()
this._tree = this._prerender(this.state)
assert.ok(this._tree, 'choo.start: no valid DOM node returned for location ' + this.state.href)
@@ -192,6 +200,11 @@ Choo.prototype.toString = function (location, state) {
assert.equal(typeof location, 'string', 'choo.toString: location should be type string')
assert.equal(typeof this.state, 'object', 'choo.toString: state should be type object')
// TODO: pass custom state down to each store.
this._stores.forEach(function (initStore) {
initStore()
})
this._matchRoute(location)
var html = this._prerender(this.state)
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)