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()
IMO
Both
daemon
/service
exist in the background, whether you use them now or not. In Angular,service
usually exists outside of time and space and doesn't lose its value if components using it are gone.Alas, I'm in the state of "You came here to criticize, but you don't even have your own variants". I will think harder
Compute(d) is more common than derived, though I came from Vue world full of them.
Atom seems pretty common too, in design systems at least. Though I don't personally like it, cause it conveys no meaning to me, why not
composable
for example, an atom has many properties, which of them do we wanna how in our stores?