Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Commit e9fc66a

Browse files
committed
Merge pull request #9 from muratayusuke/feature/multi_clients
Support for multiple client instances
2 parents fe49ca7 + 0e25584 commit e9fc66a

File tree

3 files changed

+239
-227
lines changed

3 files changed

+239
-227
lines changed

SlackKit/Sources/Client.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ public class Client: WebSocketDelegate {
5757
}
5858

5959
private var webSocket: WebSocket?
60-
60+
private var dispatcher: EventDispatcher?
61+
6162
required public init() {
62-
6363
}
6464

6565
public static let sharedInstance = Client()
6666

6767
//MARK: - Connection
6868
public func connect() {
69+
dispatcher = EventDispatcher(client: self)
6970
let request = NSURLRequest(URL: NSURL(string:"https://slack.com/api/rtm.start?token="+token)!)
7071
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()!) {
7172
(response, data, error) -> Void in
@@ -233,7 +234,7 @@ public class Client: WebSocketDelegate {
233234
return
234235
}
235236
do {
236-
try EventDispatcher.eventDispatcher(NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [String: AnyObject])
237+
try dispatcher?.dispatch(NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [String: AnyObject])
237238
}
238239
catch _ {
239240

SlackKit/Sources/EventDispatcher.swift

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,138 +21,145 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
internal struct EventDispatcher {
24+
internal class EventDispatcher {
25+
let client: Client
26+
let handler: EventHandler
2527

26-
static func eventDispatcher(event: [String: AnyObject]) {
28+
required init(client: Client) {
29+
self.client = client
30+
handler = EventHandler(client: client)
31+
}
32+
33+
func dispatch(event: [String: AnyObject]) {
2734
let event = Event(event: event)
2835
if let type = event.type {
2936
switch type {
3037
case .Hello:
31-
EventHandler.connected()
38+
handler.connected()
3239
case .Ok:
33-
EventHandler.messageSent(event)
40+
handler.messageSent(event)
3441
case .Message:
3542
if (event.subtype != nil) {
3643
messageDispatcher(event)
3744
} else {
38-
EventHandler.messageReceived(event)
45+
handler.messageReceived(event)
3946
}
4047
case .UserTyping:
41-
EventHandler.userTyping(event)
48+
handler.userTyping(event)
4249
case .ChannelMarked, .IMMarked, .GroupMarked:
43-
EventHandler.channelMarked(event)
50+
handler.channelMarked(event)
4451
case .ChannelCreated, .IMCreated:
45-
EventHandler.channelCreated(event)
52+
handler.channelCreated(event)
4653
case .ChannelJoined, .GroupJoined:
47-
EventHandler.channelJoined(event)
54+
handler.channelJoined(event)
4855
case .ChannelLeft, .GroupLeft:
49-
EventHandler.channelLeft(event)
56+
handler.channelLeft(event)
5057
case .ChannelDeleted:
51-
EventHandler.channelDeleted(event)
58+
handler.channelDeleted(event)
5259
case .ChannelRenamed, .GroupRename:
53-
EventHandler.channelRenamed(event)
60+
handler.channelRenamed(event)
5461
case .ChannelArchive, .GroupArchive:
55-
EventHandler.channelArchived(event, archived: true)
62+
handler.channelArchived(event, archived: true)
5663
case .ChannelUnarchive, .GroupUnarchive:
57-
EventHandler.channelArchived(event, archived: false)
64+
handler.channelArchived(event, archived: false)
5865
case .ChannelHistoryChanged, .IMHistoryChanged, .GroupHistoryChanged:
59-
EventHandler.channelHistoryChanged(event)
66+
handler.channelHistoryChanged(event)
6067
case .DNDUpdated:
61-
EventHandler.doNotDisturbUpdated(event)
68+
handler.doNotDisturbUpdated(event)
6269
case .DNDUpatedUser:
63-
EventHandler.doNotDisturbUserUpdated(event)
70+
handler.doNotDisturbUserUpdated(event)
6471
case .IMOpen, .GroupOpen:
65-
EventHandler.open(event, open: true)
72+
handler.open(event, open: true)
6673
case .IMClose, .GroupClose:
67-
EventHandler.open(event, open: false)
74+
handler.open(event, open: false)
6875
case .FileCreated:
69-
EventHandler.processFile(event)
76+
handler.processFile(event)
7077
case .FileShared:
71-
EventHandler.processFile(event)
78+
handler.processFile(event)
7279
case .FileUnshared:
73-
EventHandler.processFile(event)
80+
handler.processFile(event)
7481
case .FilePublic:
75-
EventHandler.processFile(event)
82+
handler.processFile(event)
7683
case .FilePrivate:
77-
EventHandler.filePrivate(event)
84+
handler.filePrivate(event)
7885
case .FileChanged:
79-
EventHandler.processFile(event)
86+
handler.processFile(event)
8087
case .FileDeleted:
81-
EventHandler.deleteFile(event)
88+
handler.deleteFile(event)
8289
case .FileCommentAdded:
83-
EventHandler.fileCommentAdded(event)
90+
handler.fileCommentAdded(event)
8491
case .FileCommentEdited:
85-
EventHandler.fileCommentEdited(event)
92+
handler.fileCommentEdited(event)
8693
case .FileCommentDeleted:
87-
EventHandler.fileCommentDeleted(event)
94+
handler.fileCommentDeleted(event)
8895
case .PinAdded:
89-
EventHandler.pinAdded(event)
96+
handler.pinAdded(event)
9097
case .PinRemoved:
91-
EventHandler.pinRemoved(event)
98+
handler.pinRemoved(event)
9299
case .PresenceChange:
93-
EventHandler.presenceChange(event)
100+
handler.presenceChange(event)
94101
case .ManualPresenceChange:
95-
EventHandler.manualPresenceChange(event)
102+
handler.manualPresenceChange(event)
96103
case .PrefChange:
97-
EventHandler.changePreference(event)
104+
handler.changePreference(event)
98105
case .UserChange:
99-
EventHandler.userChange(event)
106+
handler.userChange(event)
100107
case .TeamJoin:
101-
EventHandler.teamJoin(event)
108+
handler.teamJoin(event)
102109
case .StarAdded:
103-
EventHandler.itemStarred(event, star: true)
110+
handler.itemStarred(event, star: true)
104111
case .StarRemoved:
105-
EventHandler.itemStarred(event, star: false)
112+
handler.itemStarred(event, star: false)
106113
case .ReactionAdded:
107-
EventHandler.addedReaction(event)
114+
handler.addedReaction(event)
108115
case .ReactionRemoved:
109-
EventHandler.removedReaction(event)
116+
handler.removedReaction(event)
110117
case .EmojiChanged:
111-
EventHandler.emojiChanged(event)
118+
handler.emojiChanged(event)
112119
case .CommandsChanged:
113120
// Not implemented per Slack documentation.
114121
break
115122
case .TeamPlanChange:
116-
EventHandler.teamPlanChange(event)
123+
handler.teamPlanChange(event)
117124
case .TeamPrefChange:
118-
EventHandler.teamPreferenceChange(event)
125+
handler.teamPreferenceChange(event)
119126
case .TeamRename:
120-
EventHandler.teamNameChange(event)
127+
handler.teamNameChange(event)
121128
case .TeamDomainChange:
122-
EventHandler.teamDomainChange(event)
129+
handler.teamDomainChange(event)
123130
case .EmailDomainChange:
124-
EventHandler.emailDomainChange(event)
131+
handler.emailDomainChange(event)
125132
case .BotAdded:
126-
EventHandler.bot(event)
133+
handler.bot(event)
127134
case .BotChanged:
128-
EventHandler.bot(event)
135+
handler.bot(event)
129136
case .AccountsChanged:
130137
// Not implemented per Slack documentation.
131138
break
132139
case .TeamMigrationStarted:
133140
Client.sharedInstance.connect()
134141
case .SubteamCreated, .SubteamUpdated:
135-
EventHandler.subteam(event)
142+
handler.subteam(event)
136143
case .SubteamSelfAdded:
137-
EventHandler.subteamAddedSelf(event)
144+
handler.subteamAddedSelf(event)
138145
case.SubteamSelfRemoved:
139-
EventHandler.subteamRemovedSelf(event)
146+
handler.subteamRemovedSelf(event)
140147
case .Error:
141148
print("Error: \(event)")
142149
break
143150
}
144151
}
145152
}
146153

147-
static func messageDispatcher(event:Event) {
154+
func messageDispatcher(event:Event) {
148155
let subtype = MessageSubtype(rawValue: event.subtype!)!
149156
switch subtype {
150157
case .MessageChanged:
151-
EventHandler.messageChanged(event)
158+
handler.messageChanged(event)
152159
case .MessageDeleted:
153-
EventHandler.messageDeleted(event)
160+
handler.messageDeleted(event)
154161
default:
155-
EventHandler.messageReceived(event)
162+
handler.messageReceived(event)
156163
}
157164
}
158165

0 commit comments

Comments
 (0)