Skip to content

Commit

Permalink
Fixes race condition with auto-aubscribing
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrhodes committed Aug 27, 2016
1 parent 1115da7 commit 4635ea3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 10 additions & 9 deletions Pod/Classes/ActionCableClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,23 @@ 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,
identifier: identifier,
client: self,
autoSubscribe: autoSubscribe,
shouldBufferActions: bufferActions)

self.unconfirmedChannels[name] = channel

if (channel.autoSubscribe) {
subscribe(channel)
}

return channel
}
Expand All @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions Pod/Classes/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4635ea3

Please sign in to comment.