AlexGalays/abyssa-js

Reusing the same state

Closed this issue · 3 comments

I'm having troubles to understand if what I see is a bug or not... I have an application that do standard REST url like: /resources/:id/subResource, and you can stop at any point of the url of course. Since Abyssa doesn't allow to stop outside of a leaf, I tried to create an empty state for stopping midway if the abstract parent has already do all the logic. But when using the same empty state in several states... all is going crazy.

Here is a sample: http://jsbin.com/vajovire/2/edit?js,output

Am I missing something?

UPDATE

So one solution was to create factory for common states returning a new state each time: http://jsbin.com/vajovire/4/edit?js,output

I'm starting to think it might be related to the fact that one state cannot have several parents... which seems fair enough :-)

That is correct; The same state instance cannot be reused in different places; This would be quite difficult to do because the state that is handed over is augmented by the router, so in your case, it was augmented many times, and the last one won; (shamefully mutable)

I even observed this issue when using browserify and two files with the same exact (stub) content: Browserify was reusing one of the two modules and I thus had the same instance reused in two different places.

This should probably be documented; I do not have a good solution to lift this limitation, other than creating an indirection by forbidding all accesses to myLocalState.parent, myLocalState.data, etc and exclusively using this.parent, this.data, etc but I don't think it's an improvement really.

Roger that. Thanks for the answer.

I think the router could throw an Error when a state instance is reused