EventedMind/iron-router — pull request #93 by tmeasday (2013-08-30 09:03:16+00:00)
Title: Subscription overlap

Hey Chris, I think this is a good one.

You may or may not have noticed that if you `waitOn` the same subscription in different routes, the sub get re-run as you browse around.

If you don't know what I'm talking about, check the test I added, or this demo project: https://github.com/tmeasday/ir-double-publication-bug

Anyway, I figured out an easy solution, which I as I try to explain in the commit note, works because:

>Basically what happens is this:

> 0. We setup the computation and ran a `waitOn` inside, setting up a subscription.
> 1. We call `comp.stop()` here
> 2. The sub is listening for this, and marks itself as stopping, registering an `afterFlush` to properly stop itself.
> 3. However, as we registered our `afterFlush` first, ours runs first, potentially running a new `waitOn`.
> 4. If that new `waitOn` calls the same subscription, it picks up the old one, and it never actually stops.

> It's kind of a hack but it's pretty neat.

So all the code change I made was moving that `afterFlush` before the `computation.stop()`.

Now I know you've explained to me a bunch of times what the problem is that you were trying to deal with with this particular piece of code, but you'll have to excuse my terrible memory, as I don't remember exactly. But I've tested it with my app and it works as advertised, with no obvious bugs. And the tests pass.

Tom