From 669c623c8780bd2072d754b864c53c9d28f96588 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 28 Oct 2015 22:06:16 -0400 Subject: [PATCH] Defer users and channels creation --- lib/lita/adapters/slack/rtm_connection.rb | 15 ++++++++++++--- spec/lita/adapters/slack/rtm_connection_spec.rb | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/lita/adapters/slack/rtm_connection.rb b/lib/lita/adapters/slack/rtm_connection.rb index 0d416e0..d167761 100644 --- a/lib/lita/adapters/slack/rtm_connection.rb +++ b/lib/lita/adapters/slack/rtm_connection.rb @@ -17,7 +17,8 @@ class RTMConnection class << self def build(robot, config) - new(robot, config, API.new(config).rtm_start) + team_data = API.new(config).rtm_start + new(robot, config, team_data) end end @@ -28,8 +29,12 @@ def initialize(robot, config, team_data) @websocket_url = team_data.websocket_url @robot_id = team_data.self.id - UserCreator.create_users(team_data.users, robot, robot_id) - RoomCreator.create_rooms(team_data.channels, robot) + defer do + log.debug("Inserting #{team_data.users.size} users") + UserCreator.create_users(team_data.users, robot, robot_id) + log.debug("Inserting #{team_data.channels.size} channels") + RoomCreator.create_rooms(team_data.channels, robot) + end end def im_for(user_id) @@ -81,6 +86,10 @@ def shut_down attr_reader :websocket attr_reader :websocket_url + def defer(&block) + Thread.new(&block) + end + def log Lita.logger end diff --git a/spec/lita/adapters/slack/rtm_connection_spec.rb b/spec/lita/adapters/slack/rtm_connection_spec.rb index d733a80..e802478 100644 --- a/spec/lita/adapters/slack/rtm_connection_spec.rb +++ b/spec/lita/adapters/slack/rtm_connection_spec.rb @@ -48,12 +48,14 @@ def with_websocket(subject, queue) it "creates users with the results of rtm.start data" do expect(Lita::Adapters::Slack::UserCreator).to receive(:create_users) + expect_any_instance_of(described_class).to receive(:defer).and_yield described_class.build(robot, config) end it "creates rooms with the results of rtm.start data" do expect(Lita::Adapters::Slack::RoomCreator).to receive(:create_rooms) + expect_any_instance_of(described_class).to receive(:defer).and_yield described_class.build(robot, config) end