@biglogic/rgs 3.7.6 → 3.7.8

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.
@@ -1,69 +1,69 @@
1
- # 🛒 Chapter 6: Case Studies - Real Strategies
2
-
3
- In this chapter, we put theory aside and see how RGS solves the problems that keep you up at night (or at least frustrated in the office).
4
-
5
- ---
6
-
7
- ## 🍎 Case 1: High-Performance E-commerce
8
-
9
- **The Problem**: A shopping cart with 50 items, complex sidebar filters, and a product list that must update without "jumping" the whole page.
10
-
11
- **The RGS Strategy**:
12
-
13
- 1. **Atomic Filters**: Don't save the entire filters object. Use separate keys (`category`, `priceRange`, `search`). This way, if the user only changes the price, the search bar doesn't re-render.
14
- 2. **Persistent Cart**: Use `gstate` with a `cart` namespace.
15
- 3. **Computed State for Totals**:
16
-
17
- ```javascript
18
- cartStore.compute('totalAmount', ['items'], (s) =>
19
- s.items.reduce((acc, curr) => acc + curr.price, 0)
20
- );
21
- ```
22
-
23
- **Why do this?** Because the component displaying the total price updates *only* when the `items` array changes, not when the user's name or shipping address changes. **Zero waste.**
24
-
25
- ---
26
-
27
- ## 📊 Case 2: Real-time Dashboard (Sockets/Events)
28
-
29
- **The Problem**: You receive thousands of updates via WebSocket (e.g., crypto prices or server notifications), and React can't keep up.
30
-
31
- **The RGS Strategy**:
32
-
33
- 1. **Atomic Transactions**: In RGS, you can group updates.
34
-
35
- ```javascript
36
- socket.on('bulk_update', (data) => {
37
- store.transaction(() => {
38
- data.forEach(item => store.set(`price_${item.id}`, item.price));
39
- });
40
- });
41
- ```
42
-
43
- **Why do this?** Instead of triggering 100 React updates, the `transaction` triggers **only one** at the end. Your dashboard's performance will go from "tractor" to "Ferrari".
44
-
45
- ---
46
-
47
- ## 🏦 Case 3: Multi-Step Forms (User Onboarding)
48
-
49
- **The Problem**: A signup form with 5 steps. If the user hits "Back" or refreshes the page, they lose everything.
50
-
51
- **The RGS Strategy**:
52
-
53
- 1. Use a dedicated `gstate` called `onboarding`.
54
- 2. Enable `persist: true`.
55
- 3. At each step, just call `set('step1', values)`.
56
- **Why do this?** Because you don't have to manage manual saving logic. When the user returns, the fields are already populated. At the very end (Step 5), call `store.destroy()` to clean up. Clean and elegant.
57
-
58
- ---
59
-
60
- ## 🛡️ Message for Advanced Architects
61
-
62
- *"But I could do all this with a custom cache and an event bus..."*
63
- Sure you could. You could also walk to work instead of driving. But RGS is the car: it's tested, it handles edge cases (closed tabs, full storage, corrupted types), and it lets you focus on **business logic**, not infrastructure.
64
-
65
- Stop reinventing the wheel. Use RGS.
66
-
67
- ---
68
-
69
- **Next step:** [FAQ: For the Skeptics and the Curious](07-faq.md)
1
+ # 🛒 Chapter 6: Case Studies - Real Strategies
2
+
3
+ In this chapter, we put theory aside and see how RGS solves the problems that keep you up at night (or at least frustrated in the office).
4
+
5
+ ---
6
+
7
+ ## 🍎 Case 1: High-Performance E-commerce
8
+
9
+ **The Problem**: A shopping cart with 50 items, complex sidebar filters, and a product list that must update without "jumping" the whole page.
10
+
11
+ **The RGS Strategy**:
12
+
13
+ 1. **Atomic Filters**: Don't save the entire filters object. Use separate keys (`category`, `priceRange`, `search`). This way, if the user only changes the price, the search bar doesn't re-render.
14
+ 2. **Persistent Cart**: Use `gstate` with a `cart` namespace.
15
+ 3. **Computed State for Totals**:
16
+
17
+ ```javascript
18
+ cartStore.compute('totalAmount', ['items'], (s) =>
19
+ s.items.reduce((acc, curr) => acc + curr.price, 0)
20
+ );
21
+ ```
22
+
23
+ **Why do this?** Because the component displaying the total price updates *only* when the `items` array changes, not when the user's name or shipping address changes. **Zero waste.**
24
+
25
+ ---
26
+
27
+ ## 📊 Case 2: Real-time Dashboard (Sockets/Events)
28
+
29
+ **The Problem**: You receive thousands of updates via WebSocket (e.g., crypto prices or server notifications), and React can't keep up.
30
+
31
+ **The RGS Strategy**:
32
+
33
+ 1. **Atomic Transactions**: In RGS, you can group updates.
34
+
35
+ ```javascript
36
+ socket.on('bulk_update', (data) => {
37
+ store.transaction(() => {
38
+ data.forEach(item => store.set(`price_${item.id}`, item.price));
39
+ });
40
+ });
41
+ ```
42
+
43
+ **Why do this?** Instead of triggering 100 React updates, the `transaction` triggers **only one** at the end. Your dashboard's performance will go from "tractor" to "Ferrari".
44
+
45
+ ---
46
+
47
+ ## 🏦 Case 3: Multi-Step Forms (User Onboarding)
48
+
49
+ **The Problem**: A signup form with 5 steps. If the user hits "Back" or refreshes the page, they lose everything.
50
+
51
+ **The RGS Strategy**:
52
+
53
+ 1. Use a dedicated `gstate` called `onboarding`.
54
+ 2. Enable `persist: true`.
55
+ 3. At each step, just call `set('step1', values)`.
56
+ **Why do this?** Because you don't have to manage manual saving logic. When the user returns, the fields are already populated. At the very end (Step 5), call `store.destroy()` to clean up. Clean and elegant.
57
+
58
+ ---
59
+
60
+ ## 🛡️ Message for Advanced Architects
61
+
62
+ *"But I could do all this with a custom cache and an event bus..."*
63
+ Sure you could. You could also walk to work instead of driving. But RGS is the car: it's tested, it handles edge cases (closed tabs, full storage, corrupted types), and it lets you focus on **business logic**, not infrastructure.
64
+
65
+ Stop reinventing the wheel. Use RGS.
66
+
67
+ ---
68
+
69
+ **Next step:** [FAQ: For the Skeptics and the Curious](07-faq.md)