Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptions redesigned #665

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
be31df8
Redefine subscriptions - breaking changes
mpraglowski Aug 9, 2019
39bc7ad
Refactor subscriptions
mpraglowski Aug 9, 2019
8ff0e36
Subscription storage is optional
mpraglowski Aug 9, 2019
972be0a
All dispatchers & schedulers receive now subscription object instead …
mpraglowski Aug 10, 2019
b693e49
Single subscriptions store is enough
mpraglowski Aug 16, 2019
33f34cb
Subscription global or assigned to specific type - it's just an imple…
mpraglowski Aug 16, 2019
2c9e3aa
Minor improvement & fixes
mpraglowski Aug 16, 2019
554cdbe
Docs & lint/specs for subscriptions store
mpraglowski Aug 16, 2019
4f76f27
Docs & specs for Subscription object
mpraglowski Aug 16, 2019
e18ceb3
No more local & global subscriptions, there are main subscription sto…
mpraglowski Aug 16, 2019
c8dedab
Do not need that anymore
mpraglowski Aug 16, 2019
29e62b4
Simplify subscriptions store API
mpraglowski Aug 16, 2019
acc8184
Must be excluded from mutation tests as test where it could be trigge…
mpraglowski Aug 16, 2019
2bd6c6d
Kill mutant
mpraglowski Aug 16, 2019
9aefcdf
Typo fix
mpraglowski Aug 16, 2019
636cafb
It does not make sense to require subscription store to be chainable
mpraglowski Aug 16, 2019
ff945e7
Fix subscriptions store lint namespace
mpraglowski Aug 16, 2019
cec639c
Add instrumented subscriptions store
mpraglowski Aug 16, 2019
b82767b
Use store factory for thread store instead of just a class
mpraglowski Aug 16, 2019
570590f
Again, keep Ruby 2.4 compatibility for now
mpraglowski Sep 6, 2019
7140093
Kill mutations
mpraglowski Sep 6, 2019
25dd989
Add frozen_string_literal: true in all changed files
mpraglowski Sep 6, 2019
4ba25e3
Document subscriptions provider class
mpraglowski Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
# frozen_string_literal: true

module RubyEventStore
# In memory subscriptions store
class InMemorySubscriptionsStore
# Instantiates a new in memory subscriptions store
#
# @return [InMemorySubscriptionsStore]
def initialize
@subscriptions = Hash.new {|hsh, key| hsh[key] = [] }
@subscriptions = Hash.new { |hsh, key| hsh[key] = [] }
end

# Stores subscription in the store
# @param subscription [Subscription] subscription to store
# @param type [String, Class, GLOBAL_SUBSCRIPTION] a type of [Event]
# or global subscription for for which subscription should be stored
#
# @return [self]
def add(subscription, type = GLOBAL_SUBSCRIPTION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I'd rather call it ANY_EVENT

@subscriptions[type.to_s] << subscription
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to_s? Aren't we expecting Event#type after all?

self
end

# Removed subscription from the store
# @param subscription [Subscription] subscription to remove
# @param type [String, Class, GLOBAL_SUBSCRIPTION] a type of [Event]
# or global subscription for for which subscription should be removed
#
# @return [self]
def delete(subscription, type = GLOBAL_SUBSCRIPTION)
@subscriptions.fetch(type.to_s).delete(subscription)
self
end

# Gets all subscriptions stored for given event type
# @param type [String, Class, GLOBAL_SUBSCRIPTION] a type of [Event]
# or global subscription for which subscriptions should be returned
#
# @return [Array<Subscription>]
def all_for(type)
@subscriptions[type.to_s]
end

def all_for(event_type)
@subscriptions[event_type.to_s]
# Gets all subscriptions stored
#
# @return [Array<Subscription>]
def all
@subscriptions.values.flatten.uniq
end

# Gets this instance of subscription store
# Required for internal implementation of thread subscriptions
#
# @return [self]
def value
self
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RSpec.shared_examples :subscription_store do |subscription_store|
FirstEvent = Class.new(RubyEventStore::Event)
SecondEvent = Class.new(RubyEventStore::Event)

specify do
expect(subscription_store.all).to eq []
expect(subscription_store.all_for(FirstEvent)).to eq []
Copy link
Member

@mostlyobvious mostlyobvious Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly ok but since we allow different event instances I think I'd prefer to pass #type or at least having these shared examples run on different Event implementation

def first_type
  FirstEvent.new.type
end

expect(subscription_store.all_for(SecondEvent)).to eq []
expect(subscription_store.all_for(RubyEventStore::GLOBAL_SUBSCRIPTION)).to eq []

global = RubyEventStore::Subscription.new(-> { }, store: subscription_store)
first = RubyEventStore::Subscription.new(-> { }, [FirstEvent], store: subscription_store)
second = RubyEventStore::Subscription.new(-> { }, [FirstEvent, SecondEvent], store: subscription_store)

expect(subscription_store.all).to match_array [global, first, second]

expect(subscription_store.all_for(FirstEvent)).to match_array [first, second]
expect(subscription_store.all_for(SecondEvent)).to match_array [second]
expect(subscription_store.all_for(RubyEventStore::GLOBAL_SUBSCRIPTION)).to match_array [global]

global.unsubscribe
expect(subscription_store.all_for(RubyEventStore::GLOBAL_SUBSCRIPTION)).to eq []
expect(subscription_store.all).to match_array [first, second]

first.unsubscribe
subscription_store.delete(first, FirstEvent)
expect(subscription_store.all_for(FirstEvent)).to match_array [second]
expect(subscription_store.all_for(SecondEvent)).to match_array [second]
expect(subscription_store.all).to match_array [second]

second.unsubscribe
expect(subscription_store.all_for(FirstEvent)).to eq []
expect(subscription_store.all_for(SecondEvent)).to eq []
expect(subscription_store.all).to eq []
end

specify "#value" do
expect(subscription_store.value).to eq(subscription_store)
end
end
8 changes: 8 additions & 0 deletions ruby_event_store/spec/in_memory_subscriptions_store_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'
require 'ruby_event_store/spec/subscription_store_lint'

module RubyEventStore
RSpec.describe InMemorySubscriptionsStore do
it_behaves_like :subscription_store, InMemorySubscriptionsStore.new
end
end