Skip to content

FEATURE: Filtered reads for subscribers - #20

Merged
bwaidelich merged 2 commits into
mainfrom
filtered-reads
Nov 12, 2026
Merged

bwaidelich merged 2 commits into
mainfrom
filtered-reads

Conversation

@bwaidelich

@bwaidelich bwaidelich commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

Subscribers can declare an adapter-specific filter. The filter type is opaque to core (EventStoreAdapter<E, F>).

Subscriber::create(
    id: 'orders',
    handler: $handler,
    filter: Query::fromItems(QueryItem::create(eventTypes: 'OrderPlaced')), // DCB adapter
);

Adapter contract (breaking):

-public function read(Position $startPosition, int|null $limit = null): iterable;
+public function read(Position $startPosition, ReadOptions $options): iterable; // limit, upTo, filter
+public function matches(object $filter, object $event): bool;

How a run reads:

 handleEvents(subscriptions, limit)
-  read(start, limit + 1)
+  if one subscription with a filter
+    upTo = lastPosition()                      // snapshot
+    read(start, ReadOptions(limit + 1, upTo, filter)), skipped if start > upTo
+  else
+    read(start, ReadOptions(limit + 1))        // shared, unfiltered
+    per subscription: skip events its filter doesn't match(), but advance the position
   ...
+  read exhausted → move filtered subscription to upTo  // gap is not rescanned
  • limit counts events returned by read(), i.e. matching events in filtered single-subscription runs
  • The hasPendingEvents shortcut of catchUpNextActive() only applies if there was nothing to read
  • Filters aren't persisted. Changing one requires a new id or reset()

Reasoning and known consequences: docs/decisions.md → "Filtered reads". Usage: docs/how-it-works.md → "Filtered subscribers". Adapter changes: docs/upgrading.md.

Evidence

Merge Danger

Door: two-way until 2.0 is tagged, one-way afterwards

The read() signature and matches() are final for 2.0. A change after the tag would break every adapter again.

Blast Radius: adapters

Every EventStoreAdapter implementation fails with a fatal error until it implements the new read() and matches(). They can't support 1.x at the same time anymore, so the dcb and neos adapters require ^2. Engine users see no change unless they set a filter.

A subscriber can declare an adapter specific filter. Runs with a single
subscription pass it to the adapter, bounded by a snapshot of
`lastPosition()`, and move the subscription to that snapshot once the
read is exhausted, so gaps without matching events are not rescanned.
Runs with several subscriptions share one unfiltered read and apply the
filters through the new `EventStoreAdapter::matches()`.

Breaking: `EventStoreAdapter::read()` takes a `ReadOptions` object with
`limit`, `upTo` and `filter` instead of `int|null $limit`, and adapters
have to implement `matches()`.
@bwaidelich bwaidelich changed the title Filtered reads FEATURE: Filtered reads for subscribers Nov 12, 2026
@bwaidelich
bwaidelich merged commit 5b817d7 into main Nov 12, 2026
5 checks passed
@bwaidelich
bwaidelich deleted the filtered-reads branch November 12, 2026 16:30
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