Skip to content

Commit 55e0d47

Browse files
Merge pull request #32 from ScalaInc/fix/events-clone
Fix/events clone
2 parents d1c7251 + e550442 commit 55e0d47

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Pod/Classes/Channel.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import PromiseKit
1414
public class Channel: ChannelProtocol {
1515

1616
public typealias CallBackType = [String: AnyObject] -> Void
17-
var listeners = [String: CallBackType]()
17+
var listeners = [String: [CallBackType]]()
1818
var responders = [String: CallBackType]()
1919
var socketManager:SocketManager
2020
var channelName:String
@@ -47,7 +47,7 @@ public class Channel: ChannelProtocol {
4747
@return Promise<Any>
4848
*/
4949
public func listen(name:String, callback:CallBackType)->Promise<Any>{
50-
listeners.updateValue(callback, forKey: name)
50+
listeners.updateValue(createSubList(name, callback: callback), forKey: name)
5151
return subscribe(generateId())
5252
}
5353

@@ -67,8 +67,10 @@ public class Channel: ChannelProtocol {
6767
*/
6868
public func onBroadcast(dic: [String : AnyObject]) {
6969
let name = dic["name"] as! String
70-
if let callBack = self.listeners[name] {
71-
callBack(dic)
70+
if let subscriberList = self.listeners[name] {
71+
for callBack in subscriberList {
72+
callBack(dic)
73+
}
7274
}
7375
}
7476

@@ -111,5 +113,22 @@ public class Channel: ChannelProtocol {
111113
expLogging("IDENTIFY \(msg) ")
112114
broadCast("2000",params: msg)
113115
}
116+
117+
/**
118+
* Create Subscriber list
119+
* @param name
120+
* @param callback
121+
* @return
122+
*/
123+
private func createSubList(name:String, callback:CallBackType)->[CallBackType]{
124+
var subscriberArray: [CallBackType] = []
125+
if let listCallback = self.listeners[name] {
126+
subscriberArray = listCallback
127+
}else{
128+
self.listeners[name] = subscriberArray
129+
}
130+
subscriberArray.append(callback)
131+
return subscriberArray
132+
}
114133

115134
}

0 commit comments

Comments
 (0)