Tiny·Engine (Core)

API reference

CapsuleStore

CapsuleStore is a reducer-style state container with connect/send, middleware, and devtools inspection.

surface
import { CapsuleStore } from "tiny-engine-core";

const store = new CapsuleStore(reducer, initialState);

store.snapshot();       // current state
store.inspect();        // id, state, listener count, middleware count
store.send(action);     // run middleware, reduce, notify
store.connect(listener) // listener(state, action), returns unsubscribe
store.use(middleware);  // middleware(action, state) => action | false | void
store.destroy();        // clear listeners/middleware and devtools snapshot

Reducer flow

Construct a store with a reducer and initial state. Dispatch actions with send(); subscribers registered with connect() receive the new state and action.

counter-store.ts
const counter = new CapsuleStore(
  (state, action) => {
    if (action.type === "increment") {
      return { ...state, count: state.count + 1 };
    }
    return state;
  },
  { count: 0 }
);

counter.use((action, state) => {
  if (action.type === "increment" && state.count >= 10) return false;
  return action;
});

const off = counter.connect((state, action) => {
  console.log(action.type, state.count);
});

counter.send({ type: "increment" });
off();

Middleware

Middleware runs before the reducer. It can return a replacement action, return false to cancel, or return nothing to keep the current action.