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

RFC: router.stop() and router.setOptions() #606

Closed
wants to merge 2 commits into from

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Dec 14, 2014

No description provided.

@gaearon
Copy link
Contributor Author

gaearon commented Dec 15, 2014

To clarify, this is my use case:

'use strict';

var routes = require('./routes'),
    Router = require('react-router');

var router = Router.create({
  routes: routes,
  location: Router.HistoryLocation
});

function render(Handler, state) {
  // ...
}

router.run(render);

// this is webpack's hot module reloading:
module.hot.accept('./routes', function () {
  routes = require('./routes'); // obtain new version of the module

  router.stop();
  router.setOptions({ routes: routes });
  router.run(render);
});

Probably, a better API would be to have router.setOptions method. Router.create would just use it under the hood, and anyone who wants to change options later (like me) can do that. This would spare us weird replaceRoutes method.

Also pause should probably be called stop.

Thoughts?

@gaearon gaearon changed the title router.pause() proof of concept RFC: router.stop() and router.setOptions() Dec 15, 2014
@gaearon
Copy link
Contributor Author

gaearon commented Dec 15, 2014

I added stop() and setOptions() and I think it fits in pretty well and doesn't look outlandish.

@guillaume86
Copy link

+1 for this, I have a very similar use case.
Just being able to change the routes config would also work for me but the .run() need a .stop() counterpart anyway.

@ryanflorence
Copy link
Member

I think I like this. A little busy at the moment to spend a lot of thought on it personally, but please keep this conversation going :)

@gaearon
Copy link
Contributor Author

gaearon commented Dec 15, 2014

OK, I'll do my best to add some tests later this week!

@camshaft
Copy link

+1. this is definitely needed for hot reloading after the API changes

@gaearon
Copy link
Contributor Author

gaearon commented Dec 23, 2014

I don't think I'm going to have the time this week to write the tests, and later I'm on holiday.
If somebody volunteers to pick it up, I'm all for it!

@mjackson
Copy link
Member

@gaearon I like stop and replaceRoutes (we already have addRoutes. just use that).

Also, let's fix the weirdness that was introduced in 6417285 along the way.

@gaearon
Copy link
Contributor Author

gaearon commented Dec 23, 2014

@mjackson Do you think replaceRoutes > setOptions? I think setOptions is cleaner because it isn't specific and can be used by Router.create too.

@mjackson
Copy link
Member

@gaearon I've tried as hard as I can thus far to avoid passing an "options" hash to Router.create. Options hashes are messy IMO, and the router's scope is small enough that we should be able to avoid it. Also, once you introduce an options hash it sort of becomes the default place to add everything, which leads to poor API design later on.

Anyway, just my opinion obviously. Feel free to disagree :)

@gaearon
Copy link
Contributor Author

gaearon commented Dec 24, 2014

I see your point now! Let's proceed with replaceRoutes then and take another look if any use case for changing other options midflight comes up.

mjackson added a commit that referenced this pull request Dec 29, 2014
This commit adds a router.stop() method that may be used to prevent
a router from listening to further changes to location. This method
is also called automatically when a router component is removed from
the DOM.

Thanks to @gaearon and @rpflorence for the initial thrust to get this
functionality into the router. This work doesn't entirely supercede #606,
but duplicates much of the work done there.
@mjackson
Copy link
Member

@gaearon I implemented router.stop in 75c6206, which is half of the work needed here. If you'd like to do replaceRoutes in this PR, that would be great. Otherwise we can close and do it later.

@gaearon
Copy link
Contributor Author

gaearon commented Dec 29, 2014

Let's do it later! I'm on a vacation now and can do replaceRoutes separately later if no one beats me to it.

@mjackson
Copy link
Member

@gaearon Sounds good. Enjoy your holiday! :)

@mjackson mjackson closed this Dec 29, 2014
@gaearon gaearon deleted the router-stop branch January 5, 2015 08:37
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants