[router] Fix hopefully the last race condition

This commit is contained in:
Pierre-Yves Gerardy 2016-12-08 21:44:21 +01:00
parent 605fa44711
commit 703aab7905
2 changed files with 13 additions and 13 deletions

View file

@ -8,14 +8,9 @@ module.exports = function($window, redrawService) {
var routeService = coreRouter($window) var routeService = coreRouter($window)
var identity = function(v) {return v} var identity = function(v) {return v}
var render, component, attrs, currentPath, updatePending = false var render, component, attrs, currentPath, lastUpdate
var route = function(root, defaultRoute, routes) { var route = function(root, defaultRoute, routes) {
if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined") if (root == null) throw new Error("Ensure the DOM element that was passed to `m.route` is not undefined")
var update = function(routeResolver, comp, params, path) {
component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, updatePending = false
render = (routeResolver.render || identity).bind(routeResolver)
run()
}
var run = function() { var run = function() {
if (render != null) redrawService.render(root, render(Vnode(component, attrs.key, attrs))) if (render != null) redrawService.render(root, render(Vnode(component, attrs.key, attrs)))
} }
@ -23,22 +18,27 @@ module.exports = function($window, redrawService) {
routeService.setPath(defaultRoute) routeService.setPath(defaultRoute)
} }
routeService.defineRoutes(routes, function(payload, params, path) { routeService.defineRoutes(routes, function(payload, params, path) {
if (payload.view) update({}, payload, params, path) var update = lastUpdate = function(routeResolver, comp) {
if (update !== lastUpdate) return
component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, lastUpdate = null
render = (routeResolver.render || identity).bind(routeResolver)
run()
}
if (payload.view) update({}, payload)
else { else {
if (payload.onmatch) { if (payload.onmatch) {
updatePending = true
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) { Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {
if (updatePending) update(payload, resolved, params, path) update(payload, resolved)
}, bail) }, bail)
} }
else update(payload, "div", params, path) else update(payload, "div")
} }
}, bail) }, bail)
redrawService.subscribe(root, run) redrawService.subscribe(root, run)
} }
route.set = function(path, data, options) { route.set = function(path, data, options) {
if (updatePending) options = {replace: true} if (lastUpdate != null) options = {replace: true}
updatePending = false lastUpdate = null
routeService.setPath(path, data, options) routeService.setPath(path, data, options)
} }
route.get = function() {return currentPath} route.get = function() {return currentPath}

View file

@ -932,7 +932,7 @@ o.spec("route", function() {
}) })
}) })
o.only("when two async routes are racing, the last one set cancels the finalization of the first", function(done) { o("when two async routes are racing, the last one set cancels the finalization of the first", function(done) {
var renderA = o.spy() var renderA = o.spy()
var renderB = o.spy() var renderB = o.spy()
var onmatchA = o.spy(function(){ var onmatchA = o.spy(function(){