goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 322 forks source link

Non-singleton stores #672

Closed gangstertim closed 8 years ago

gangstertim commented 8 years ago

It seems that the overwhelming preference is to keep stores as singletons. When they'll be used by multiple components simultaneously, the recommendation has been for components to pass a key to the store, and for the store state to be a map mapping keys to individual states.

That all works just fine, though sometimes gets just a little bit messy because of the prevalence of deeply-nested objects.

We're considering a non-singleton pattern whereby stores that are consumed by similarly-structured components are instantiated in connnected components. IE,

const storeInstanceA = new fooStore(storeId);
const fooComponentA = connectToStores({
  getStores() { return storeInstanceA; });
  ...
});

const storeInstanceB = new fooStore(storeId);
const fooComponentB = connectToStores({
  getStores() { return storeInstanceB; });
  ...
});

This pattern allows us to remove a layer of nesting from our store, which is nice! Is there any compelling reason NOT to do this?

sweetticket commented 8 years ago

+1

goatslacker commented 8 years ago

No, feel free to use your stores either as singletons or as instances.

The only con to this particular method I see is that a bunch of stores would be instantiated at runtime when the file is evaluated. This is probably not bad in itself but maybe you'd like to dynamically construct/destroy stores?