@inglorious/store
v12.9.6
Published
A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.
Maintainers
Readme
Inglorious Store
An entity-based state store, inspired by game engines and compatible with Redux's tooling. State is a map of plain entities, behavior lives in types, and everything that happens is an event.
Install
pnpm add @inglorious/storeIn a few lines
import { createStore } from "@inglorious/store"
const types = {
TodoList: {
todoAdd(entity, text) {
entity.todos.push({ id: Date.now(), text, isDone: false })
},
},
}
const entities = {
work: { type: "TodoList", todos: [] },
personal: { type: "TodoList", todos: [] },
}
const store = createStore({ types, entities })
store.notify("todoAdd", "Buy milk") // every TodoList
store.notify("#work:todoAdd", "Finish report") // only `work`- An entity is a plain object with an
idand atype. A type is an object of event handlers,handler(entity, payload, api), which mutate the entity they are given; Mutative turns the mutation into a new immutable state. - An event reaches every entity whose type handles it, or one type (
"TodoList:todoAdd"), or one entity ("#work:todoAdd"). Handlers notify further events throughapi.notify, and the queue applies them in order, one transaction each. - Entities have a lifecycle:
addandremoverun theircreateanddestroy, where a type opens and closes what it needs from the outside world.
What's in it
handleRequestandhandleAsync: the lifecycle of an async call as events, with status and error kept on the entity, outdated answers dropped, andoptimisticupdates.fsmandscenes: a type that answers events by state, and the entities that stand in each state.withStorage: fields kept in local storage as a byproduct of the entity.- Systems and middlewares: handlers that see every entity at once, and functions that wrap the store.
compute: memoized derived state, withcreateSelectoras its Redux-compatible alias.- Redux DevTools and
react-redux: the store hassubscribe,getStateanddispatch, so a React app can adopt it as its Redux store first and move its components to Inglorious Web later.@inglorious/react-storeaddsuseEntityanduseNotify. - Testing:
trigger(entity, handler, payload)runs a handler and returns the new entity and the events it notified, with no store. - TypeScript: typed entities, handlers, payloads and events, with no casts.
Two rules
- A handler writes to the entity it is given, before its first
await. The entity is a draft that lives until the update ends; after anawait, notify an event that does the writing, or usehandleRequest. - An event's handlers read the state as the previous event left it. Whatever order they run in, they see the same world, and the next event sees what they changed.
Where it goes
Standalone, in a React app through react-redux or @inglorious/react-store, under Inglorious Web for the DOM, under Inglorious Engine for games, or on a server with @inglorious/server for multiplayer.
License
MIT © Matteo Antony Mistretta
