@@ -87,9 +87,26 @@ def clear
87
87
# # }
88
88
#
89
89
# The +notify+ API can receive either an event name and a payload hash, or an event object.
90
- # If an event object is used, it will be passed through to subscribers as-is, and the name of the
90
+
91
+ # ==== Event Objects
92
+ #
93
+ # If an event object is passed to the +notify+ API, it will be passed through to subscribers as-is, and the name of the
91
94
# object's class will be used as the event name.
92
95
#
96
+ # class UserCreatedEvent
97
+ # def initialize(id:, name:)
98
+ # @id = id
99
+ # @name = name
100
+ # end
101
+ #
102
+ # def to_h
103
+ # {
104
+ # id: @id,
105
+ # name: @name
106
+ # }
107
+ # end
108
+ # end
109
+ #
93
110
# Rails.event.notify(UserCreatedEvent.new(id: 123, name: "John Doe"))
94
111
# # Emits event:
95
112
# # {
@@ -99,7 +116,8 @@ def clear
99
116
# # source_location: { filepath: "path/to/file.rb", lineno: 123, label: "UserService#create" }
100
117
# # }
101
118
#
102
- # These objects should represent schematized events and be serializable.
119
+ # An event is any Ruby object representing a schematized event. While payload hashes allow arbitrary,
120
+ # implicitly-structured data, event objects are intended to enforce a particular schema.
103
121
#
104
122
# ==== Default Encoders
105
123
#
@@ -249,7 +267,6 @@ def initialize(*subscribers, raise_on_error: false, tags: nil)
249
267
# source_location: Hash (The source location of the event, containing the filepath, lineno, and label)
250
268
#
251
269
def subscribe ( subscriber )
252
- return if @subscribers . include? ( subscriber )
253
270
unless subscriber . respond_to? ( :emit )
254
271
raise ArgumentError , "Event subscriber #{ subscriber . class . name } must respond to #emit"
255
272
end
0 commit comments