Managing Stores

The createStore function is the core of QuantaJS, providing a structured way to manage state, getters, and actions.

Creating a Store

import { createStore } from "quantajs";

const counter = createStore({
  state: { count: 0 },
  getters: {
    isPositive: (state) => state.count > 0,
  },
  actions: {
    increment() {
      this.count++;
    },
  },
});

console.log(counter.count); // 0
counter.increment();
console.log(counter.count); // 1
console.log(counter.isPositive); // true

Flattened Access

Stores flatten state, getters, and actions into a single interface:

counter.count = 5; // Direct state mutation
console.log(counter.isPositive); // true

Store Registry

Stores are registered by name to prevent duplicates:

import { useStore } from "quantajs";

const store = createStore("myStore", {
  state: { value: "hello" },
});

const retrieved = useStore("myStore");
console.log(retrieved.value); // "hello"
Attempting to create a store with an existing name throws an error.