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

Fix default store missing event hooks #420

Merged
merged 1 commit into from
Oct 4, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [#416](https://github.com/slack-ruby/slack-ruby-client/pull/416): Removes default values for Faraday's SSL settings `ca_file` and `ca_path` - [@irphilli](https://github.com/irphilli).
* [#417](https://github.com/slack-ruby/slack-ruby-client/pull/417): Raise rescuable errors - [@zachahn](https://github.com/zachahn).
* [#419](https://github.com/slack-ruby/slack-ruby-client/pull/419): Use `rtm.connect` instead of `rtm.start` - [@kstole](https://github.com/kstole).
* [#420](https://github.com/slack-ruby/slack-ruby-client/pull/420): Fix default store missing event hooks - [@kstole](https://github.com/kstole).
* Your contribution here.

### 1.1.0 (2022/06/05)
Expand Down
10 changes: 1 addition & 9 deletions lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class ClientAlreadyStartedError < StandardError; end
include Api::Message
include Api::Typing

@events = {}

class << self
attr_accessor :events
end
Comment on lines -14 to -18
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was left unused when event hooks were migrated to inside the stores (d612c35)


attr_accessor :web_client, :store, :url, *Config::ATTRIBUTES

protected :store_class, :store_class=
Expand Down Expand Up @@ -239,10 +233,8 @@ def dispatch(event)
end

def run_handlers(type, data)
return unless store.class.events

handlers = store.class.events[type.to_s]
handlers&.each do |handler|
handlers.each do |handler|
store.instance_exec(data, &handler)
end
rescue StandardError => e
Expand Down
20 changes: 13 additions & 7 deletions lib/slack/real_time/stores/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# frozen_string_literal: true

module Slack
module RealTime
module Stores
# Doesn't store anything.
class Base
@events = Hash.new { |h, k| h[k] = [] }

class << self
attr_accessor :events
attr_reader :events

def inherited(subclass)
super
subclass.instance_variable_set :@events, events.dup
end

def on(event, &handler)
events[event.to_s] << handler
end
end

attr_accessor :users, :bots, :channels, :groups, :teams, :ims
Expand All @@ -19,12 +31,6 @@ def team
end

def initialize(_attrs); end

def self.on(event, &block)
self.events ||= {}
self.events[event.to_s] ||= []
self.events[event.to_s] << block
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/slack/real_time/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@
end

describe '#run_handlers' do
describe 'empty events' do
context 'when store has no event hooks' do
before do
@e = client.store.class.events
client.store.class.events = nil
@events = client.store.class.events.dup
client.store.class.events.clear
end

after do
client.store.class.events = @e
client.store.class.events.merge!(@events)
end

it 'returns false when event is nil' do
expect(client.send(:run_handlers, 'example', {})).to be_nil
it 'returns empty array of handlers' do
expect(client.send(:run_handlers, 'example', {})).to be_empty
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/slack/real_time/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
expect(store.team).to be_nil
expect(store.teams.count).to eq 0
end

it 'includes event handlers from superclass' do
Slack::RealTime::Stores::Store.on :channel_created, &proc {}
expect(described_class.events.key?('channel_created')).to be true
end
end