Skip to content

feat(data): add Observe.fromKeys reactive keyed-collection combinator#145

Merged
krisnye merged 1 commit into
mainfrom
krisnye/observe-from-keys
Jul 14, 2026
Merged

feat(data): add Observe.fromKeys reactive keyed-collection combinator#145
krisnye merged 1 commit into
mainfrom
krisnye/observe-from-keys

Conversation

@krisnye

@krisnye krisnye commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Description

Adds Observe.fromKeys — a reactive keyed collection combinator:

fromKeys<K, V>(keys: Observe<readonly K[]>, observeChild: (key: K) => Observe<V>): Observe<readonly V[]>

Given an observable list of keys and a factory mapping each key to a child observe function, it yields the child values in key order. Child subscriptions are keyed by === identity and reused across list changes — only added keys create a child and only removed keys dispose one — so a value change on one child re-emits only that child's slot (siblings keep their values, no recompute, no re-subscribe). It emits once every current child has yielded a value (synchronously when all children are synchronous), and re-emits on any key-list change or child emission.

This is the dynamic, keyed counterpart to the existing fromArray / fromProperties, which combine a fixed set of observe functions; here the set of children is itself driven by an observe function of keys.

Motivation

It generalizes a local fromEntities helper written for firefly-platform's avTimelinePayload reactive-graph refactor (select the ordered entity set → one per-entity child observable → collect), so that consumer can drop the private helper and import this instead. It is the "membership set → per-entity leaf" substrate for building O(changes) reactive dependency graphs over an ECS.

Tests

New from-keys.test.ts (peer to the source): ordered emission, per-slot re-emit with sibling reuse (asserts sibling subscription count is untouched), key add / remove (disposes child) / reorder, waiting for all children before first emit, empty-list, and full disposal (keys + every child unsubscribed).

pnpm run typecheck, pnpm run lint, and the full observe suite (176 tests) pass. Version bumped to 0.9.80 via pnpm run bump.

fromKeys(keys$, observeChild) maps an observable list of keys to an
observable of per-key child values in key order. Child subscriptions are
keyed by identity and reused across list changes — only added/removed keys
create/dispose a child — so a value change on one child re-emits just its
slot without recomputing or re-subscribing the others. Emits once every
current child has yielded (synchronously when all children are synchronous);
re-emits on key-list change or any child emission.

This is the dynamic, keyed counterpart to fromArray / fromProperties (which
combine a fixed set of observe functions). It generalizes the local
`fromEntities` helper written in firefly-platform's avTimelinePayload
reactive-graph refactor; that consumer can now import this instead.

Peer tests cover ordered emission, per-slot re-emit with sibling reuse, key
add/remove/reorder, waiting for all children, empty list, and disposal.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@krisnye krisnye force-pushed the krisnye/observe-from-keys branch from e68e9d4 to 7df6ebf Compare July 14, 2026 22:50
@krisnye krisnye merged commit 6fee566 into main Jul 14, 2026
4 checks passed
@krisnye krisnye deleted the krisnye/observe-from-keys branch July 14, 2026 22:59
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