@zeix/cause-effect 0.15.0 → 0.15.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 +40 -10
- package/index.dev.js +508 -382
- package/index.js +1 -1
- package/index.ts +26 -5
- package/package.json +1 -1
- package/src/computed.ts +2 -2
- package/src/diff.ts +70 -45
- package/src/effect.ts +3 -8
- package/src/errors.ts +56 -0
- package/src/match.ts +14 -19
- package/src/resolve.ts +8 -18
- package/src/signal.ts +38 -46
- package/src/state.ts +3 -2
- package/src/store.ts +410 -188
- package/src/util.ts +62 -20
- package/test/computed.test.ts +1 -1
- package/test/diff.test.ts +321 -4
- package/test/effect.test.ts +1 -1
- package/test/match.test.ts +13 -3
- package/test/signal.test.ts +323 -0
- package/test/store.test.ts +971 -1
- package/types/index.d.ts +6 -5
- package/types/src/diff.d.ts +8 -3
- package/types/src/errors.d.ts +19 -0
- package/types/src/match.d.ts +4 -4
- package/types/src/resolve.d.ts +3 -3
- package/types/src/signal.d.ts +15 -18
- package/types/src/store.d.ts +56 -33
- package/types/src/util.d.ts +9 -7
- package/index.d.ts +0 -36
- package/types/test-new-effect.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Cause & Effect
|
|
2
2
|
|
|
3
|
-
Version 0.15.
|
|
3
|
+
Version 0.15.2
|
|
4
4
|
|
|
5
5
|
**Cause & Effect** is a lightweight, reactive state management library for JavaScript applications. It uses fine-grained reactivity with signals to create predictable and efficient data flow in your app.
|
|
6
6
|
|
|
@@ -23,7 +23,7 @@ Version 0.15.0
|
|
|
23
23
|
- 🛡️ **Error Handling**: Built-in helper functions for declarative error handling
|
|
24
24
|
- 🔧 **Helper Functions**: `resolve()` and `match()` for type-safe value extraction and pattern matching for suspense and error boundaries
|
|
25
25
|
- 🚀 **Performance**: Batching and efficient dependency tracking
|
|
26
|
-
- 📦 **Tiny**: Less than 3kB gzipped, zero dependencies
|
|
26
|
+
- 📦 **Tiny**: Less than 3kB gzipped, tree-shakable, zero dependencies
|
|
27
27
|
|
|
28
28
|
## Quick Start
|
|
29
29
|
|
|
@@ -140,6 +140,37 @@ The `add()` and `remove()` methods are optimized for performance:
|
|
|
140
140
|
- They're perfect for frequent single-property additions/removals
|
|
141
141
|
- They trigger the same events and reactivity as other store operations
|
|
142
142
|
|
|
143
|
+
#### Array-like Stores
|
|
144
|
+
|
|
145
|
+
Stores created from arrays behave like arrays with reactive properties. They support duck-typing with length property, single-parameter `add()`, and efficient sorting:
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import { store, effect } from '@zeix/cause-effect'
|
|
149
|
+
|
|
150
|
+
const items = store(['banana', 'apple', 'cherry'])
|
|
151
|
+
|
|
152
|
+
// Duck-typing: behaves like an array
|
|
153
|
+
console.log(items.length) // 3
|
|
154
|
+
console.log(typeof items.length) // 'number'
|
|
155
|
+
|
|
156
|
+
// Individual items are reactive
|
|
157
|
+
effect(() => {
|
|
158
|
+
console.log(`First item: ${items[0].get()}`)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
// Single-parameter add() appends to end
|
|
162
|
+
items.add('date') // Adds at index 3
|
|
163
|
+
console.log(items.get()) // ['banana', 'apple', 'cherry', 'date']
|
|
164
|
+
|
|
165
|
+
// Efficient sorting preserves signal references
|
|
166
|
+
items.sort() // Default: string comparison
|
|
167
|
+
console.log(items.get()) // ['apple', 'banana', 'cherry', 'date']
|
|
168
|
+
|
|
169
|
+
// Custom sorting
|
|
170
|
+
items.sort((a, b) => b.localeCompare(a)) // Reverse alphabetical
|
|
171
|
+
console.log(items.get()) // ['date', 'cherry', 'banana', 'apple']
|
|
172
|
+
```
|
|
173
|
+
|
|
143
174
|
#### Store Events
|
|
144
175
|
|
|
145
176
|
Stores emit events when properties are added, changed, or removed. You can listen to these events using standard `addEventListener()`:
|
|
@@ -168,6 +199,13 @@ user.addEventListener('store-remove', (event) => {
|
|
|
168
199
|
user.add('email', 'alice@example.com') // Logs: "Added properties: { email: 'alice@example.com' }"
|
|
169
200
|
user.age.set(31) // Logs: "Changed properties: { age: 31 }"
|
|
170
201
|
user.remove('email') // Logs: "Removed properties: { email: UNSET }"
|
|
202
|
+
|
|
203
|
+
// Listen for sort events (useful for UI animations)
|
|
204
|
+
const items = store(['banana', 'apple', 'cherry'])
|
|
205
|
+
items.sort((a, b) => b.localeCompare(a)) // Reverse alphabetical
|
|
206
|
+
items.addEventListener('store-sort', (event) => {
|
|
207
|
+
console.log('Items reordered:', event.detail) // ['2', '1', '0']
|
|
208
|
+
})
|
|
171
209
|
```
|
|
172
210
|
|
|
173
211
|
Events are also fired when using `set()` or `update()` methods on the entire store:
|
|
@@ -445,14 +483,6 @@ document.querySelector('.double-all').addEventListener('click', () => {
|
|
|
445
483
|
signals[0].set(NaN)
|
|
446
484
|
```
|
|
447
485
|
|
|
448
|
-
The Cause & Effect library is designed around these principles:
|
|
449
|
-
|
|
450
|
-
- **Minimal API**: Core primitives with a small but powerful interface
|
|
451
|
-
- **Automatic Dependency Tracking**: Fine-grained reactivity with minimal boilerplate
|
|
452
|
-
- **Performance-Focused**: Choose the right tool (functions vs computed) for optimal speed
|
|
453
|
-
- **Tree-Shakable**: Import only what you need for optimal bundle size
|
|
454
|
-
- **Flexible Integration**: Works with any JavaScript application or framework
|
|
455
|
-
|
|
456
486
|
### Cleanup Functions
|
|
457
487
|
|
|
458
488
|
Effects return a cleanup function. When executed, it will unsubscribe from signals and run cleanup functions returned by effect callbacks, for example to remove event listeners.
|