react-state-basis 0.2.0 β 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -365
- package/dist/index.d.mts +18 -11
- package/dist/index.d.ts +18 -11
- package/dist/index.js +77 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +76 -52
- package/dist/index.mjs.map +1 -1
- package/dist/production.d.mts +32 -0
- package/dist/production.d.ts +32 -0
- package/dist/production.js +115 -0
- package/dist/production.js.map +1 -0
- package/dist/production.mjs +58 -0
- package/dist/production.mjs.map +1 -0
- package/package.json +10 -3
package/README.md
CHANGED
|
@@ -2,145 +2,84 @@
|
|
|
2
2
|
<img src="./assets/logo.png" width="300" alt="Basis Logo">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
[](https://github.com/liovic/react-state-basis#readme)
|
|
7
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
<div align="center">
|
|
8
6
|
|
|
9
7
|
# π REACT-STATE-BASIS
|
|
10
8
|
### **Behavioral State Analysis for React**
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
**TL;DR:**
|
|
17
|
-
React-State-Basis watches your state like a mathematician: every `useState`, `useReducer`, and even `useEffect`-driven update becomes a tracked signal that turns into a time-series vector. When two signals move identically over time, they're collinear (redundant) - and Basis instantly flags it with location, math details, and a copy-paste refactor to derived state.
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
**React-State-Basis** is a real-time architectural auditing engine that treats a React application as a **dynamic system of discrete-time vectors**. Instead of static linting, which only analyzes syntax, Basis monitors the **State Space Topology** of your application to detect mathematical redundancy (collinearity) and synchronization anti-patterns in real-time.
|
|
22
|
-
|
|
23
|
-
Inspired by the work of **Sheldon Axler** (*"Linear Algebra Done Right"*), Basis aims to enforce a mathematically optimal "Source of Truth" by ensuring your application state forms a **Basis**.
|
|
24
|
-
|
|
25
|
-
---
|
|
10
|
+
[](https://www.npmjs.com/package/react-state-basis)
|
|
11
|
+
[](https://github.com/liovic/react-state-basis/stargazers)
|
|
12
|
+
[](https://opensource.org/licenses/MIT)
|
|
26
13
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
npm i react-state-basis
|
|
31
|
-
```
|
|
14
|
+
</div>
|
|
32
15
|
|
|
33
16
|
---
|
|
34
17
|
|
|
35
|
-
|
|
18
|
+
### The Core Concept
|
|
19
|
+
In Linear Algebra, a **Basis** is a set of linearly independent vectors that span a space. **Basis** treats your React application as a dynamic system where every state variable is a signal over time.
|
|
36
20
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
Mathematically, your state variables $\{v_1, v_2, \dots, v_n\}$ should form a **Basis** for your application's state space $V$. A Basis must be **linearly independent**. If two variables update in perfect synchronization, they are **collinear** (linearly dependent). This creates:
|
|
40
|
-
1. **Redundant Renders:** Multiple cycles for a single logical change.
|
|
41
|
-
2. **State Desynchronization:** High risk of "impossible states" (e.g., `user` exists but `isLoggedIn` is false).
|
|
42
|
-
3. **Architectural Entropy:** High cognitive load in tracing data causality.
|
|
21
|
+
If two states always update in lockstep, they are **linearly dependent** (redundant). Basis detects these "Dimension Collapses" at runtime and suggests refactoring to derived state (`useMemo`).
|
|
43
22
|
|
|
44
23
|
---
|
|
45
|
-
## π See It In Action
|
|
46
|
-
|
|
47
|
-
> ### π‘ Detecting Causal Links & Double Renders
|
|
48
|
-

|
|
49
24
|
|
|
50
|
-
|
|
25
|
+
## How it Works: The Vectorization Engine
|
|
51
26
|
|
|
52
|
-
|
|
27
|
+
Basis doesn't care about the *values* of your state. It monitors the **topology of transitions**.
|
|
53
28
|
|
|
54
|
-
|
|
55
|
-
|
|
29
|
+
### 1. The System Tick
|
|
30
|
+
The engine groups all state updates occurring within a **20ms window** (aligned with the 60FPS frame budget) into a single **System Tick**.
|
|
56
31
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
**The Insight:** It flags a **Dimension Collapse**, alerting you that 3 independent state variables are actually spanning only 1 dimension of information. It suggests consolidating them into a single state machine or a status string.
|
|
62
|
-
|
|
63
|
-
> ### π Circuit Breaker (Infinite Loop Protection)
|
|
64
|
-

|
|
65
|
-
|
|
66
|
-
**The Trap:** A recursive `useEffect` that triggers an infinite state oscillation, a common mistake that usually freezes the browser's main thread.
|
|
67
|
-
|
|
68
|
-
**The Intervention:** Basis acts as a real-time stability monitor. If it detects a high-frequency state oscillation (e.g., 25 updates within 500ms), it automatically activates the **Circuit Breaker**.
|
|
69
|
-
|
|
70
|
-
**The Result:** The engine forcefully halts the update chain before the browser locks up. It provides a critical diagnostic report with the exact location of the loop, allowing you to fix the bug without having to kill the browser process.
|
|
71
|
-
|
|
72
|
-
> ### π Cross-Context Dependency Audit
|
|
73
|
-

|
|
74
|
-
|
|
75
|
-
**The Scenario:** Modern apps often split state into multiple providers (e.g., `AuthContext` and `ThemeContext`). While architecturally decoupled, they are often **manually synchronized** in logic (e.g., switching to "dark theme" every time a user logs in).
|
|
76
|
-
|
|
77
|
-
**The Global Discovery:** Basis performs a **Global State Space Audit**. It doesn't care where your state lives in the component tree; it only cares about the **temporal signals**.
|
|
78
|
-
|
|
79
|
-
**The Insight:** By initiating a "Global Sync," Basis identifies that `user` and `theme` are moving in perfect synchronization. It exposes **hidden coupling** between disparate parts of your architecture.
|
|
80
|
-
|
|
81
|
-
**The Benefit:** This helps architects identify states that should potentially be merged or derived from a single source of truth, even when they are physically separated across different providers.
|
|
82
|
-
|
|
83
|
-
> ### π System Health & Structural Audit
|
|
84
|
-

|
|
85
|
-
|
|
86
|
-
**System Rank & Efficiency:** Basis performs a global audit of your state space to calculate its **Mathematical Rank**βthe actual number of independent information dimensions. An efficiency of **40% (Rank: 4/10)** warns you that 60% of your state is mathematically redundant.
|
|
87
|
-
|
|
88
|
-
**Redundancy Clusters:** Instead of a raw matrix, Basis automatically groups "entangled" variables into **Redundancy Clusters**. Whether they are booleans in a single component or states across different contexts, Basis identifies them as a single, collapsed dimension if they move in perfect sync.
|
|
32
|
+
### 2. State-to-Vector Mapping
|
|
33
|
+
Every hook is mapped to a vector $v$ in a 50-dimensional space $\mathbb{R}^{50}$ representing a sliding window of the last 50 ticks:
|
|
34
|
+
* `1` = State transition occurred during this tick.
|
|
35
|
+
* `0` = State remained stagnant.
|
|
89
36
|
|
|
90
|
-
**
|
|
37
|
+
**Example of two redundant (collinear) states:**
|
|
38
|
+
```text
|
|
39
|
+
State A: [0, 1, 0, 0, 1, 0, 1, 0, 1, 0] <-- vA
|
|
40
|
+
State B: [0, 1, 0, 0, 1, 0, 1, 0, 1, 0] <-- vB
|
|
41
|
+
Result: Cosine Similarity = 1.00 (REDUNDANT)
|
|
42
|
+
```
|
|
91
43
|
|
|
92
|
-
|
|
44
|
+
Basis detects synchronization, not identity. Even if the data inside the hooks is different, if they always change at the same time, they are collinear in the state-space."
|
|
93
45
|
|
|
94
|
-
|
|
46
|
+
### 3. Real-time Auditing
|
|
47
|
+
Every 5 ticks, Basis calculates the **Cosine Similarity** between all active state vectors. If similarity exceeds **0.88**, an architectural alert is triggered in your console with a suggested fix.
|
|
95
48
|
|
|
96
49
|
---
|
|
97
50
|
|
|
98
|
-
##
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
Here's a quick comparison:
|
|
103
|
-
|
|
104
|
-
| Tool / Approach | Static analysis | Runtime behavior tracking | Detects temporal synchronization | Gives refactor suggestions | Overhead in production | Focus on mathematical independence |
|
|
105
|
-
|----------------------------------|-----------------|----------------------------|----------------------------------|----------------------------|------------------------|-------------------------------------|
|
|
106
|
-
| ESLint + plugins (no-redundant-state, etc.) | β
| β | β | Partial (rules only) | None | β |
|
|
107
|
-
| React DevTools | β | Partial (component tree) | β | No | Low | β |
|
|
108
|
-
| Why Did You Render | β | β
(render tracking) | Partial (render causes) | No | Removable | β |
|
|
109
|
-
| Redux/Zustand DevTools | β | β
(store changes) | β (only store level) | No | Removable | β |
|
|
110
|
-
| react-state-basis (Basis) | Partial (Babel) | β
| β
(tick-based sync detection) | β
(copy-paste useMemo) | Zero (change imports) | β
(inspired by linear algebra) |
|
|
111
|
-
|
|
112
|
-
Basis shines when:
|
|
113
|
-
- You have manual state syncing (`setA β setB` in effects/onClick/etc.)
|
|
114
|
-
- Multiple booleans or flags that are always updated together
|
|
115
|
-
- You want to know which state is the true "source of truth" without guessing
|
|
116
|
-
- You want runtime insights that linters can't see (because they don't run the code)
|
|
117
|
-
|
|
118
|
-
It is not trying to be a full state manager, linter replacement, or performance profiler.
|
|
119
|
-
It is a specialized diagnostic tool for one very common anti-pattern - and it tries to do that one thing really well.
|
|
120
|
-
|
|
51
|
+
## Ghost Mode: Zero-Overhead Production
|
|
52
|
+
Basis is a **development-only** infrastructure. Using **Conditional Exports**, it automatically swaps itself for a "Zero-Op" version in production:
|
|
53
|
+
* **Development:** Full Linear Algebra engine and auditor active.
|
|
54
|
+
* **Production:** Exports raw React hooks directly. **Zero bundle bloat. Zero performance penalty.**
|
|
121
55
|
|
|
122
56
|
---
|
|
123
57
|
|
|
124
|
-
##
|
|
125
|
-
|
|
126
|
-
To enable the mathematical monitoring of your application, follow these two steps:
|
|
58
|
+
## Quick Start
|
|
127
59
|
|
|
128
|
-
### 1.
|
|
129
|
-
|
|
60
|
+
### 1. Install
|
|
61
|
+
```bash
|
|
62
|
+
npm i react-state-basis
|
|
63
|
+
```
|
|
130
64
|
|
|
65
|
+
### 2. Initialize
|
|
131
66
|
```tsx
|
|
132
67
|
import { BasisProvider } from 'react-state-basis';
|
|
133
68
|
|
|
134
69
|
export default function Root() {
|
|
135
70
|
return (
|
|
136
|
-
<BasisProvider debug={true}>
|
|
71
|
+
<BasisProvider debug={true}> {/* Set debug={false} for total silence in dev */}
|
|
137
72
|
<App />
|
|
138
73
|
</BasisProvider>
|
|
139
74
|
);
|
|
140
75
|
}
|
|
141
76
|
```
|
|
77
|
+
**Props:**
|
|
78
|
+
* `debug (boolean)`:
|
|
79
|
+
* `true` (Default): Enables the real-time diagnostic dashboard, visual system status badge (Web only), and detailed console auditing (including the Circuit Breaker).
|
|
80
|
+
* `false`: Completely deactivates the Basis engine. No background analysis, no memory consumption, and no logging. The "Circuit Breaker" is also disabled in this mode to allow for a zero-overhead, raw React experience in development.
|
|
142
81
|
|
|
143
|
-
###
|
|
82
|
+
### 3. Drop-in Replacement
|
|
144
83
|
Replace your standard React hook imports with `react-state-basis`. This allows the engine to instrument your state updates without changing your component logic.
|
|
145
84
|
|
|
146
85
|
**Standard Named Imports (Recommended):**
|
|
@@ -177,19 +116,13 @@ function MyComponent() {
|
|
|
177
116
|
const [count, setCount] = Basis.useState(0); // Also tracked automatically
|
|
178
117
|
}
|
|
179
118
|
```
|
|
119
|
+
---
|
|
180
120
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
> If you choose not to use the Babel plugin, you can still get specific labels by passing a string as the last argument:
|
|
185
|
-
|
|
186
|
-
```tsx
|
|
187
|
-
const [count, setCount] = useState(0, "MyComponent -> count");
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
To get the most out of Basis, you should enable the Babel plugin. This automatically injects the **filename** and **variable name** into your hooks so you don't have to label them manually.
|
|
121
|
+
## Automated Diagnostics (Babel)
|
|
122
|
+
While Basis works out of the box, hooks will be labeled as `anonymous_state` by default. To get the rich diagnostics seen in the demos (with automatic **filenames** and **variable names**), we highly recommend using our Babel plugin.
|
|
191
123
|
|
|
192
|
-
|
|
124
|
+
### Vite Integration
|
|
125
|
+
Add Basis to your `vite.config.ts`. It will automatically instrument your hooks during build time:
|
|
193
126
|
|
|
194
127
|
```typescript
|
|
195
128
|
// vite.config.ts
|
|
@@ -201,7 +134,6 @@ export default defineConfig({
|
|
|
201
134
|
react({
|
|
202
135
|
babel: {
|
|
203
136
|
// Automatically labels useState, useMemo, etc.
|
|
204
|
-
// for richer diagnostics in the console.
|
|
205
137
|
plugins: [['react-state-basis/plugin']]
|
|
206
138
|
}
|
|
207
139
|
})
|
|
@@ -209,285 +141,87 @@ export default defineConfig({
|
|
|
209
141
|
})
|
|
210
142
|
```
|
|
211
143
|
|
|
144
|
+
### Manual Labeling (Alternative)
|
|
145
|
+
If you prefer not to use Babel, you can manually label any hook by passing a string as the last argument:
|
|
212
146
|
|
|
213
|
-
---
|
|
214
|
-
|
|
215
|
-
## π΅οΈββοΈ Technical Architecture
|
|
216
|
-
|
|
217
|
-
React-State-Basis utilizes a three-tier instrumentation pipeline to audit your system:
|
|
218
|
-
|
|
219
|
-
### 1. The Compiler Layer (Babel AST)
|
|
220
|
-
A build-time plugin performs static analysis, injecting the **filename** and **variable name** directly into the runtime calls. This transforms an anonymous execution graph into a **structured state map**.
|
|
221
|
-
|
|
222
|
-
### 2. The Runtime Layer (Signal Mapping)
|
|
223
|
-
Every state transition is intercepted. Basis groups updates occurring within a **16ms window** into a single "System Tick." Each state variable is mapped to a vector in $\mathbb{R}^{50}$.
|
|
224
|
-
* `1` = State transition occurred in this tick.
|
|
225
|
-
* `0` = State remained stagnant.
|
|
226
|
-
|
|
227
|
-
React-State-Basis does not compare state values. It analyzes the timing and synchronization of updates, treating each state variable as a discrete activation signal over time.
|
|
228
|
-
|
|
229
|
-
### 3. The Analysis Layer (The Heuristic)
|
|
230
|
-
In pure Linear Algebra, proving linear independence for $N$ variables requires solving the system $a_1v_1 + \dots + a_nv_n = 0$. Using algorithms like Gaussian elimination or SVD to determine the **Rank** of the state matrix has a computational complexity of $O(N^3)$. Running this in a browser runtime for every state update would be prohibitively expensive.
|
|
231
|
-
|
|
232
|
-
To maintain real-time performance, React-State-Basis uses **Cosine Similarity** as a high-speed heuristic ($O(D)$, where $D$ is the vector dimension) to detect **pairwise collinearity**:
|
|
233
|
-
|
|
234
|
-
```math
|
|
235
|
-
\text{similarity} = \cos(\theta) = \frac{\mathbf{A} \cdot \mathbf{B}}{\Vert \mathbf{A} \Vert \Vert \mathbf{B} \Vert}
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
If $\cos(\theta) \approx 1.00$, the vectors are collinear (linearly dependent), and the engine triggers a redundancy alert.
|
|
239
|
-
|
|
240
|
-
---
|
|
241
|
-
|
|
242
|
-
## π Real-World Demonstration: The Auth Anti-Pattern
|
|
243
|
-
|
|
244
|
-
Developers often manually sync related states, creating a redundant dimension in the Basis:
|
|
245
|
-
|
|
246
|
-
### β Redundant Basis (State Bloat)
|
|
247
147
|
```tsx
|
|
248
|
-
const [
|
|
249
|
-
const [isLogged, setIsLogged] = useState(false);
|
|
250
|
-
|
|
251
|
-
const onLogin = (userData) => {
|
|
252
|
-
setUser(userData);
|
|
253
|
-
setIsLogged(true); // β οΈ BASIS ALERT: Always updated in sync with 'user'
|
|
254
|
-
};
|
|
255
|
-
```
|
|
256
|
-
**Engine Analysis:** The Engine calculates that `user` and `isLogged` are perfectly synchronized. It warns you that you are using two dimensions to describe a 1D problem.
|
|
257
|
-
|
|
258
|
-
### β
Independent Basis (Optimal Design)
|
|
259
|
-
Basis suggests a **Projection**-transforming a basis vector into a derived value.
|
|
260
|
-
```tsx
|
|
261
|
-
const [user, setUser] = useState(null);
|
|
262
|
-
const isLogged = useMemo(() => !!user, [user]); // Mathematically clean
|
|
148
|
+
const [count, setCount] = useState(0, "MyComponent -> count");
|
|
263
149
|
```
|
|
264
150
|
|
|
265
151
|
---
|
|
266
152
|
|
|
267
|
-
##
|
|
153
|
+
## High-Level Insights
|
|
268
154
|
|
|
269
|
-
|
|
155
|
+
### System Health Report
|
|
156
|
+
For a bird's-eye view of your entire application's state-space, call the global reporter in your browser console:
|
|
270
157
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
---
|
|
276
|
-
|
|
277
|
-
## β¨ Key Features
|
|
278
|
-
|
|
279
|
-
* **π΅οΈββοΈ Content-Agnostic:** Identifies logical links through temporal synchronization, not data types.
|
|
280
|
-
* **π‘οΈ Circuit Breaker:** Halts high-frequency state oscillations (Infinite Loops) to protect the browser thread.
|
|
281
|
-
* **π‘ Causal Detective:** Tracks causality chains from `useEffect` to `useState` to identify cascading renders.
|
|
282
|
-
* **π Zero Lock-in:** Simply point your imports back to `'react'` in production. Basis is a **Development-time verification infrastructure**.
|
|
283
|
-
|
|
284
|
-
---
|
|
285
|
-
|
|
286
|
-
## π Mathematical Inspiration
|
|
287
|
-
|
|
288
|
-
### π The Basis Theorem
|
|
289
|
-
According to Axler (*Linear Algebra Done Right, Definition 2.27*):
|
|
290
|
-
|
|
291
|
-
> A **basis** of $V$ is a list of vectors in $V$ that is **linearly independent** and **spans** $V$.
|
|
292
|
-
|
|
293
|
-
To satisfy this theorem in the context of application state:
|
|
294
|
-
|
|
295
|
-
1. **Linear Independence:** No state variable in the list can be expressed as a linear combination of the others. If a state $v_n$ can be derived from $\{v_1, \dots, v_{n-1}\}$, the list is linearly dependent and contains redundancy.
|
|
296
|
-
2. **Spanning the Space:** The list of state variables must contain enough information to represent every possible configuration of the user interface.
|
|
297
|
-
|
|
298
|
-
React-State-Basis ensures that your state list is a true Basis by identifying and flagging vectors that fail the test of linear independence.
|
|
299
|
-
|
|
300
|
-
> *"Linear algebra is the study of linear maps on finite-dimensional vector spaces."*
|
|
301
|
-
> - **Sheldon Axler**
|
|
302
|
-
|
|
303
|
-
React-State-Basis bridges the gap between abstract algebra and UI engineering.
|
|
304
|
-
By ensuring your application state forms an independent, non-redundant basis, it helps you build software that is inherently more stable, efficient, and easier to reason about.
|
|
305
|
-
---
|
|
306
|
-
|
|
307
|
-
### π Implementation of the Linear Dependency Lemma
|
|
308
|
-
According to Axler (*Lemma 2.21*), in a linearly dependent **list** of vectors, there exists an index $j$ such that $v_j$ is in the span of the **preceding** vectors ($v_1, \dots, v_{j-1}$).
|
|
309
|
-
|
|
310
|
-
**React-State-Basis** implements this sequential logic to audit your state:
|
|
311
|
-
|
|
312
|
-
1. **The Ordered List:** Every state variable is treated as an element in an ordered list $(v_1, v_2, \dots, v_n)$ based on its registration order in the application.
|
|
313
|
-
2. **Sequential Discovery:** As time progresses, the engine monitors the list. It doesn't just look for "similar" vectors; it looks for vectors that **fail to add a new dimension** to the subspace generated by the vectors that came before them.
|
|
314
|
-
3. **Identifying the Redundant Element:** If $v_{isLogged}$ is perfectly correlated with $v_{user}$, the engine identifies that $v_{isLogged} \in \text{span}(v_{user})$. Since $v_{user}$ preceded it, $v_{isLogged}$ is mathematically the redundant element.
|
|
315
|
-
4. **Basis Reduction:** Following the lemma's second conclusion, the engine advises removing $v_j$, proving that the "information span" of your app remains identical while the complexity of the Basis decreases.
|
|
316
|
-
|
|
317
|
-
---
|
|
318
|
-
|
|
319
|
-
## β οΈ Design Constraints & Heuristics
|
|
320
|
-
|
|
321
|
-
React-State-Basis uses probabilistic, time-windowed heuristics to approximate linear dependence.
|
|
322
|
-
As with any runtime analysis:
|
|
323
|
-
|
|
324
|
-
- Rarely-updated states may appear correlated by chance
|
|
325
|
-
- High-frequency UI interactions may trigger conservative warnings
|
|
326
|
-
- Results are advisory, not prescriptive
|
|
327
|
-
|
|
328
|
-
React-State-Basis is designed to **surface architectural questions**, not enforce correctness.
|
|
329
|
-
|
|
330
|
-
---
|
|
331
|
-
|
|
332
|
-
## β Frequently Asked Questions
|
|
333
|
-
|
|
334
|
-
### **Is React-State-Basis a replacement for React DevTools or linters?**
|
|
335
|
-
No.
|
|
336
|
-
|
|
337
|
-
React-State-Basis complements existing tools.
|
|
338
|
-
Linters analyze **code structure**, and React DevTools show **component behavior**. React-State-Basis analyzes **state relationships over time**-something neither tool is designed to detect.
|
|
339
|
-
|
|
340
|
-
It answers questions like:
|
|
341
|
-
- *βWhy do these two states always change together?β*
|
|
342
|
-
- *βWhich state is the true source of truth?β*
|
|
343
|
-
- *βAm I manually synchronizing derived data?β*
|
|
344
|
-
|
|
345
|
-
---
|
|
346
|
-
|
|
347
|
-
### **Does this change React behavior or execution order?**
|
|
348
|
-
No.
|
|
349
|
-
|
|
350
|
-
React-State-Basis **does not modify Reactβs scheduling, rendering, or reconciliation**.
|
|
351
|
-
It observes state updates at runtime and logs diagnostics during development.
|
|
352
|
-
|
|
353
|
-
Removing React-State-Basis restores your application to standard React behavior with no residual effects.
|
|
354
|
-
|
|
355
|
-
---
|
|
356
|
-
|
|
357
|
-
### **Is this safe to use in production?**
|
|
358
|
-
React-State-Basis is designed for **development-time analysis**.
|
|
359
|
-
|
|
360
|
-
While it is technically safe to run in production, it:
|
|
361
|
-
- adds runtime overhead
|
|
362
|
-
- logs diagnostic output
|
|
363
|
-
- performs continuous analysis
|
|
364
|
-
|
|
365
|
-
For production builds, simply switch your imports back to `'react'`.
|
|
366
|
-
|
|
367
|
-
---
|
|
368
|
-
|
|
369
|
-
### **How accurate is the redundancy detection?**
|
|
370
|
-
React-State-Basis uses **time-windowed behavioral analysis**, not formal proofs.
|
|
371
|
-
|
|
372
|
-
This means:
|
|
373
|
-
- Strong, consistent correlations are highly reliable indicators of redundancy
|
|
374
|
-
- Rare or coincidental correlations may trigger conservative warnings
|
|
375
|
-
|
|
376
|
-
All results are **advisory** and should be interpreted as architectural signals, not errors.
|
|
377
|
-
|
|
378
|
-
---
|
|
379
|
-
|
|
380
|
-
### **Can this detect all redundant state?**
|
|
381
|
-
No-and thatβs intentional.
|
|
382
|
-
|
|
383
|
-
React-State-Basis detects **behavioral redundancy**, not semantic equivalence.
|
|
384
|
-
Two states may contain the same *data* but update independently, which is architecturally valid.
|
|
385
|
-
|
|
386
|
-
React-State-Basis only flags redundancy when two states behave as a single information dimension over time.
|
|
387
|
-
|
|
388
|
-
---
|
|
389
|
-
|
|
390
|
-
### **Why not just use selectors or derived state manually?**
|
|
391
|
-
You should-and React-State-Basis encourages that.
|
|
392
|
-
|
|
393
|
-
The challenge is *finding* where derived state should exist in large or evolving codebases. React-State-Basis helps identify:
|
|
394
|
-
- state that should be derived
|
|
395
|
-
- state that is unintentionally synchronized
|
|
396
|
-
- state that adds no new information
|
|
397
|
-
|
|
398
|
-
It surfaces opportunities for refactoring, not rules you must follow.
|
|
399
|
-
|
|
400
|
-
---
|
|
401
|
-
|
|
402
|
-
### **Does this work with Redux, Zustand, or other state managers?**
|
|
403
|
-
React-State-Basis currently instruments **React hooks directly**.
|
|
404
|
-
|
|
405
|
-
However, the underlying model is store-agnostic. Any system with:
|
|
406
|
-
- discrete state updates
|
|
407
|
-
- identifiable update points
|
|
408
|
-
- consistent labeling
|
|
409
|
-
|
|
410
|
-
could theoretically be analyzed using the same approach.
|
|
411
|
-
|
|
412
|
-
---
|
|
413
|
-
|
|
414
|
-
### **What about performance?**
|
|
415
|
-
React-State-Basis is optimized for real-time use in development.
|
|
416
|
-
|
|
417
|
-
Key design choices:
|
|
418
|
-
- Fixed-size sliding windows
|
|
419
|
-
- O(D) similarity checks
|
|
420
|
-
- Batched analysis every N ticks
|
|
421
|
-
|
|
422
|
-
For typical applications, overhead is negligible. For extremely high-frequency updates (e.g., animations), React-State-Basis may emit conservative warnings.
|
|
158
|
+
```javascript
|
|
159
|
+
window.printBasisReport();
|
|
160
|
+
```
|
|
161
|
+
This generates a correlation matrix and calculates your **Basis Efficiency Score** in real-time.
|
|
423
162
|
|
|
424
163
|
---
|
|
164
|
+
### React Native & Expo
|
|
425
165
|
|
|
426
|
-
|
|
427
|
-
No.
|
|
428
|
-
|
|
429
|
-
React-State-Basis performs **runtime architectural auditing**, not formal mathematical verification.
|
|
430
|
-
It applies concepts from linear algebra to **observe and analyze behavior**, not to prove correctness.
|
|
166
|
+
Basis automatically detects the environment. In mobile environments, it switches to **Headless Mode**-disabling the web badge and formatting all diagnostics for the Metro terminal.
|
|
431
167
|
|
|
432
168
|
---
|
|
433
169
|
|
|
434
|
-
|
|
435
|
-
React-State-Basis is best suited for:
|
|
436
|
-
- Medium to large React applications
|
|
437
|
-
- Codebases with complex state interactions
|
|
438
|
-
- Engineers debugging synchronization bugs
|
|
439
|
-
- Teams prioritizing architectural clarity
|
|
170
|
+
## Basis vs Existing Tools
|
|
440
171
|
|
|
441
|
-
|
|
172
|
+
| Feature | React DevTools | Why Did You Render | Basis π |
|
|
173
|
+
| :--- | :---: | :---: | :---: |
|
|
174
|
+
| **Analyzes Values** | β
| β
| β (Value-agnostic) |
|
|
175
|
+
| **Tracks Timing/Ticks** | β | β | β
|
|
|
176
|
+
| **Detects Redundancy** | β | β | β
(Linear Dependence) |
|
|
177
|
+
| **Circuit Breaker** | β | β | β
(Halts petlje) |
|
|
178
|
+
| **Prod. Overhead** | Low | Medium | **Zero** (Ghost Mode) |
|
|
442
179
|
|
|
443
180
|
---
|
|
444
181
|
|
|
445
|
-
|
|
446
|
-
Because state redundancy *is* linear dependence.
|
|
182
|
+
## Key Capabilities
|
|
447
183
|
|
|
448
|
-
|
|
184
|
+
### Redundant State Detection
|
|
185
|
+
Basis monitors transition vectors to identify "Dimension Collapses." When multiple states (like `isLoading`, `isSuccess`) update in lockstep, it suggests consolidating them.
|
|
186
|
+
<br/>
|
|
187
|
+
<img src="./example/screenshots/booleanEntanglement.gif" width="700" alt="Boolean Entanglement Demo" />
|
|
449
188
|
|
|
450
|
-
|
|
189
|
+
### Causal Detective
|
|
190
|
+
Identifies "Double Render Cycles" by tracking the causality chain from `useEffect` or `useLayoutEffect` to state setters. Basis provides a direct refactor snippet to move from manual sync to a pure mathematical projection (`useMemo`).
|
|
191
|
+
<br/>
|
|
192
|
+
<img src="./example/screenshots/causal.gif" width="700" alt="Causal Link Demo" />
|
|
451
193
|
|
|
452
|
-
###
|
|
453
|
-
|
|
194
|
+
### Circuit Breaker
|
|
195
|
+
A real-time stability monitor. If high-frequency state oscillation is detected (e.g., a recursive effect), Basis forcefully halts the update chain to prevent the browser thread from locking up.
|
|
196
|
+
<br/>
|
|
197
|
+
<img src="./example/screenshots/infiniteLoopTrap.gif" width="700" alt="Circuit Breaker Demo" />
|
|
454
198
|
|
|
455
|
-
|
|
456
|
-
|
|
199
|
+
### Cross-Context Audit
|
|
200
|
+
Performs a global state space audit to expose hidden coupling between disparate parts of your architecture (e.g., syncing states across different Providers).
|
|
201
|
+
<br/>
|
|
202
|
+
<img src="./example/screenshots/initiateGlobalSync.gif" width="700" alt="Cross-Context Sync Demo" />
|
|
457
203
|
|
|
458
|
-
|
|
204
|
+
### System Health & Efficiency Audit
|
|
205
|
+
Basis performs a global audit of your state space to calculate its **Mathematical Rank**-the actual number of independent information dimensions. Use the **Efficiency Score** as a real-time KPI for your architecture.
|
|
206
|
+
<br/>
|
|
207
|
+
<img src="./example/screenshots/systemHealthReport.gif" width="700" alt="System Health Report Demo" />
|
|
459
208
|
|
|
460
209
|
---
|
|
461
210
|
|
|
462
|
-
##
|
|
463
|
-
|
|
464
|
-
React-State-Basis is evolving from a runtime auditor to a complete development infrastructure. Here is the planned trajectory:
|
|
211
|
+
## Roadmap
|
|
465
212
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
* **Signature Robustness:** Smart disambiguation between dependency arrays and manual labels.
|
|
213
|
+
#### **v0.2.x - Stability & Universal Support (Current)** β
|
|
214
|
+
- [x] Full React Hook API parity & React Native support.
|
|
215
|
+
- [x] Memory-optimized "Ghost Registration" logic.
|
|
216
|
+
- [x] 95% Test Coverage verified engine.
|
|
471
217
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
### **v0.4.0 - Developer Ecosystem & Visuals**
|
|
478
|
-
Tools for better ergonomics and high-level insights.
|
|
479
|
-
* **CLI Utilities (`rsb-init`, `rsb-clean`):** Automated codemods to instantly inject or remove Basis from large codebases. No more manual search-and-replace.
|
|
480
|
-
* **State-Space Visualizer:** A 2D topology map showing "Redundancy Clusters." Visualize your state vectors as physical nodes to identify where the architecture is collapsing.
|
|
481
|
-
|
|
482
|
-
### **v1.0.0 - Formal Verification**
|
|
483
|
-
* **Architectural Gatekeeping:** CI/CD integration to fail builds on infinite loops or critical dimension collapses.
|
|
484
|
-
* **KPI Tracking:** Long-term monitoring of your applicationβs **System Efficiency Score**.
|
|
218
|
+
#### **v0.3.0 - Visuals & React 19** π
|
|
219
|
+
- [ ] **State-Space Visualizer:** 2D topology map showing redundancy clusters.
|
|
220
|
+
- [ ] **CLI Utilities:** `rsb-init` and `rsb-clean` for automated codemods.
|
|
221
|
+
- [ ] **React 19 Native Support:** `use()`, `useOptimistic()`, `useActionState()`.
|
|
485
222
|
|
|
486
223
|
---
|
|
487
224
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
---
|
|
492
|
-
*Developed by LP*
|
|
493
|
-
*For engineers who treat software as applied mathematics.* ππ
|
|
225
|
+
<div align="center">
|
|
226
|
+
Developed by LP | For engineers who treat software as applied mathematics. π
|
|
227
|
+
</div>
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import
|
|
1
|
+
import * as React$1 from 'react';
|
|
2
|
+
import React__default, { Dispatch, SetStateAction, Reducer, DependencyList, EffectCallback, Context, ReactNode } from 'react';
|
|
3
3
|
export { CSSProperties, Context, DependencyList, Dispatch, EffectCallback, FC, PropsWithChildren, ReactElement, ReactNode, Reducer, SetStateAction } from 'react';
|
|
4
4
|
|
|
5
|
+
interface BasisConfig {
|
|
6
|
+
debug: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare let config: BasisConfig;
|
|
9
|
+
declare const configureBasis: (newConfig: Partial<BasisConfig>) => void;
|
|
5
10
|
declare const history: Map<string, number[]>;
|
|
6
11
|
declare const currentTickBatch: Set<string>;
|
|
7
12
|
declare const printBasisHealthReport: (threshold?: number) => void;
|
|
@@ -11,6 +16,8 @@ declare const registerVariable: (label: string) => void;
|
|
|
11
16
|
declare const unregisterVariable: (label: string) => void;
|
|
12
17
|
declare const recordUpdate: (label: string) => boolean;
|
|
13
18
|
declare const __testEngine__: {
|
|
19
|
+
config: BasisConfig;
|
|
20
|
+
configureBasis: (newConfig: Partial<BasisConfig>) => void;
|
|
14
21
|
history: Map<string, number[]>;
|
|
15
22
|
currentTickBatch: Set<string>;
|
|
16
23
|
registerVariable: (label: string) => void;
|
|
@@ -21,20 +28,20 @@ declare const __testEngine__: {
|
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
declare function useState<T>(initialValue: T, label?: string): [T, Dispatch<SetStateAction<T>>];
|
|
24
|
-
declare function useMemo<T>(factory: () => T, depsOrLabel?: DependencyList | string, label?: string): T;
|
|
25
|
-
declare function useEffect(effect: EffectCallback, depsOrLabel?: DependencyList | string, label?: string): void;
|
|
26
31
|
declare function useReducer<S, A, I>(reducer: Reducer<S, A>, initialArg: I & S, init?: any, label?: string): [S, Dispatch<A>];
|
|
27
|
-
declare function
|
|
32
|
+
declare function useMemo<T>(factory: () => T, depsOrLabel?: DependencyList | string, label?: string): T;
|
|
28
33
|
declare function useCallback<T extends (...args: any[]) => any>(callback: T, depsOrLabel?: DependencyList | string, label?: string): T;
|
|
29
|
-
declare function
|
|
34
|
+
declare function useEffect(effect: EffectCallback, depsOrLabel?: DependencyList | string, label?: string): void;
|
|
30
35
|
declare function useLayoutEffect(effect: EffectCallback, depsOrLabel?: DependencyList | string, label?: string): void;
|
|
36
|
+
declare function useInsertionEffect(effect: EffectCallback, deps?: DependencyList, _label?: string): void;
|
|
37
|
+
declare function useTransition(_label?: string): [boolean, (callback: () => void) => void];
|
|
38
|
+
declare function useDeferredValue<T>(value: T, initialValueOrLabel?: T | string, label?: string): T;
|
|
39
|
+
declare function useRef<T>(initialValue: T, _label?: string): React$1.RefObject<T>;
|
|
40
|
+
declare function createContext<T>(defaultValue: T, label?: string): Context<T>;
|
|
31
41
|
declare function useContext<T>(context: Context<T>): T;
|
|
32
42
|
declare function useId(_label?: string): string;
|
|
33
43
|
declare function useDebugValue<T>(value: T, formatter?: (value: T) => any, _label?: string): void;
|
|
34
44
|
declare function useImperativeHandle<T, R extends T>(ref: React.Ref<T> | undefined, init: () => R, deps?: DependencyList, _label?: string): void;
|
|
35
|
-
declare function useInsertionEffect(effect: EffectCallback, deps?: DependencyList, _label?: string): void;
|
|
36
|
-
declare function useTransition(_label?: string): [boolean, (callback: () => void) => void];
|
|
37
|
-
declare function useDeferredValue<T>(value: T, initialValueOrLabel?: T | string, label?: string): T;
|
|
38
45
|
declare function useSyncExternalStore<Snapshot>(subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => Snapshot, getServerSnapshot?: () => Snapshot, _label?: string): Snapshot;
|
|
39
46
|
declare const __test__: {
|
|
40
47
|
registerVariable: (label: string) => void;
|
|
@@ -50,9 +57,9 @@ interface BasisProviderProps {
|
|
|
50
57
|
children: ReactNode;
|
|
51
58
|
debug?: boolean;
|
|
52
59
|
}
|
|
53
|
-
declare const BasisProvider:
|
|
60
|
+
declare const BasisProvider: React__default.FC<BasisProviderProps>;
|
|
54
61
|
declare const useBasisConfig: () => {
|
|
55
62
|
debug: boolean;
|
|
56
63
|
};
|
|
57
64
|
|
|
58
|
-
export { BasisProvider, __testEngine__, __test__, beginEffectTracking, createContext, currentTickBatch, endEffectTracking, history, printBasisHealthReport, recordUpdate, registerVariable, unregisterVariable, useBasisConfig, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition };
|
|
65
|
+
export { type BasisConfig, BasisProvider, __testEngine__, __test__, beginEffectTracking, config, configureBasis, createContext, currentTickBatch, endEffectTracking, history, printBasisHealthReport, recordUpdate, registerVariable, unregisterVariable, useBasisConfig, useCallback, useContext, useDebugValue, useDeferredValue, useEffect, useId, useImperativeHandle, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore, useTransition };
|