Skip to content

Instantly share code, notes, and snippets.

@ai
Last active September 9, 2021 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ai/e8b0313b7733d54df15a4ce5499e087d to your computer and use it in GitHub Desktop.
Save ai/e8b0313b7733d54df15a4ce5499e087d to your computer and use it in GitHub Desktop.

New API for Nano Stores

We split all stores to 2 categories: store (for any store) and map (for key-value object and special helpers to work with keys).

Memory store is a regular store, which keep value in the memory (and do not clean it until page will be closed).

import { memoryStore } from 'nanostores'

let profileCache = memoryStore<Profile>(guestProfile)

Old API: store = createStore(); keepActive(store)

Daemon store is a store with constructor and destructor. It could listen other services (like WebSocket or History API). It has active and disabled mode. On first store’s listener it goes to active mode. On last listener unsubscibring it will be removed from the memory and will lost it’s value.

import { daemonStore } from 'nanostores'

let router = daemonStore<Route>(defaultRoute, () => {
  // subscribe to history API
  return () => {
    // unsubscribe from history API
  }
})
import { daemonMap } from 'nanostores'

// For key-value store

Old API: createStore()

Question: maybe “service store” is better than “deamon store”?

Deamon template is a template to create similar map stores. For instance, every User could be a separated store (useful for CRDT libraries like Logux, where every User store hides CRDT complixty inside):

import { deamonTemplate } from 'nanostores'

let User = deamonTemplate((store, id, ...args) => {
  // connect listsners to WebSocket log and listen for CRDT events
  () => {
    // remove listener from log
  }
})

let user1 = User(id, ...args)

Old API: defineMap()

@AleksandrSl
Copy link

I think atom is a very good name for a non-map 'store' that holds one value without the internal structure, selectors and so on.

Still think that in this case singleValue('ru')/single('ru') is more precise and meaningful
Smart is no a good prefix because it doesn't describe the way in which a store is smart. In our case that it can be initialized and destroyed.

Silly idea: use mortal/immortal store, at least it shows that store could become deactivated/dead

@ai
Copy link
Author

ai commented Sep 9, 2021

I moved API discussion to issue nanostores/nanostores#57

@ai
Copy link
Author

ai commented Sep 9, 2021

Still think that in this case singleValue('ru')/single('ru') is more precise and meaningful

It can be an object (like we have in profile store or in router) which can be changed key by key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment