Skip to content

Commit

Permalink
Merge pull request #420 from kstole/bugfix_realtime_store
Browse files Browse the repository at this point in the history
Fix default store missing event hooks
  • Loading branch information
dblock authored Oct 4, 2022
2 parents 962acfc + 8509c7e commit 57cef8a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
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

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

0 comments on commit 57cef8a

Please sign in to comment.