Deduplicate m.route and m.redraw logic (#2453)

- Remove appropriate route change subcriptions when a root is removed
  via `m.mount(root, null)`.
- Don't pollute `onpopstate` and friends - use standard event listeners
  instead.
- Simplify and streamline subscriptions, in preparation of adding a
  `remove` parameter to `m.mount`.
- Change the redraw internals to redraw immediately, with ability to
  cancel via returning a sentinel.
- Change `"bleeding-edge"` for `m.version` in `next` to instead just be
  the latest `m.version`. (If you're using `next`, you should know what
  you're in for.)
- Update tests to be aware of these changes. (Some were failing for
  subtle reasons.)
- Drive-by: remove some uses of `string.charAt(n)` and use `string[n]`
  instead.
This commit is contained in:
Isiah Meadows 2019-07-05 18:52:06 -04:00 committed by GitHub
parent 6c562d2b9b
commit 90bcff0fa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 397 additions and 192 deletions

View file

@ -11,6 +11,10 @@ o.spec("Router.defineRoutes", function() {
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
var $window, router, onRouteChange, onFail
function defineRoutes(routes, defaultRoute) {
router.defineRoutes(routes, onRouteChange, onFail, defaultRoute, function() {})
}
o.beforeEach(function() {
$window = pushStateMock(env)
router = new Router($window)
@ -21,7 +25,10 @@ o.spec("Router.defineRoutes", function() {
o("calls onRouteChange on init", function(done) {
$window.location.href = prefix + "/a"
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail)
var subscribe = o.spy()
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail, null, subscribe)
o(subscribe.callCount).equals(1)
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -32,7 +39,7 @@ o.spec("Router.defineRoutes", function() {
o("resolves to route", function(done) {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -45,7 +52,7 @@ o.spec("Router.defineRoutes", function() {
o("resolves to route w/ escaped unicode", function(done) {
$window.location.href = prefix + "/%C3%B6?%C3%B6=%C3%B6"
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
defineRoutes({"/ö": {data: 2}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -58,7 +65,7 @@ o.spec("Router.defineRoutes", function() {
o("resolves to route w/ unicode", function(done) {
$window.location.href = prefix + "/ö?ö=ö"
router.defineRoutes({"/ö": {data: 2}}, onRouteChange, onFail)
defineRoutes({"/ö": {data: 2}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -75,7 +82,7 @@ o.spec("Router.defineRoutes", function() {
router = new Router($window)
router.prefix = prefix
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -88,7 +95,7 @@ o.spec("Router.defineRoutes", function() {
o("handles parameterized route", function(done) {
$window.location.href = prefix + "/test/x"
router.defineRoutes({"/test/:a": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test/:a": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -101,7 +108,7 @@ o.spec("Router.defineRoutes", function() {
o("handles multi-parameterized route", function(done) {
$window.location.href = prefix + "/test/x/y"
router.defineRoutes({"/test/:a/:b": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test/:a/:b": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -114,7 +121,7 @@ o.spec("Router.defineRoutes", function() {
o("handles rest parameterized route", function(done) {
$window.location.href = prefix + "/test/x/y"
router.defineRoutes({"/test/:a...": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test/:a...": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -127,7 +134,7 @@ o.spec("Router.defineRoutes", function() {
o("handles route with search", function(done) {
$window.location.href = prefix + "/test?a=b&c=d"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/test": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -140,7 +147,7 @@ o.spec("Router.defineRoutes", function() {
o("calls reject", function(done) {
$window.location.href = prefix + "/test"
router.defineRoutes({"/other": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/other": {data: 1}})
callAsync(function() {
o(onFail.callCount).equals(1)
@ -152,7 +159,7 @@ o.spec("Router.defineRoutes", function() {
o("handles out of order routes", function(done) {
$window.location.href = prefix + "/z/y/x"
router.defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}}, onRouteChange, onFail)
defineRoutes({"/z/y/x": {data: 1}, "/:a...": {data: 2}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -164,7 +171,7 @@ o.spec("Router.defineRoutes", function() {
o("handles reverse out of order routes", function(done) {
$window.location.href = prefix + "/z/y/x"
router.defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}}, onRouteChange, onFail)
defineRoutes({"/:a...": {data: 2}, "/z/y/x": {data: 1}})
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -180,7 +187,7 @@ o.spec("Router.defineRoutes", function() {
routes["/:a..."] = {data: 2}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
defineRoutes(routes)
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -196,7 +203,7 @@ o.spec("Router.defineRoutes", function() {
routes["/z/y/x"] = {data: 1}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
defineRoutes(routes)
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -211,7 +218,7 @@ o.spec("Router.defineRoutes", function() {
routes["/:a..."] = {data: 2}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
defineRoutes(routes)
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -226,7 +233,7 @@ o.spec("Router.defineRoutes", function() {
routes["/z/y/x"] = {data: 12}
$window.location.href = prefix + "/z/y/x"
router.defineRoutes(routes, onRouteChange, onFail)
defineRoutes(routes)
callAsync(function() {
o(onRouteChange.callCount).equals(1)
@ -238,7 +245,7 @@ o.spec("Router.defineRoutes", function() {
o("handles non-ascii routes", function(done) {
$window.location.href = prefix + "/ö"
router.defineRoutes({"/ö": "aaa"}, onRouteChange, onFail)
defineRoutes({"/ö": "aaa"})
callAsync(function() {
o(onRouteChange.callCount).equals(1)