Skip to content

Commit 4635ea3

Browse files
committed
Fixes race condition with auto-aubscribing
1 parent 1115da7 commit 4635ea3

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

Pod/Classes/ActionCableClient.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,23 @@ extension ActionCableClient {
204204
@warn_unused_result(message="You must hold on to the Channel returned from a create(_:)")
205205
public func create(name: String, identifier: ChannelIdentifier?, autoSubscribe: Bool=true, bufferActions: Bool=true) -> Channel {
206206
// Look in existing channels and return that
207-
if let channel = channels[name] {
208-
return channel
209-
}
207+
if let channel = channels[name] { return channel }
210208

211209
// Look in unconfirmed channels and return that
212-
if let channel = unconfirmedChannels[name] {
213-
return channel
214-
}
210+
if let channel = unconfirmedChannels[name] { return channel }
215211

216212
// Otherwise create a new one
217213
let channel = Channel(name: name,
218214
identifier: identifier,
219215
client: self,
220216
autoSubscribe: autoSubscribe,
221217
shouldBufferActions: bufferActions)
222-
218+
223219
self.unconfirmedChannels[name] = channel
220+
221+
if (channel.autoSubscribe) {
222+
subscribe(channel)
223+
}
224224

225225
return channel
226226
}
@@ -240,11 +240,12 @@ extension ActionCableClient {
240240

241241
internal func subscribe(channel: Channel) {
242242
// Is it already added and subscribed?
243-
if let existingChannel = channels[channel.name] where (existingChannel == channel) && (existingChannel.subscribed) {
243+
if let existingChannel = channels[channel.name] where (existingChannel == channel) {
244244
return
245245
}
246246

247-
unconfirmedChannels.updateValue(channel, forKey: channel.name)
247+
guard let channel = unconfirmedChannels[channel.name]
248+
else { debugPrint("[ActionCableClient] Internal inconsistency error!"); return }
248249

249250
do {
250251
try self.transmit(channel, command: Command.Subscribe, data: nil)

Pod/Classes/Channel.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ public class Channel: Hashable, Equatable {
8787
self.autoSubscribe = autoSubscribe
8888
self.shouldBufferActions = shouldBufferActions
8989
self.identifier = identifier
90-
91-
if (self.autoSubscribe) {
92-
subscribe()
93-
}
9490
}
9591

9692
public func onReceive(action:String, handler: (OnReceiveClosure)) -> Void {

0 commit comments

Comments
 (0)