Skip to content

Commit

Permalink
Merge pull request #624 from RailsEventStore/rl/remove-pubsub-module
Browse files Browse the repository at this point in the history
Rename RubyEventStore::PubSub::* -> RubyEventStore::*
  • Loading branch information
mostlyobvious authored Jun 25, 2019
2 parents 17998d1 + 4564f1d commit 454a26f
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 248 deletions.
2 changes: 1 addition & 1 deletion contrib/measure/examples/demo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ asn = ActiveSupport::Notifications
event_store = RubyEventStore::Client.new(
repository: RubyEventStore::InstrumentedRepository.new(RubyEventStore::InMemoryRepository.new, asn),
mapper: RubyEventStore::Mappers::InstrumentedMapper.new(RubyEventStore::Mappers::Default.new, asn),
dispatcher: RubyEventStore::InstrumentedDispatcher.new(RubyEventStore::PubSub::Dispatcher.new, asn)
dispatcher: RubyEventStore::InstrumentedDispatcher.new(RubyEventStore::Dispatcher.new, asn)
)
DummyEvent = Class.new(RubyEventStore::Event)
dummy = DummyEvent.new
Expand Down
2 changes: 1 addition & 1 deletion rails_event_store/lib/rails_event_store/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
module RailsEventStore
Event = RubyEventStore::Event
InMemoryRepository = RubyEventStore::InMemoryRepository
Subscriptions = RubyEventStore::PubSub::Subscriptions
Subscriptions = RubyEventStore::Subscriptions
Projection = RubyEventStore::Projection
WrongExpectedEventVersion = RubyEventStore::WrongExpectedEventVersion
InvalidExpectedVersion = RubyEventStore::InvalidExpectedVersion
Expand Down
4 changes: 2 additions & 2 deletions rails_event_store/lib/rails_event_store/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Client < RubyEventStore::Client

def initialize(repository: RailsEventStoreActiveRecord::EventRepository.new,
mapper: RubyEventStore::Mappers::Default.new,
subscriptions: RubyEventStore::PubSub::Subscriptions.new,
subscriptions: RubyEventStore::Subscriptions.new,
dispatcher: RubyEventStore::ComposedDispatcher.new(
RubyEventStore::ImmediateAsyncDispatcher.new(scheduler: ActiveJobScheduler.new),
RubyEventStore::PubSub::Dispatcher.new),
RubyEventStore::Dispatcher.new),
request_metadata: default_request_metadata)
super(repository: RubyEventStore::InstrumentedRepository.new(repository, ActiveSupport::Notifications),
mapper: RubyEventStore::Mappers::InstrumentedMapper.new(mapper, ActiveSupport::Notifications),
Expand Down
2 changes: 1 addition & 1 deletion railseventstore.org/source/docs/instrumentation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ That having said, if you want to instrument your event store with `ActiveSupport

```ruby
repository = RailsEventStoreActiveRecord::EventRepository.new # or other repo you use
dispatcher = RubyEventStore::PubSub::Dispatcher.new # or other dispatcher you use
dispatcher = RubyEventStore::Dispatcher.new # or other dispatcher you use
RubyEventStore::Client.new(
repository: InstrumentedRepository.new(repository, ActiveSupport::Notifications),
dispatcher: InstrumentedDispatcher.new(dispatcher, ActiveSupport::Notifications)
Expand Down
4 changes: 2 additions & 2 deletions railseventstore.org/source/docs/subscribe.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Often you will want to be able to specify both asynchronous and synchronous disp
event_store = RailsEventStore::Client.new(
dispatcher: RubyEventStore::ComposedDispatcher.new(
RubyEventStore::ImmediateAsyncDispatcher.new(scheduler: CustomScheduler.new), # our asynchronous dispatcher, which expects that subscriber respond to `perform_async` method
RubyEventStore::PubSub::Dispatcher.new # regular synchronous dispatcher
RubyEventStore::Dispatcher.new # regular synchronous dispatcher
)
)
```
Expand Down Expand Up @@ -377,7 +377,7 @@ end
event_store = RailsEventStore::Client.new(
dispatcher: RubyEventStore::ComposedDispatcher.new(
RailsEventStore::AfterCommitAsyncDispatcher.new(scheduler: RailsEventStore::ActiveJobScheduler.new),
RubyEventStore::PubSub::Dispatcher.new
RubyEventStore::Dispatcher.new
)
)

Expand Down
7 changes: 4 additions & 3 deletions ruby_event_store/lib/ruby_event_store.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'ruby_event_store/pub_sub/dispatcher'
require 'ruby_event_store/pub_sub/subscriptions'
require 'ruby_event_store/pub_sub/broker'
require 'ruby_event_store/dispatcher'
require 'ruby_event_store/subscriptions'
require 'ruby_event_store/broker'
require 'ruby_event_store/pub_sub'
require 'ruby_event_store/in_memory_repository'
require 'ruby_event_store/projection'
require 'ruby_event_store/errors'
Expand Down
43 changes: 43 additions & 0 deletions ruby_event_store/lib/ruby_event_store/broker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module RubyEventStore
class Broker
def initialize(subscriptions:, dispatcher:)
@subscriptions = subscriptions
@dispatcher = dispatcher
end

def call(event, serialized_event)
subscribers = subscriptions.all_for(event.type)
subscribers.each do |subscriber|
dispatcher.call(subscriber, event, serialized_event)
end
end

def add_subscription(subscriber, event_types)
verify_subscription(subscriber)
subscriptions.add_subscription(subscriber, event_types)
end

def add_global_subscription(subscriber)
verify_subscription(subscriber)
subscriptions.add_global_subscription(subscriber)
end

def add_thread_subscription(subscriber, event_types)
verify_subscription(subscriber)
subscriptions.add_thread_subscription(subscriber, event_types)
end

def add_thread_global_subscription(subscriber)
verify_subscription(subscriber)
subscriptions.add_thread_global_subscription(subscriber)
end

private
attr_reader :subscriptions, :dispatcher

def verify_subscription(subscriber)
raise SubscriberNotExist, "subscriber must be first argument or block" unless subscriber
raise InvalidHandler.new("Handler #{subscriber} is invalid for dispatcher #{dispatcher}") unless dispatcher.verify(subscriber)
end
end
end
6 changes: 3 additions & 3 deletions ruby_event_store/lib/ruby_event_store/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class Client

def initialize(repository:,
mapper: Mappers::Default.new,
subscriptions: PubSub::Subscriptions.new,
dispatcher: PubSub::Dispatcher.new,
subscriptions: Subscriptions.new,
dispatcher: Dispatcher.new,
clock: ->{ Time.now.utc })
@repository = repository
@mapper = mapper
@broker = PubSub::Broker.new(subscriptions: subscriptions, dispatcher: dispatcher)
@broker = Broker.new(subscriptions: subscriptions, dispatcher: dispatcher)
@clock = clock
@metadata = Concurrent::ThreadLocalVar.new
end
Expand Down
18 changes: 18 additions & 0 deletions ruby_event_store/lib/ruby_event_store/dispatcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RubyEventStore
class Dispatcher
def call(subscriber, event, _)
subscriber = subscriber.new if Class === subscriber
subscriber.call(event)
end

def verify(subscriber)
begin
subscriber_instance = Class === subscriber ? subscriber.new : subscriber
rescue ArgumentError
false
else
subscriber_instance.respond_to?(:call)
end
end
end
end
21 changes: 21 additions & 0 deletions ruby_event_store/lib/ruby_event_store/pub_sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module RubyEventStore
module PubSub
def self.const_missing(const_name)
if const_name.equal?(:Subscriptions)
warn "`RubyEventStore::PubSub::Subscriptions` has been deprecated. Use `RubyEventStore::Subscriptions` instead."

Subscriptions
elsif const_name.equal?(:Broker)
warn "`RubyEventStore::PubSub::Broker` has been deprecated. Use `RubyEventStore::Broker` instead."

Broker
elsif const_name.equal?(:Dispatcher)
warn "`RubyEventStore::PubSub::Dispatcher` has been deprecated. Use `RubyEventStore::Dispatcher` instead."

Dispatcher
else
super
end
end
end
end
45 changes: 0 additions & 45 deletions ruby_event_store/lib/ruby_event_store/pub_sub/broker.rb

This file was deleted.

20 changes: 0 additions & 20 deletions ruby_event_store/lib/ruby_event_store/pub_sub/dispatcher.rb

This file was deleted.

110 changes: 0 additions & 110 deletions ruby_event_store/lib/ruby_event_store/pub_sub/subscriptions.rb

This file was deleted.

6 changes: 3 additions & 3 deletions ruby_event_store/lib/ruby_event_store/spec/broker_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
let(:event) { instance_double(::RubyEventStore::Event, type: 'EventType') }
let(:serialized_event) { instance_double(::RubyEventStore::SerializedRecord) }
let(:handler) { HandlerClass.new }
let(:subscriptions) { ::RubyEventStore::PubSub::Subscriptions.new }
let(:dispatcher) { ::RubyEventStore::PubSub::Dispatcher.new }
let(:subscriptions) { ::RubyEventStore::Subscriptions.new }
let(:dispatcher) { ::RubyEventStore::Dispatcher.new }
let(:broker) { broker_klass.new(subscriptions: subscriptions, dispatcher: dispatcher) }

specify "no dispatch when no subscriptions" do
Expand Down Expand Up @@ -42,7 +42,7 @@
allow(dispatcher).to receive(:verify).and_return(false)
expect do
broker.add_subscription(HandlerClass, [])
end.to raise_error(RubyEventStore::InvalidHandler, /Handler HandlerClass is invalid for dispatcher .*PubSub::Dispatcher/)
end.to raise_error(RubyEventStore::InvalidHandler, /Handler HandlerClass is invalid for dispatcher .*Dispatcher/)
expect do
broker.add_global_subscription(HandlerClass)
end.to raise_error(RubyEventStore::InvalidHandler, /is invalid for dispatcher/)
Expand Down
Loading

0 comments on commit 454a26f

Please sign in to comment.