-
Notifications
You must be signed in to change notification settings - Fork 121
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
Changes from 1 commit
be31df8
39bc7ad
8ff0e36
972be0a
b693e49
33f34cb
2c9e3aa
554cdbe
4f76f27
e18ceb3
c8dedab
29e62b4
acc8184
2bd6c6d
9aefcdf
636cafb
ff945e7
cec639c
b82767b
570590f
7140093
25dd989
4ba25e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
@subscriptions[type.to_s] << subscription | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
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 | ||
|
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 [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
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 |
There was a problem hiding this comment.
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