-
Notifications
You must be signed in to change notification settings - Fork 60
Description
The "global" nature of stores is a feature of sweetstate making it easy to use. It is also some sort of 🔫 footgun creating an opportunity for various issues to appear.
Containers for the rescue, but creating and more importantly using containers for all possible stores is a little complicated, potentially leading to a "wrong coupling" when a parent has to pull containers from different dependencies in order to apply them in a correct place.
I would like to prose an extension to the default Container primitive capable of acting as a boundary for multiple stores, a single Container many:
BoundaryContainer: emulatesdefaultRegistrypassing to the one above some stores scoping the rest inside self- in a "stop all" mode, it is handy in tests and storybooks to provide more "local" behavior and reduce the necessity to clear
defaultRegistryand pollute the global scope. - in "stop stores starting with" it is handy around some isolated experience as it limits sub-stores of that experience inside known boundaris
- in a "stop all" mode, it is handy in tests and storybooks to provide more "local" behavior and reduce the necessity to clear
Technical implementation
I can see multiple different ways of achieving desired functionality but would like to propose a family(or a tag) based one.
import {createFamily, BoundaryContainer} from 'react-sweet-state';
const familyA = createFamily('unique-name');
const Store = createStore({
initialState: {
count: 0,
},
actions: {},
// ⬇️
family: [familyA], // one can be a part of multiple families, the nearest BoundaryContainer with known family will act as a defaultRegistry
});
// -----
<BoundaryContainer family={familyA} scope="Doe">
// all stores with corresponding family will "end" here
<Store />
</FamilyContainer>Technically speaking, a single FamilyContainer will "unfold" into multiple "normal" ones. And obviously - it can be only a boundary with no life-time hooks present.
Extra requirements
With some stores now being able be "captured" at some point it might be handly to introduce stores which have to be captured. For example, one might demand that a store with configured family should have a dedicated Container to be used as store is "expected" to be isolated.
However, such a feature might be useful for other states as well and ideally should be configurable separately.