Skip to content

Batch operations #48

Open

Description

@lucasfelixs

Context

core.add and core.remove work one entity at a time, and each call emits its own entities:changed event. Loading 1,000 entities emits 1,000 events, and each one triggers a full repaint in the SVG renderer (see #47). Deleting a multi-selection has the same problem.

core.updateMany already exists and emits a single { type: 'update', ids } event. This issue adds the same for add and remove, and a generic batch for mixed changes.


What changes

New methods on SpatialCore:

core.addMany(entities: Entity[]): void;      // one { type: 'add', ids } event
core.removeMany(ids: EntityId[]): void;      // one { type: 'remove', ids } event
                                             // + one selection:change if any removed id was selected

Optional: a generic batch. It collects every change made inside the callback and emits at most one event per type at the end:

core.batch(() => {
  core.add(a);
  core.update(b.id, { bounds });
  core.remove(c.id);
});
// emits: add [a], update [b], remove [c] — once each, after the callback

Proposal: ship addMany and removeMany first (small and clear), and discuss batch in the comments. Nested batches and errors thrown inside the callback need a decision.


Affected files

File Change
packages/core/src/core.ts addMany, removeMany (and possibly batch)
packages/core/src/__tests__/ Tests for the new methods
packages/core/README.md Document them next to updateMany

Invariants to preserve

  • Event payload shapes do not change: { type, ids } and { selection }.
  • Empty input emits no event, the same as updateMany today.
  • removeMany removes ids from the selection, the same as remove.
  • Insertion order is preserved by addMany (it defines z-order).
  • No production dependencies; core stays framework-agnostic.

Implementation checklist

  • addMany with tests (one event, ids in order, empty input)
  • removeMany with tests (one event, selection updated with one selection:change, unknown ids ignored)
  • Docs in the core README
  • Changeset (minor for core)
  • Run pnpm typecheck && pnpm test && pnpm lint
  • Optional: discuss and implement batch (may be a follow-up issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions