@zeix/cause-effect 0.15.1 → 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 +39 -1
- package/index.dev.js +517 -392
- package/index.js +1 -1
- package/index.ts +15 -4
- package/package.json +1 -1
- package/src/computed.ts +2 -2
- package/src/diff.ts +57 -44
- package/src/effect.ts +2 -6
- package/src/errors.ts +56 -0
- package/src/match.ts +2 -2
- package/src/resolve.ts +2 -2
- package/src/signal.ts +21 -43
- package/src/state.ts +3 -2
- package/src/store.ts +402 -179
- package/src/util.ts +50 -6
- 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 +12 -140
- package/test/store.test.ts +963 -20
- package/types/index.d.ts +5 -4
- package/types/src/diff.d.ts +5 -3
- package/types/src/errors.d.ts +19 -0
- package/types/src/match.d.ts +1 -1
- package/types/src/resolve.d.ts +1 -1
- package/types/src/signal.d.ts +12 -19
- package/types/src/store.d.ts +48 -30
- package/types/src/util.d.ts +8 -5
- 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
|
|
|
@@ -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:
|