Giter Club home page Giter Club logo

events's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

events's Issues

Consider using compiled ETS match specs for event filtering

This is just a wild idea, but could provide a very powerful and flexible API.

ETS allows making "standalone" match specs created with :ets.match_spec_compile/1 and executed with :ets.match_spec_run/2 - this is used by DETS for executing match specs and for example by the Version module for matching version requirements. Match specs are pretty efficient as they are executed on an internal small VM that runs inside BEAM itself, called PAM (Yo, Dawg).

I could imagine an API that allows you to subscribe with a match spec to events and would allow you to filter and possibly transform them before they would be delivered to you, reducing the amount of flying messages at the cost of extra work for the sender.

For example, here's a simple API for a PubSub based on elixir's Registry and this idea:

defmodule RegistryPubSub do
  def child_spec(opts) do
    Registry.child_spec(Keyword.merge(opts, keys: :duplicate))
  end

  def subscribe_match_spec(name, channel, spec) do
    Registry.register(name, channel, :ets.match_spec_compile(spec))
  end

  def publish(name, channel, events) do
    Registry.dispatch(name, channel, fn entries ->
      for {pid, spec} <- entries, event <- :ets.match_spec_run(events, spec), do: send(pid, event)
      :ok
    end)
  end
end

With some macro magic, I could imagine an API like:

require RegistryPubSub
RegistryPubSub.subscribe(MyServer, "foo_channel") do
  {:created, topic, _details} -> {:created, topic}
  {:deleted, %{name: name}} -> {:deleted, name}
end

I haven't evaluated the performance of this solution in practice, but I think it's very promising and allows for filtering events at the producer instead of filtering them at the receiver with a very powerful mechanism - basically almost all of pattern matching, since match specs are almost like making patterns first class citizens.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.