Skip to content

Commit

Permalink
Merge pull request #19 from Kaligo/maintainance/listen-multiple-event…
Browse files Browse the repository at this point in the history
…-sources

Allow listen multiple event names for EventSource listener
  • Loading branch information
Drenmi authored Nov 8, 2021
2 parents 81df8a1 + edee757 commit 7d59360
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.8.3

### New features

- Listen on multiple event names to `EventSource` listener.

## 0.8.2

### Bug fixes
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
stimpack (0.8.1)
stimpack (0.8.3)
activesupport (>= 6.1)

GEM
Expand Down Expand Up @@ -54,10 +54,11 @@ GEM
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
zeitwerk (2.4.2)
zeitwerk (2.5.1)

PLATFORMS
x86_64-darwin-19
x86_64-darwin-20

DEPENDENCIES
rake (~> 13.0)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ we can register a callback to listen for events from another part of our
application, and we will receive an event object when the event is emitted:

```ruby
Foo.on(:bar) do |event|
Foo.on(:bar, :qui, :qux) do |event|
puts event.message
end

Foo.new.bar
#=> "Hello, world!"
Foo.new.qui
#=> "Hello, world!"
Foo.new.qux
#=> "Hello, world!"
```

*Note: Callbacks are invoked synchronously in the same thread, so don't use
Expand Down
6 changes: 4 additions & 2 deletions lib/stimpack/event_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ def self.extended(klass)
end
end

def on(event_name, raise_errors: false, &block)
def on(*event_names, raise_errors: false, &block)
listener = Listener.new(raise_errors: raise_errors, &block)

event_listeners["#{self}.#{event_name}"] << listener
event_names.each do |event_name|
event_listeners["#{self}.#{event_name}"] << listener
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/stimpack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stimpack
VERSION = "0.8.2"
VERSION = "0.8.3"
end
9 changes: 8 additions & 1 deletion spec/stimpack/event_source_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "stimpack/event_source"
require "active_support/core_ext/class/attribute"

RSpec.describe Stimpack::EventSource do
subject(:service) { klass }
Expand Down Expand Up @@ -36,7 +37,13 @@ def self.to_s
end

describe ".on" do
it { expect { service.on(:foo) {} }.to change { klass.event_listeners["Foo.foo"].size }.by(1) }
context "with single event" do
it { expect { service.on(:foo) {} }.to change { klass.event_listeners["Foo.foo"].size }.by(1) }
end

context "with multiple events" do
it { expect { service.on(:foo, :bar, :qux) {} }.to change { klass.event_listeners["Foo.foo"].size }.by(1).and change { klass.event_listeners["Foo.bar"].size }.by(1).and change { klass.event_listeners["Foo.qux"].size }.by(1) }
end
end

describe "#emit" do
Expand Down

0 comments on commit 7d59360

Please sign in to comment.