Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .match(route) to get a matching route without invoking the handler #64

Merged
merged 1 commit into from
Dec 28, 2017
Merged

Add .match(route) to get a matching route without invoking the handler #64

merged 1 commit into from
Dec 28, 2017

Conversation

marcbachmann
Copy link
Collaborator

@marcbachmann marcbachmann commented Dec 28, 2017

Let's begin at the bottom 😁
(see choojs/choo#613 and choojs/nanorouter#9 for more details)

This PR exposes a new .match method on the router which can be used to return a matching route without invoking it directly.

I added that description to the readme:


matchedRoute = router.match(route)

Matches a route and returns an object. The returned object contains the properties {cb, params, route}. This method does not invoke the callback of a route. If no route matches, the default route will be returned. If no default route matches, an error will be thrown.


Now that .emit calls .match, I'm not sure how we should handle the tests.
Maybe those two tests are good enough for you.

If the trie module would be private and not documented, I'd also remove the xtend when returning the node as we're creating a new object anyways.

@coveralls
Copy link

coveralls commented Dec 28, 2017

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling df03e25 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

coveralls commented Dec 28, 2017

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 63cc334 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 6860a24 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 6860a24 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 6860a24 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 734a547 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 734a547 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 97.368% when pulling 734a547 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@marcbachmann
Copy link
Collaborator Author

Coveralls, you're drunk. You better also go to sleep. 👋

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

This PR is pretty rad! - just a small comment to prevent some GC action, but otherwise this is ace!

I was thinking we should perhaps also create an issue to rewrite this module to use prototypes, and then only expose the .match() function. That way we can shave some bytes from our bundle.

index.js Outdated

throw new Error("route '" + route + "' did not match")
}

function nodeToObject (matched) {
return {cb: matched.cb, route: matched.cb.route, params: matched.params}
Copy link
Member

Choose a reason for hiding this comment

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

heya, could we perhaps statically allocate the object ahead of time, and then reuse it on later runs? The router here might be called up to 60 times a second (sure hope that it's optimized tho, haha) - but that might be problematic later down the line.

so something along the lines of:

var obj = {}

function nodeToObject (matched) {
  obj.cb = matched.cb,
  obj.route = matched.cb.route,
  obj.params = matched.params
  return obj
}

Copy link
Collaborator Author

@marcbachmann marcbachmann Dec 28, 2017

Choose a reason for hiding this comment

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

The object creation isn't an issue at all. Chrome supports millions of operations per second. Stuff like iterating through keys and assigning them like it's done in the trie file is much slower. So I'd rather remove that one https://github.com/yoshuawuyts/wayfarer/blob/master/trie.js#L99

I don't like modifying an existing object. That will cause issues. Especially because I'd also like to pass that object around and still access the correct values in a next tick.

As alternative, thi should be much faster.

function Node (matched) {
  this.cb = matched.cb,
  this.route = matched.cb.route,
  this.params = matched.params
}

Copy link
Member

Choose a reason for hiding this comment

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

Alright, yeah I'd be cool with that!

@coveralls
Copy link

coveralls commented Dec 28, 2017

Coverage Status

Coverage increased (+0.05%) to 97.403% when pulling 84b7a99 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@coveralls
Copy link

coveralls commented Dec 28, 2017

Coverage Status

Coverage increased (+0.05%) to 97.403% when pulling 84b7a99 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.05%) to 97.403% when pulling 84b7a99 on marcbachmann:add-match-method into b35012f on yoshuawuyts:master.

@marcbachmann
Copy link
Collaborator Author

@yoshuawuyts updated. Thanks for the review.

Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

yay!

@yoshuawuyts yoshuawuyts merged commit 21711fb into choojs:master Dec 28, 2017
@marcbachmann
Copy link
Collaborator Author

Thanks 🎉

Ah, I forgot that the merge & rebase button now appends the pr link to the commit.
Now we have it twice in there. Sorry.
image

@marcbachmann marcbachmann deleted the add-match-method branch December 28, 2017 21:47
@yoshuawuyts
Copy link
Member

@marcbachmann ahahaha, no worries! :P

@yoshuawuyts
Copy link
Member

Published as v6.6.3 🎉

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.

3 participants