-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Context #171
Comments
Let's figure out how to store data. I see two approaches
const container: Map<NamespaceString, Map<StoreIdString, AtomStore>>
const enableGlobalNanostores = () => {
globalThis.__nanostores__ = container
}
// some builtin namespace to work out off the box
const defaultNamespace = 'default'
// access store and manipulate its content in atom methods
const atomStore = container.get(getCurrentNamespace())?.get(currentStoreId)
/// builtin container to work out of the box
const defaultContainer: Map<StoreIdString, AtomStore>
// access store and manipulate its content in atom methods
const atomStore = getCurrentContainer().get(currentStoreId)
const userContainer = createContainer()
globalThis.__myNanostores__ = userContainer
|
hi, |
@geminigeek the best solutions is to export store creators and not stores themselves export function createProfileStore () {
return atom()
} And then create a single instance in application root. |
Ref nanostores#171 Here's initial attempt to move all stores state into global context object. Context is basically a listeners queue and a map of stores states. Context management api is still yet to explore. For now it's for internal usage now.
Ref nanostores#171 Here's initial attempt to move all stores state into global context object. Context is basically a listeners queue and a map of stores states. Context management api is still yet to explore. For now it's for internal usage now.
My suggestion of Context API:
|
Looks great. One more use case I have is clearing context when switch routes on client. Maybe as additional method or helper. |
I have already been using nanostores, svelte stores, & solid-js signals isomorphically using an api which I made in @ctx-core/object which handles all of the use cases mentioned in #171. The only caveat is that the export declare type MapCtx = Map<Be<any>|string|symbol, unknown>
export interface NestedMapCtx extends Array<NestedMapCtx|MapCtx> {
}
export type Ctx = MapCtx|NestedMapCtx The Here is a small code sample of how I have been using the api: import { nullish_check_ } from '@ctx-core/function'
import { be_ } from '@ctx-core/object'
import { val__be_atom_triple_, val__be_computed_pair_ } from '@ctx-core/nanostores'
const default_user_id_ = be_(() => -1) // just showing the primitive be_ function here
const [
user_id$_,
user_id_,
user_id__set
] = val__be_atom_triple_(ctx => default_user_id_(ctx))
const [
user$_,
user_
] = val__be_computed_pair_(ctx =>
nullish_check_([user_id_(ctx)], () =>
fetch(`https://my-api/users/${user_id_(ctx)}`).then(response => response.json()))) Perhaps in the interest of keeping nanostores focused on the stores themself, it would make sense to create a separate library to manage context in an generalized & isomorphic way? The added benefit is that it would also work for any other type state management. I can shamelessly plug ctx-core but I'm also using a vector name system which is different from camelCaseNaming which works great for my use cases but is not the dominant trend...so I would be happy to assist on making a new library with more standard naming conventions & would better fit the mold of how nanostores is used. If this is not the direction you want to head in, then please disregard this comment. I just wanted to share in case it is something worth pursuing or there is anything useful from the solution. Edit I see there are already PR's in place for an implicit Context which looks great & probably makes sense in the context (no pun intended) of this project. |
Let’s discuss a way to solve a few problems.
Problem 1: Cache API
Many stores have cache:
Current Nano Stores has
MapTemplate
for such things. But it has a few problems:MapTemplate
.Problem 2: SSR
In SSR, Node.js will render HTML for multiple users in the same time (in the same Node.js environment). If you just export store from a file, all users will share the same store. It could be a very dangerous.
For instance, if we have something like
import { authStore } from '../store/auth.js'
will be the same store for all users.We need a way to separate stores from user’s environment
Problem 3: Mocks
It will be nice to be able to replace any store with a mock for tests or to create an environment for Storybook.
Right now we can use
atom#set()
or some stores provide custom API. But it doesn’t disable events, and it will be nice to have some common API.Problem 4: DevTools
It will be nice to have some sort of
Map
orSet
with all stores on the current page, so we will be able to use them in DevTools.It will be also nice to be able to dump the whole state to a file or reproduce of the state of user reported a bug.
The text was updated successfully, but these errors were encountered: