@zeix/cause-effect 0.18.0 → 0.18.1
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/.ai-context.md +14 -3
- package/.github/copilot-instructions.md +15 -5
- package/ARCHITECTURE.md +15 -13
- package/CHANGELOG.md +29 -0
- package/CLAUDE.md +9 -7
- package/README.md +23 -5
- package/context7.json +4 -0
- package/examples/events-sensor.ts +187 -0
- package/examples/selector-sensor.ts +173 -0
- package/index.dev.js +276 -222
- package/index.js +1 -1
- package/index.ts +4 -2
- package/package.json +2 -2
- package/skills/changelog-keeper/SKILL.md +59 -0
- package/skills/changelog-keeper/agents/openai.yaml +4 -0
- package/src/graph.ts +13 -2
- package/src/nodes/collection.ts +166 -128
- package/src/nodes/list.ts +105 -104
- package/src/nodes/memo.ts +31 -3
- package/src/nodes/sensor.ts +27 -17
- package/src/nodes/state.ts +2 -2
- package/src/nodes/store.ts +55 -60
- package/src/nodes/task.ts +31 -3
- package/test/collection.test.ts +40 -51
- package/test/memo.test.ts +194 -0
- package/test/task.test.ts +134 -0
- package/types/index.d.ts +5 -5
- package/types/src/graph.d.ts +12 -2
- package/types/src/nodes/collection.d.ts +12 -7
- package/types/src/nodes/list.d.ts +12 -11
- package/types/src/nodes/memo.d.ts +6 -0
- package/types/src/nodes/sensor.d.ts +15 -9
- package/types/src/nodes/store.d.ts +4 -4
- package/types/src/nodes/task.d.ts +6 -0
- package/COLLECTION_REFACTORING.md +0 -161
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
# Collection Refactoring Plan
|
|
2
|
-
|
|
3
|
-
## Goal
|
|
4
|
-
|
|
5
|
-
Unify `createCollection()` and `createSourceCollection()` into a single `createCollection()` primitive whose primary form mirrors `createSensor()`: an externally-driven signal with a watched lifecycle. The derived-from-List/Collection form becomes an internal helper used by `.deriveCollection()`.
|
|
6
|
-
|
|
7
|
-
## Motivation
|
|
8
|
-
|
|
9
|
-
- **Sensor ↔ Collection parallel**: Both are externally-driven, lazily activated, and auto-cleaned. Making their signatures parallel sharpens this mental model.
|
|
10
|
-
- **One primitive, one name**: Users learn `createCollection(start, options)` the same way they learn `createSensor(start, options)`.
|
|
11
|
-
- **Derived collections are a method, not a standalone call**: `list.deriveCollection(fn)` and `collection.deriveCollection(fn)` already exist and are the natural way to create derived collections.
|
|
12
|
-
|
|
13
|
-
## New API Surface
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
// Primary form — externally driven (replaces createSourceCollection)
|
|
17
|
-
function createCollection<T extends {}>(
|
|
18
|
-
start: CollectionCallback<T>,
|
|
19
|
-
options?: CollectionOptions<T>,
|
|
20
|
-
): Collection<T>
|
|
21
|
-
|
|
22
|
-
// CollectionCallback mirrors SensorCallback but receives applyChanges
|
|
23
|
-
type CollectionCallback<T extends {}> = (
|
|
24
|
-
applyChanges: (changes: DiffResult) => void,
|
|
25
|
-
) => Cleanup
|
|
26
|
-
|
|
27
|
-
// CollectionOptions — initial value hidden in options (like Memo, Task, Sensor)
|
|
28
|
-
type CollectionOptions<T extends {}> = {
|
|
29
|
-
value?: T[] // initial items (default: [])
|
|
30
|
-
keyConfig?: KeyConfig<T> // key generation strategy
|
|
31
|
-
createItem?: (key: string, value: T) => Signal<T> // custom item factory
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Derive method — unchanged on List and Collection
|
|
35
|
-
collection.deriveCollection(callback)
|
|
36
|
-
list.deriveCollection(callback)
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Refactoring Steps
|
|
40
|
-
|
|
41
|
-
Order matters: the existing `createCollection` and `CollectionCallback` names must be freed up before they can be reused for the new concept. The refactoring proceeds in two phases.
|
|
42
|
-
|
|
43
|
-
### Phase 1 — Rename existing symbols (free the names)
|
|
44
|
-
|
|
45
|
-
#### 1.1. Rename `createCollection` → `deriveCollection`
|
|
46
|
-
|
|
47
|
-
In `src/nodes/collection.ts`:
|
|
48
|
-
|
|
49
|
-
- Rename the function `createCollection(source, callback)` → `deriveCollection(source, callback)`.
|
|
50
|
-
- Update both overload signatures and the implementation signature.
|
|
51
|
-
- Update the internal `deriveCollection()` call inside the `Collection.deriveCollection` method body (both in the derived-collection object and the source-collection object).
|
|
52
|
-
|
|
53
|
-
#### 1.2. Rename `CollectionCallback<T, U>` → `DeriveCollectionCallback<T, U>`
|
|
54
|
-
|
|
55
|
-
- Rename the type alias in `src/nodes/collection.ts`.
|
|
56
|
-
- Update all references: the `deriveCollection` parameter types, and the `Collection.deriveCollection` method parameter type annotations.
|
|
57
|
-
|
|
58
|
-
#### 1.3. Update `list.ts`
|
|
59
|
-
|
|
60
|
-
- Change the import from `createCollection` to `deriveCollection`.
|
|
61
|
-
- Update `List.deriveCollection()` body to call `deriveCollection(list, cb)`.
|
|
62
|
-
|
|
63
|
-
#### 1.4. Update exports in `index.ts`
|
|
64
|
-
|
|
65
|
-
- Replace `createCollection` → `deriveCollection` in the export list.
|
|
66
|
-
- Replace `CollectionCallback` → `DeriveCollectionCallback`.
|
|
67
|
-
- Keep or drop `CollectionSource` from public exports (internal detail of `deriveCollection`).
|
|
68
|
-
|
|
69
|
-
#### 1.5. Update tests
|
|
70
|
-
|
|
71
|
-
- In `test/collection.test.ts` (or `test/collection.next.test.ts`): replace all direct `createCollection(source, cb)` calls with either `deriveCollection(source, cb)` or the equivalent `.deriveCollection(cb)` method.
|
|
72
|
-
- Update imports accordingly.
|
|
73
|
-
|
|
74
|
-
#### 1.6. Verify
|
|
75
|
-
|
|
76
|
-
- `bun run check` and `bun test` pass.
|
|
77
|
-
- Commit: "Rename createCollection → deriveCollection, CollectionCallback → DeriveCollectionCallback"
|
|
78
|
-
|
|
79
|
-
### Phase 2 — Reshape `createSourceCollection` → `createCollection`
|
|
80
|
-
|
|
81
|
-
#### 2.1. Rename and reshape function
|
|
82
|
-
|
|
83
|
-
In `src/nodes/collection.ts`:
|
|
84
|
-
|
|
85
|
-
- Rename `createSourceCollection` → `createCollection`.
|
|
86
|
-
- Move `initialValue` from first positional arg into `options.value` (default `[]`).
|
|
87
|
-
- New signature: `createCollection<T>(start: CollectionCallback<T>, options?: CollectionOptions<T>)`.
|
|
88
|
-
|
|
89
|
-
#### 2.2. Rename types
|
|
90
|
-
|
|
91
|
-
- `SourceCollectionCallback` → `CollectionCallback<T>` (generic over `T` for type coherence with `CollectionOptions<T>`, even though the callback itself doesn't use `T` directly).
|
|
92
|
-
- `SourceCollectionOptions<T>` → `CollectionOptions<T>`, adding the `value?: T[]` field.
|
|
93
|
-
|
|
94
|
-
#### 2.3. Update exports in `index.ts`
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
// Remove
|
|
98
|
-
export { createSourceCollection, SourceCollectionCallback, SourceCollectionOptions, CollectionSource }
|
|
99
|
-
|
|
100
|
-
// Add / rename
|
|
101
|
-
export { createCollection, CollectionCallback, CollectionOptions }
|
|
102
|
-
|
|
103
|
-
// Keep
|
|
104
|
-
export { Collection, DiffResult, isCollection }
|
|
105
|
-
|
|
106
|
-
// Optional (if deriveCollection is exported)
|
|
107
|
-
export { deriveCollection, DeriveCollectionCallback }
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
#### 2.4. Update tests
|
|
111
|
-
|
|
112
|
-
- `test/source-collection.test.ts` → rename to `test/collection.next.test.ts` (or merge into existing collection test file).
|
|
113
|
-
- Update all `createSourceCollection(initialValue, start, options)` calls to `createCollection(start, { value: initialValue, ...options })`.
|
|
114
|
-
- Update type imports: `CollectionCallback` instead of `SourceCollectionCallback`, etc.
|
|
115
|
-
|
|
116
|
-
#### 2.5. Update CLAUDE.md and docs
|
|
117
|
-
|
|
118
|
-
- Update the Collection section to present `createCollection(start, options)` as the primary form.
|
|
119
|
-
- Show `.deriveCollection()` as the way to transform Lists/Collections.
|
|
120
|
-
- Emphasize Sensor ↔ Collection parallel in the mental model section.
|
|
121
|
-
|
|
122
|
-
#### 2.6. Verify
|
|
123
|
-
|
|
124
|
-
- `bun run check` and `bun test` pass.
|
|
125
|
-
- Commit: "Reshape createSourceCollection → createCollection(start, options)"
|
|
126
|
-
|
|
127
|
-
## Type Summary
|
|
128
|
-
|
|
129
|
-
```
|
|
130
|
-
Before After
|
|
131
|
-
───────────────────────────────── ─────────────────────────────────
|
|
132
|
-
createSourceCollection(init, start, opts) → createCollection(start, opts)
|
|
133
|
-
SourceCollectionCallback → CollectionCallback<T>
|
|
134
|
-
SourceCollectionOptions<T> → CollectionOptions<T>
|
|
135
|
-
|
|
136
|
-
createCollection(source, callback) → deriveCollection(source, callback) [internal]
|
|
137
|
-
CollectionCallback<T, U> → DeriveCollectionCallback<T, U> [internal or optional export]
|
|
138
|
-
CollectionSource<T> → CollectionSource<T> [internal]
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## Migration Checklist
|
|
142
|
-
|
|
143
|
-
### Phase 1 — Free the names
|
|
144
|
-
- [ ] Rename `createCollection(source, cb)` → `deriveCollection(source, cb)`
|
|
145
|
-
- [ ] Rename type `CollectionCallback<T, U>` → `DeriveCollectionCallback<T, U>`
|
|
146
|
-
- [ ] Update `List.deriveCollection()` and `Collection.deriveCollection()` to call `deriveCollection()`
|
|
147
|
-
- [ ] Update `index.ts` exports (phase 1)
|
|
148
|
-
- [ ] Update tests (phase 1)
|
|
149
|
-
- [ ] Verify: `bun run check` and `bun test` pass
|
|
150
|
-
- [ ] Commit phase 1
|
|
151
|
-
|
|
152
|
-
### Phase 2 — Reclaim the names
|
|
153
|
-
- [ ] Rename `createSourceCollection` → `createCollection(start, options)` with `options.value`
|
|
154
|
-
- [ ] Rename type `SourceCollectionCallback` → `CollectionCallback<T>`
|
|
155
|
-
- [ ] Rename type `SourceCollectionOptions` → `CollectionOptions` (add `value?: T[]`)
|
|
156
|
-
- [ ] Drop `CollectionSource` from public exports
|
|
157
|
-
- [ ] Update `index.ts` exports (phase 2)
|
|
158
|
-
- [ ] Update tests (phase 2)
|
|
159
|
-
- [ ] Update CLAUDE.md Collection sections
|
|
160
|
-
- [ ] Verify: `bun run check` and `bun test` pass
|
|
161
|
-
- [ ] Commit phase 2
|