Skip to content

Improve Consistency for Lazy Lifecycle (watched) across API#24

Merged
estherbrunner merged 8 commits intomainfrom
feature/collection-changes
Feb 14, 2026
Merged

Improve Consistency for Lazy Lifecycle (watched) across API#24
estherbrunner merged 8 commits intomainfrom
feature/collection-changes

Conversation

@estherbrunner
Copy link
Member

@estherbrunner estherbrunner commented Feb 14, 2026

0.18.1

Added

  • Memo watched(invalidate) option: createMemo(fn, { watched }) accepts a lazy lifecycle callback that receives an invalidate function. Calling invalidate() marks the memo dirty and triggers re-evaluation. The callback is invoked on first sink attachment and cleaned up when the last sink detaches. This enables patterns like DOM observation where a memo re-derives its value in response to external events (e.g., MutationObserver) without needing a separate Sensor.
  • Task watched(invalidate) option: Same pattern as Memo. Calling invalidate() aborts any in-flight computation and triggers re-execution.
  • CollectionChanges<T> type: New typed interface for collection mutations with add?: T[], change?: T[], remove?: T[] arrays. Replaces the untyped DiffResult records previously used by CollectionCallback.
  • SensorOptions<T> type: Dedicated options type for createSensor, extending SignalOptions<T> with optional value.
  • CollectionChanges export from public API (index.ts).
  • SensorOptions export from public API (index.ts).

Changed

  • createSensor parameter renamed: startwatched for consistency with Store/List lifecycle terminology.
  • createSensor options type: ComputedOptions<T>SensorOptions<T>. This decouples Sensor options from ComputedOptions, which now carries the watched(invalidate) field for Memo/Task.
  • createCollection parameter renamed: startwatched for consistency.
  • CollectionCallback is now generic: CollectionCallbackCollectionCallback<T>. The applyChanges parameter accepts CollectionChanges<T> instead of DiffResult.
  • CollectionOptions.createItem signature: (key: string, value: T) => Signal<T>(value: T) => Signal<T>. Key generation is now handled internally.
  • KeyConfig<T> return type relaxed: Key functions may now return string | undefined. Returning undefined falls back to synthetic key generation.

Removed

  • DiffResult removed from public API: No longer re-exported from index.ts. The type remains available from src/nodes/list.ts for internal use but is superseded by CollectionChanges<T> for collection mutations.

- Introduce CollectionChanges<T> and switch collection API to use it
  instead of DiffResult
- Update deriveCollection and createCollection signatures to accept
  applyChanges: (changes: CollectionChanges<T>) and remove DiffResult
- Update tests and typings to reference CollectionChanges
- Add example demonstrating element-based collection integration
- Add getKeyGenerator to centralize item key creation
- Replace per-file inline key config logic with getKeyGenerator
- Use DEFAULT_EQUALITY instead of legacy defaultEquals across nodes
- Mark collection equality as SKIP_EQUALITY for external changes
- Export getKeyGenerator from types and update typings
- Align store/list/collection/sensor/memo/state/task with new equality
- Rename start -> watched lifecycles for sensors and collections
- Add optional watched(invalidate) callbacks to Memo and Task
- Change Collection Options.createItem signature to (value)
- Rename Store/List initializer parameter from initialValue to value
- Update types/docs to reflect watched semantics
- Bump version to 0.18.1
Switch deriveCollection to cache derived values (T[]) and track keys
Build and expose values via per-item signals; maintain separate keys
array
Re-establish edges when keys change to keep graph in sync
Align with Store/List pattern for consistent internal structure
@estherbrunner estherbrunner merged commit 8538112 into main Feb 14, 2026
1 check passed
@estherbrunner estherbrunner deleted the feature/collection-changes branch February 14, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant