Comments (4)
There is also another tangent to this: when there is an isGlobal
container, RSS will execute the relevant actions (onInit
, onUpdate
etc.) but if you forget the container (or misplace the hook), it will silently continue using the non-initialized global instance. I think this will also detect container-less usages as once you set isolated
to true, you will have to use a container as well. OTOH will only allow a global container, not a scoped one. So maybe we also need a contained: true
which will throw on container-less store accesses.
From another viewpoint, one can argue that detecting the existence of a container is possible by additional logic at onInit
. Is there a way to detect the same condition (if the instance is global) from the userland code?
from react-sweet-state.
That sounds interesting - distinguish global containers, local containers and container-less use cases.
contained: undefined, // or 'global'
contained: 'local', // local scope-less container
contained: 'scope', // local any scoped container
contained: true // any container, including global
Might be too much for one field, but this looks like a single property. Let's do the opposite and create 3 different options:
// requires container with a scope
scoped: true, // false
// disallow global usage
global: false
// requires any container
contained: true
All 3 makes a sense, but not simultaneously.
from react-sweet-state.
Maybe something named expectContainer
? Because I feel like we want to catch problems when some hooks are used outside container but not sure what use case there is for "limiting" a store to be used only on scoped/local
However, I'm not a huge fun of blowing things up. If we proceed with #184 then it is quite simple to "log" things that end up being global but they should not.
As said in the opening message, this behaviour can be implemented with tag + global container.
const AppContainer = createGlobalContainer({
capture: (StoreType) => !StoreType.tag.startsWith('global'),
onStoreInit: (storeInstance, otherInitialisedStores) => {
// log exception and report store
},
})
But I understand this will be less "harsh" than the store self check as it will not catch places where AppContainer
is missing. Unless we add a global api to the Registry class:
defaultRegistry.onStoreInit((storeInstance, otherInitialisedStores) => {
// log exception and report store
})
That way we can directly look into the stores initialised in the global scope
from react-sweet-state.
Proposed solution:
- extend
createStore
, add new optional properly -behavior
/target
/mode
(🤷♂️ )
createStore(
initialState: {},
actions: { },
name: 'my-store',
behavior: 'contained', // cannot be global. Writing or reading to the global store will throw an error
behavior: 'global', // cannot be containerized. Explicitly global
behavior: undefined, // uncontrolled
})
// alternatively utilize single boolean
createStore(
initialState: {},
actions: { },
name: 'my-store',
isolated: true, // isolated
isolated: false, // global
isolated: undefined, // uncontrolled
});
- in order not to worry much about the API above - expose constructs with better discoverability. Aka
named factories
:
- createLocalStore
- createGlobalStore
- depending on friction adopting test - add extra command to disable isolation control in tests, at least for migration period. Something I don't think we need, but something I feel we might need.
from react-sweet-state.
Related Issues (20)
- Dates don't work in selectors HOT 2
- Shortcut for "action only" selectors HOT 1
- A better way to orchestrate multiple stores HOT 2
- Evaluate move to useSyncExternalStore
- [typescript] Wrong type for StoreState.key HOT 2
- Is this lib still maintained? HOT 1
- [Suggestion] Change `ResourceStoreContext` to use an interface instead of type so consumers can do declaration merging HOT 2
- Typescript: Can't use container without explicitly declared prop 'children' on it HOT 1
- [Documentation request] Comparison of bundle sizes HOT 2
- The useHook with selector does not trigger rendering properly HOT 3
- [Documentation request] - Example with Next.js SSR
- Possibly incorrect type definition for `ActionThunk` HOT 2
- Incorrect selector behaviour with promises
- Input caret will jump to end of string when using store HOT 4
- better type/name for dispatched actions HOT 1
- Persistent global scoped stores HOT 2
- "Families" containers HOT 7
- Ability to pass callback function during store creation for store initialisation and updates HOT 3
- Container cleanup occurs after remounting, clearing state set by onInit HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-sweet-state.