From 4635ea36613bb26064d593fb1847b6724192bc46 Mon Sep 17 00:00:00 2001 From: Daniel Rhodes Date: Sat, 27 Aug 2016 22:45:12 +0200 Subject: [PATCH] Fixes race condition with auto-aubscribing --- Pod/Classes/ActionCableClient.swift | 19 ++++++++++--------- Pod/Classes/Channel.swift | 4 ---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Pod/Classes/ActionCableClient.swift b/Pod/Classes/ActionCableClient.swift index 6fc5c2f..55a08a9 100644 --- a/Pod/Classes/ActionCableClient.swift +++ b/Pod/Classes/ActionCableClient.swift @@ -204,14 +204,10 @@ extension ActionCableClient { @warn_unused_result(message="You must hold on to the Channel returned from a create(_:)") public func create(name: String, identifier: ChannelIdentifier?, autoSubscribe: Bool=true, bufferActions: Bool=true) -> Channel { // Look in existing channels and return that - if let channel = channels[name] { - return channel - } + if let channel = channels[name] { return channel } // Look in unconfirmed channels and return that - if let channel = unconfirmedChannels[name] { - return channel - } + if let channel = unconfirmedChannels[name] { return channel } // Otherwise create a new one let channel = Channel(name: name, @@ -219,8 +215,12 @@ extension ActionCableClient { client: self, autoSubscribe: autoSubscribe, shouldBufferActions: bufferActions) - + self.unconfirmedChannels[name] = channel + + if (channel.autoSubscribe) { + subscribe(channel) + } return channel } @@ -240,11 +240,12 @@ extension ActionCableClient { internal func subscribe(channel: Channel) { // Is it already added and subscribed? - if let existingChannel = channels[channel.name] where (existingChannel == channel) && (existingChannel.subscribed) { + if let existingChannel = channels[channel.name] where (existingChannel == channel) { return } - unconfirmedChannels.updateValue(channel, forKey: channel.name) + guard let channel = unconfirmedChannels[channel.name] + else { debugPrint("[ActionCableClient] Internal inconsistency error!"); return } do { try self.transmit(channel, command: Command.Subscribe, data: nil) diff --git a/Pod/Classes/Channel.swift b/Pod/Classes/Channel.swift index 6ef71f5..0d622a0 100644 --- a/Pod/Classes/Channel.swift +++ b/Pod/Classes/Channel.swift @@ -87,10 +87,6 @@ public class Channel: Hashable, Equatable { self.autoSubscribe = autoSubscribe self.shouldBufferActions = shouldBufferActions self.identifier = identifier - - if (self.autoSubscribe) { - subscribe() - } } public func onReceive(action:String, handler: (OnReceiveClosure)) -> Void {