Giter Site home page Giter Site logo

Non global states about react-sweet-state HOT 4 CLOSED

theKashey avatar theKashey commented on July 20, 2024
Non global states

from react-sweet-state.

Comments (4)

anacierdem avatar anacierdem commented on July 20, 2024 1

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.

theKashey avatar theKashey commented on July 20, 2024

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.

albertogasparin avatar albertogasparin commented on July 20, 2024

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.

theKashey avatar theKashey commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.