-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Best practises for singleton stores in ES6 #1192
Comments
Hi
|
Doesn't matter. The consumer can't make any assumption about
Note it still relies on module cache. Imho, it's worse than exporting the object itself, because it hides the same functionallity behind a syntax, which suggests absolutely different behavior (creation of a new object).
I think the reasoning here is that it doesn't make sense to have a class(prototype) for something, which is never actually created more than once. Aside from possible confusion, I don't see any practical implications...
This. People should forget about singletons and rather focus on how to manage dependencies in general. |
Agrees with @urugator. Discussion here may helps :) @LeonardoGentile |
@LeonardoGentile I changed the docs, no clue why I put Beyond that, I think this discussions goes a bit beyond mobx itself and might better fit SO |
This is not strictly a mobx question, but it is more about the best practise to create singleton stores.
In your documentation you states:
This can be achieved in many different ways, an example and a commonly used pattern (also used in many of your examples) is to export a class instance from a module, this will become a singleton because of module caching:
Some says this is a bad pattern because it relies on module caching and because it would still be possible to create new instances of the classes if the consumer really wants it, for example:
Others also says we should never use a class instance for creating a singleton, singleton should be just plain objects. Even tough I agree that mobx stores could be just plain objects that makes a bit more difficult to organise code compared to the class syntax. So another common pattern I've seen for creating singleton from classes is this:
There are also other ways of creating singletons, here I have only presented 2 just for example.
Also to avoid circular dependencies it might be a good idea to never export single instances from modules, we should export class/object definitions and then instantiate/initialise them in the entry point.
What's your take on this, what would you consider to be the best practise to create singleton stores in ES6 for a complex application?
The text was updated successfully, but these errors were encountered: