I am using dry-events in services objects inside rails controllers:
class MyService
include Dry::Events::Publisher[:my_service]
register_event(:ok)
register_event(:invalid)
def call(params)
if params # Just a demo
publish(:ok)
else
publish(:invalid)
end
end
end
# Rails controller
class MyController < ApplicationController
def create
@service = MyService.new
@service.subscribe(:ok) do
flash.now[:success] = "Good"
render action: 'new' and return
end
@service.subscribe(:invalid) do
flash.now[:error] = "Bad"
render action: 'new' and return
end
@service.(params)
end
end
I will get PublisherAlreadyRegisteredError
sometimes (but not every time, not sure what triggers it).
Using include Dry::Events::Publisher[self.__id__]
would fix the issue. However, Dry::Events::Publisher.registry
will grow without deleting the service objects.
Question: Should I use dry-events inside object instances?
Thanks!