Skip to content

Updated Router Functionality#1

Merged
kwalker3690 merged 8 commits into
masterfrom
kelly/return-early-change-route
Nov 22, 2016
Merged

Updated Router Functionality#1
kwalker3690 merged 8 commits into
masterfrom
kelly/return-early-change-route

Conversation

@kwalker3690
Copy link
Copy Markdown

  • Updating router to be compatible with what the Optimizely app expects from a router api
  • Stop running handler functions if route path changes before handler functions are complete
  • Added support for onRouteStart and onRouteComplete functions, to enable route duration tracking
  • Updated node modules to get tests to run properly

@jordangarcia can you take a look at these changes?

@kwalker3690 kwalker3690 changed the title Kelly/return early change route Updated Router Functionality Nov 14, 2016
@kwalker3690 kwalker3690 force-pushed the kelly/return-early-change-route branch from 608abee to 657746f Compare November 14, 2016 21:28
Comment thread src/Router.js Outdated
@@ -85,62 +70,119 @@ export default class Router {
throw new Error('Router#registerRoutes must be passed an array of Routes')
}
routes.map(route => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's kind of silly this was a map before, probably more semantic to be a forEach

Comment thread src/Router.js
if (this.onRouteComplete) {
const handlerLength = route.handlers.length-1;
const lastHandler = route.handlers[handlerLength];
const routingEnd = (ctx, next) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was super hacky before because page.js expects the last function to not have a next() function.

Instead it'd be nicer to push the config.onRouteComplete function if it is present and not have to hackily slice up the array of handler functions

Comment thread src/Router.js
this.__routes = []
this.__catchallPath = null;
this.__routes = [];
WindowEnv.removeEventListener('popstate', this.__onpopstate)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if reset should reset the route start and route end functions. what do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it definitely should, good call

Comment thread src/Router.js Outdated
let i = 0;

let next = () => {
if (this.__currentCanonicalPath !== ctx.canonicalPath) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm i'm a little concerned over this mechanism to escape the route.

what happens if you click a new route, then click back to the same route. I suppose it's synchronous so it will probably work.

One idea I had was to just keep track of a number / id that way any new dispatch will abondon the old dispatch instead of trying to use the canonical path as the identifier.

Ex

constructor() {
  this.dispatchId = 1;
}

__dispatch() {
  this.dispatchId++;
  if (ctx.dispatchId !== this.dispatchId) { 
    return
  }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i like that a lot, i tried a similar approach but wound up keeping track of more state than was necessary, this is super clean

@kwalker3690 kwalker3690 force-pushed the kelly/return-early-change-route branch from 657746f to be27015 Compare November 15, 2016 18:13
@kwalker3690 kwalker3690 force-pushed the kelly/return-early-change-route branch from be27015 to ce739c7 Compare November 15, 2016 18:14
Copy link
Copy Markdown

@jordangarcia jordangarcia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to have the new functionality tested here, but i wont block on that.

LGTM

Comment thread src/Router.js
this.__routes.push(new Route(route))
routes.forEach(route => {
route = new Route(route);
if (this.onRouteComplete) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tested?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests now

@kwalker3690 kwalker3690 merged commit 76cc927 into master Nov 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants