Selective Broadcasts

This proposal probably arises from my immature experience with GenStage,
but what if, at subscribe time a consumer could specify a selector i.e. a function
(event :: any -> boolean) which would filter events being sent to itself. Somewhat like:

GenStage.sync_subscribe(consumer, 
  to: producer, 
  selector: fn %{key: key} -> key =~ "foo" end
)

Now when producer emits an event say %{key: key}, this will be dispatched to consumer
only if "foo" matches key.
This is probably a similar approach to the partition dispatcher with extra control over the hashing function. Could I state this so?

I have a very naïve proof of concept in the form of a private pull request here.
https://github.com/zampino/gen_stage/pull/1

where I also modified the gen_event example:
https://github.com/zampino/gen_stage/blob/selective-broadcast-dispatcher/examples/gen_event.exs#L89-L108

What would the community think of this feature?
Is there already a way to achieve this with no need of extra work?

1 Like