# anurag6713/ninja-store architecture

> Architecture diagram of the GitHub repository anurag6713/ninja-store, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anurag6713/ninja-store
- Repository: https://github.com/anurag6713/ninja-store

## Overview

##### Project type and purpose
- This is an open-source React state-management library (“ninja-store”) published on npm.
- Purpose: To let React and React Native components create and share simple named stores (with optional reducer logic) via a custom hook (`useStore`).

##### File structure
- Top-level files:
- createStore.js – implements the factory that returns `useStore`.
- stores.js – holds the internal registry of all named stores and implements subscribe/notify.
- index.js / index.d.ts – module entry point and TypeScript declarations.
- package.json / README.md – metadata and documentation.
- No “frontend” or “backend” dirs – this is purely a client-side library.
- No build or deployment scripts present in the tree (they’re presumably external).

##### Key insights from the README
- The public API surface is:
a. `createStore(name, initialState)`
b. `createStore(name, reducer, initialState)`
c. The returned object exposes `{ useStore }`, where `useStore([mapState])` returns `[state, setState]` or `[state, dispatch]`.
- It supports:
- Plain setter API (`setState`)
- Reducer/dispatch API
- Optional `mapState` selector to subscribe only to slices of the store.

1. Consumer Components
– any React or React Native component that calls `useStore()`.
2. `ninja-store` Library
– createStore module (createStore.js)
– Stores registry (stores.js)
– Exports (index.js / index.d.ts)
3. Store Instance
– one per unique string name, holding:
- state container (JS object)
- Of subscribers (React component hooks)
4. React (external)
– the React runtime that calls hooks and re-renders components.
1. React component calls → `useStore()`
2. `useStore()` registers the component’s subscription with a named store in “stores registry.”
3. Component uses setter or dispatch to update store state → store registry updates state.
4. Store registry notifies all subscribed components → React schedules re-render.
c. Architectural pattern
- Simple pub/sub (observer) pattern combined with React hooks.
- Optional reducer pattern for structured actions.
- No global context provider – stores are managed in an internal registry keyed by name.
d. Key technologies
- JavaScript (ES6 modules)
- React / React Native hooks API
- Optional TypeScript typings (index.d.ts)

##### Final recommendations
- Multiple consumer components subscribing to the same store instance to emphasize shared state.

## Diagram (Mermaid)

```mermaid
flowchart TB
    ReactRuntime["React Runtime"]:::external

    subgraph "ninja-store Library"
        CS["createStore.js (factory)"]:::lib
        US["useStore hook"]:::lib
        SR["stores.js (registry & pub/sub)"]:::lib
        IP["index.js (public API export)"]:::lib
        DT["index.d.ts (TypeScript declarations)"]:::lib
    end

    subgraph "Documentation & Metadata"
        MD["README.md"]:::doc
        PJ["package.json"]:::doc
    end

    StoreInstance["Store Instance\n(state container)"]:::store

    ConsumerA["Consumer Component A"]:::consumer
    ConsumerB["Consumer Component B"]:::consumer

    ReactRuntime -->|"calls useStore()"| CS
    CS -->|"returns useStore hook"| US
    ConsumerA -->|"useStore() subscribe"| US
    ConsumerB -->|"useStore() subscribe"| US
    US -->|"register in"| SR
    SR -->|"create/update state"| StoreInstance
    ConsumerA -->|"setState()/dispatch()"| StoreInstance
    StoreInstance -->|"notify subscribers"| SR
    SR -->|"trigger re-render"| ReactRuntime

    click CS "https://github.com/anurag6713/ninja-store/blob/master/createStore.js"
    click SR "https://github.com/anurag6713/ninja-store/blob/master/stores.js"
    click IP "https://github.com/anurag6713/ninja-store/blob/master/index.js"
    click DT "https://github.com/anurag6713/ninja-store/blob/master/index.d.ts"
    click MD "https://github.com/anurag6713/ninja-store/blob/master/README.md"
    click PJ "https://github.com/anurag6713/ninja-store/blob/master/package.json"

    classDef lib fill:#cce5ff,stroke:#004085;
    classDef store fill:#d4edda,stroke:#155724;
    classDef consumer fill:#ffe5b4,stroke:#cc8400;
    classDef external fill:#e2e3e5,stroke:#b7b7b7;
    classDef doc fill:#f8d7da,stroke:#721c24;
```
