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

Improve usefulness of event_class_to_event_type resolver #1036

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 17 additions & 19 deletions ruby_event_store/lib/ruby_event_store/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

module RubyEventStore
class Client
def initialize(repository:,
mapper: Mappers::Default.new,
subscriptions: Subscriptions.new,
dispatcher: Dispatcher.new,
clock: default_clock,
correlation_id_generator: default_correlation_id_generator)


def initialize(
repository:,
mapper: Mappers::Default.new,
subscriptions: Subscriptions.new,
dispatcher: Dispatcher.new,
clock: default_clock,
correlation_id_generator: default_correlation_id_generator
)
@repository = repository
@mapper = mapper
@subscriptions = subscriptions
@broker = Broker.new(subscriptions: subscriptions, dispatcher: dispatcher)
@clock = clock
@metadata = Concurrent::ThreadLocalVar.new
@correlation_id_generator = correlation_id_generator
@event_type_resolver = subscriptions.event_type_resolver
end


# Persists events and notifies subscribed handlers about them
#
# @param events [Array<Event>, Event] event(s)
Expand Down Expand Up @@ -109,7 +109,7 @@ def streams_of(event_id)
def subscribe(subscriber = nil, to:, &proc)
raise ArgumentError, "subscriber must be first argument or block, cannot be both" if subscriber && proc
subscriber ||= proc
broker.add_subscription(subscriber, to)
broker.add_subscription(subscriber, to.map(&event_type_resolver))
end

# Subscribes a handler (subscriber) that will be invoked for all published events
Expand Down Expand Up @@ -139,11 +139,12 @@ def subscribers_for(event_class)
# which are active only during the invocation of the provided
# block of code.
class Within
def initialize(block, broker)
def initialize(block, broker, resolver)
@block = block
@broker = broker
@global_subscribers = []
@subscribers = Hash.new {[]}
@resolver = resolver
end

# Subscribes temporary handlers that
Expand Down Expand Up @@ -175,9 +176,9 @@ def subscribe_to_all_events(*handlers, &handler2)
# @param to [Array<Class>] types of events to subscribe
# @param handler [Proc] handler passed as proc
# @return [self]
def subscribe(handler=nil, to:, &handler2)
def subscribe(handler = nil, to:, &handler2)
raise ArgumentError if handler && handler2
@subscribers[handler || handler2] += Array(to)
@subscribers[handler || handler2] += Array(to).map(&resolver)
self
end

Expand All @@ -195,6 +196,7 @@ def call
end

private
attr_reader :resolver

def add_thread_subscribers
@subscribers.map do |subscriber, types|
Expand All @@ -216,7 +218,7 @@ def add_thread_global_subscribers
# @return [Within] builder object which collects temporary subscriptions
def within(&block)
raise ArgumentError if block.nil?
Within.new(block, broker)
Within.new(block, broker, event_type_resolver)
end

# Set additional metadata for all events published within the provided block
Expand Down Expand Up @@ -328,10 +330,6 @@ def append_records_to_stream(records, stream_name:, expected_version:)

protected

def event_type_resolver
subscriptions.event_type_resolver
end

def metadata=(value)
@metadata.value = value
end
Expand All @@ -344,6 +342,6 @@ def default_correlation_id_generator
->{ SecureRandom.uuid }
end

attr_reader :repository, :mapper, :subscriptions, :broker, :clock, :correlation_id_generator
attr_reader :repository, :mapper, :subscriptions, :broker, :clock, :correlation_id_generator, :event_type_resolver
end
end
18 changes: 9 additions & 9 deletions ruby_event_store/lib/ruby_event_store/spec/subscriptions_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def call(event)
another_handler = TestHandler.new
global_handler = TestHandler.new

subscriptions.add_subscription(handler, [Test1DomainEvent, Test3DomainEvent])
subscriptions.add_subscription(another_handler, [Test2DomainEvent])
subscriptions.add_subscription(handler, ['Test1DomainEvent', 'Test3DomainEvent'])
subscriptions.add_subscription(another_handler, ['Test2DomainEvent'])
subscriptions.add_global_subscription(global_handler)

expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler, global_handler])
Expand All @@ -36,11 +36,11 @@ def call(event)
another_handler = TestHandler.new
global_handler = TestHandler.new

subscriptions.add_thread_subscription(handler, [Test1DomainEvent, Test3DomainEvent])
subscriptions.add_thread_subscription(another_handler, [Test2DomainEvent])
subscriptions.add_thread_subscription(handler, ['Test1DomainEvent', 'Test3DomainEvent'])
subscriptions.add_thread_subscription(another_handler, ['Test2DomainEvent'])
subscriptions.add_thread_global_subscription(global_handler)
t = Thread.new do
subscriptions.add_thread_subscription(handler, [Test2DomainEvent])
subscriptions.add_thread_subscription(handler, ['Test2DomainEvent'])
subscriptions.add_thread_global_subscription(another_handler)
expect(subscriptions.all_for('Test2DomainEvent')).to eq([another_handler, handler])
end
Expand Down Expand Up @@ -77,7 +77,7 @@ def call(event)
it 'revokes subscription' do
handler = TestHandler.new

revoke = subscriptions.add_subscription(handler, [Test1DomainEvent, Test2DomainEvent])
revoke = subscriptions.add_subscription(handler, ['Test1DomainEvent', 'Test2DomainEvent'])
expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler])
expect(subscriptions.all_for('Test2DomainEvent')).to eq([handler])
revoke.()
Expand All @@ -99,7 +99,7 @@ def call(event)
it 'revokes thread subscription' do
handler = TestHandler.new

revoke = subscriptions.add_thread_subscription(handler, [Test1DomainEvent, Test2DomainEvent])
revoke = subscriptions.add_thread_subscription(handler, ['Test1DomainEvent', 'Test2DomainEvent'])
expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler])
expect(subscriptions.all_for('Test2DomainEvent')).to eq([handler])
revoke.()
Expand All @@ -117,8 +117,8 @@ def call(event)

it 'subscribes by type of event which is a class' do
handler = TestHandler.new
subscriptions.add_subscription(handler, [Test1DomainEvent])
subscriptions.add_thread_subscription(handler, [Test1DomainEvent])
subscriptions.add_subscription(handler, ['Test1DomainEvent'])
subscriptions.add_thread_subscription(handler, ['Test1DomainEvent'])

expect(subscriptions.all_for('Test1DomainEvent')).to eq([handler, handler])
end
Expand Down
12 changes: 2 additions & 10 deletions ruby_event_store/lib/ruby_event_store/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def initialize(event_type_resolver: default_event_type_resolver)
end

def add_subscription(subscriber, event_types)
local.add(subscriber, resolve_event_types(event_types))
local.add(subscriber, event_types)
end

def add_global_subscription(subscriber)
global.add(subscriber)
end

def add_thread_subscription(subscriber, event_types)
thread.local.add(subscriber, resolve_event_types(event_types))
thread.local.add(subscriber, event_types)
end

def add_thread_global_subscription(subscriber)
Expand All @@ -40,14 +40,6 @@ def default_event_type_resolver
->(value) { value.to_s }
end

def resolve_event_types(event_types)
event_types.map(&method(:resolve_event_type))
end

def resolve_event_type(type)
event_type_resolver.call(type)
end

class ThreadSubscriptions
def initialize
@local = ThreadLocalSubscriptions.new
Expand Down