cogsbox-state 0.5.8 → 0.5.9
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 +74 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Cogsbox State: A Practical Guide
|
|
2
2
|
|
|
3
|
+
> **⚠️ WARNING**: This README is AI-generated based on the current implementation of Cogsbox State. The library is under active development and APIs are subject to change. Always refer to the official documentation or source code for the most up-to-date information.
|
|
4
|
+
|
|
3
5
|
## Getting Started
|
|
4
6
|
|
|
5
7
|
Cogsbox State is a React state management library that provides a fluent interface for managing complex state.
|
|
@@ -25,16 +27,16 @@ export const { useCogsState } = createCogsState(InitialState);
|
|
|
25
27
|
|
|
26
28
|
// 3. Use in your component
|
|
27
29
|
function MyComponent() {
|
|
28
|
-
const
|
|
30
|
+
const cart = useCogsState("cart");
|
|
29
31
|
|
|
30
32
|
// Access values
|
|
31
|
-
const cartItems =
|
|
32
|
-
const total =
|
|
33
|
+
const cartItems = cart.items.get();
|
|
34
|
+
const total = cart.total.get();
|
|
33
35
|
|
|
34
36
|
// Update values
|
|
35
37
|
const addItem = (item) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
cart.items.insert(item);
|
|
39
|
+
cart.total.update(total + item.price);
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
return (
|
|
@@ -49,26 +51,26 @@ function MyComponent() {
|
|
|
49
51
|
|
|
50
52
|
```typescript
|
|
51
53
|
// Get the entire state object
|
|
52
|
-
const entireCart =
|
|
54
|
+
const entireCart = cart.get();
|
|
53
55
|
|
|
54
56
|
// Access a specific property
|
|
55
|
-
const cartItems =
|
|
57
|
+
const cartItems = cart.items.get();
|
|
56
58
|
|
|
57
59
|
// Access nested properties
|
|
58
|
-
const firstItemPrice =
|
|
60
|
+
const firstItemPrice = cart.items.index(0).price.get();
|
|
59
61
|
```
|
|
60
62
|
|
|
61
63
|
### Updating State
|
|
62
64
|
|
|
63
65
|
```typescript
|
|
64
66
|
// Direct update
|
|
65
|
-
|
|
67
|
+
cart.settings.darkMode.update(true);
|
|
66
68
|
|
|
67
69
|
// Functional update (based on previous value)
|
|
68
|
-
|
|
70
|
+
cart.cart.total.update((prev) => prev + 10);
|
|
69
71
|
|
|
70
72
|
// Deep update
|
|
71
|
-
|
|
73
|
+
cart.users.findWith("id", "123").name.update("New Name");
|
|
72
74
|
```
|
|
73
75
|
|
|
74
76
|
## Working with Arrays
|
|
@@ -77,23 +79,23 @@ updater.users.findWith("id", "123").name.update("New Name");
|
|
|
77
79
|
|
|
78
80
|
```typescript
|
|
79
81
|
// Add an item
|
|
80
|
-
|
|
82
|
+
cart.cart.items.insert({ id: "prod1", name: "Product 1", price: 29.99 });
|
|
81
83
|
|
|
82
84
|
// Remove an item at index
|
|
83
|
-
|
|
85
|
+
cart.cart.items.cut(2);
|
|
84
86
|
|
|
85
87
|
// Find and update an item
|
|
86
|
-
|
|
88
|
+
cart.cart.items.findWith("id", "prod1").quantity.update((prev) => prev + 1);
|
|
87
89
|
|
|
88
90
|
// Update item at specific index
|
|
89
|
-
|
|
91
|
+
cart.cart.items.index(0).price.update(19.99);
|
|
90
92
|
```
|
|
91
93
|
|
|
92
94
|
### Advanced Array Methods
|
|
93
95
|
|
|
94
96
|
```typescript
|
|
95
97
|
// Map with access to updaters
|
|
96
|
-
|
|
98
|
+
cart.cart.items.stateMap((item, itemUpdater) => (
|
|
97
99
|
<CartItem
|
|
98
100
|
key={item.id}
|
|
99
101
|
item={item}
|
|
@@ -102,16 +104,16 @@ updater.cart.items.stateMap((item, itemUpdater) => (
|
|
|
102
104
|
));
|
|
103
105
|
|
|
104
106
|
// Filter items while maintaining updater capabilities
|
|
105
|
-
const inStockItems =
|
|
107
|
+
const inStockItems = cart.products.stateFilter(product => product.stock > 0);
|
|
106
108
|
|
|
107
109
|
// Insert only if the item doesn't exist
|
|
108
|
-
|
|
110
|
+
cart.cart.items.uniqueInsert(
|
|
109
111
|
{ id: "prod1", quantity: 1 },
|
|
110
112
|
["id"] // fields to check for uniqueness
|
|
111
113
|
);
|
|
112
114
|
|
|
113
115
|
// Flatten nested arrays by property
|
|
114
|
-
const allVariants =
|
|
116
|
+
const allVariants = cart.products.stateFlattenOn("variants");
|
|
115
117
|
```
|
|
116
118
|
|
|
117
119
|
## Reactivity Control
|
|
@@ -463,7 +465,7 @@ const products = useCogsState("products", {
|
|
|
463
465
|
});
|
|
464
466
|
|
|
465
467
|
// State is automatically synced with server after changes
|
|
466
|
-
products.items
|
|
468
|
+
products.items.index(0).stock.update((prev) => prev - 1);
|
|
467
469
|
```
|
|
468
470
|
|
|
469
471
|
## Local Storage Persistence
|
|
@@ -539,42 +541,88 @@ function ShoppingCart() {
|
|
|
539
541
|
}
|
|
540
542
|
```
|
|
541
543
|
|
|
544
|
+
## Session Support
|
|
545
|
+
|
|
546
|
+
Cogsbox State supports session-based state management through the `useCogsConfig` hook, allowing you to isolate state for different user sessions:
|
|
547
|
+
|
|
548
|
+
```typescript
|
|
549
|
+
// Using session-specific state
|
|
550
|
+
const cart = useCogsState("cart", {
|
|
551
|
+
localStorageKey: "user-cart", // Will be prefixed with sessionId
|
|
552
|
+
initState: {
|
|
553
|
+
initialState: {
|
|
554
|
+
items: [],
|
|
555
|
+
total: 0,
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
});
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
## Performance Optimizations
|
|
562
|
+
|
|
563
|
+
The library includes several performance optimizations:
|
|
564
|
+
|
|
565
|
+
1. **Cache Management**: Cogsbox maintains a cache of proxy objects to reduce re-creation overhead.
|
|
566
|
+
2. **Batched Updates**: State updates are batched where possible to minimize render cycles.
|
|
567
|
+
3. **Signal-based DOM Updates**: Directly update DOM elements without re-rendering components.
|
|
568
|
+
|
|
542
569
|
## Common Patterns and Tips
|
|
543
570
|
|
|
544
571
|
1. **Path-based Updates**: Always use the fluent API to update nested properties.
|
|
545
572
|
|
|
546
573
|
```typescript
|
|
547
574
|
// Good
|
|
548
|
-
|
|
575
|
+
user.users.index(0).address.city.update("New York");
|
|
549
576
|
|
|
550
577
|
// Avoid
|
|
551
|
-
|
|
578
|
+
user.update({ ...state, users: [...] });
|
|
552
579
|
```
|
|
553
580
|
|
|
554
581
|
2. **Working with Arrays**: Use the built-in array methods instead of manually updating array state.
|
|
555
582
|
|
|
556
583
|
```typescript
|
|
557
584
|
// Good
|
|
558
|
-
|
|
559
|
-
|
|
585
|
+
user.users.insert(newUser);
|
|
586
|
+
user.users.findWith("id", 123).active.update(true);
|
|
560
587
|
|
|
561
588
|
// Avoid
|
|
562
|
-
|
|
589
|
+
user.users.update([...users, newUser]);
|
|
563
590
|
```
|
|
564
591
|
|
|
565
592
|
3. **Optimization**: Use the appropriate reactivity type for your needs.
|
|
566
593
|
|
|
567
594
|
```typescript
|
|
568
595
|
// For lists where only specific items change frequently
|
|
569
|
-
|
|
596
|
+
user.items.stateMap((item) => (
|
|
570
597
|
<div>{item.$get()}</div> // Only this item re-renders when changed
|
|
571
598
|
));
|
|
572
599
|
```
|
|
573
600
|
|
|
574
601
|
4. **Form Management**: Use formElement for all form inputs to get automatic validation and debouncing.
|
|
602
|
+
|
|
575
603
|
```typescript
|
|
576
604
|
profile.name.formElement(
|
|
577
605
|
({ inputProps }) => <input {...inputProps} />,
|
|
578
606
|
{ debounceTime: 300 }
|
|
579
607
|
);
|
|
580
608
|
```
|
|
609
|
+
|
|
610
|
+
5. **Middleware Support**: Add middleware for logging, analytics, or custom state processing:
|
|
611
|
+
|
|
612
|
+
```typescript
|
|
613
|
+
const user = useCogsState("user", {
|
|
614
|
+
middleware: ({ update, updateLog }) => {
|
|
615
|
+
// Log all state changes
|
|
616
|
+
console.log("State update:", update);
|
|
617
|
+
|
|
618
|
+
// Trigger analytics
|
|
619
|
+
if (update.path.includes("preferences")) {
|
|
620
|
+
analytics.track("preferences_changed", update.newValue);
|
|
621
|
+
}
|
|
622
|
+
},
|
|
623
|
+
});
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
## API Reference
|
|
627
|
+
|
|
628
|
+
For a comprehensive API reference, see the TypeScript interfaces and examples in the source code.
|