|
21 | 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22 | 22 | // THE SOFTWARE. |
23 | 23 |
|
24 | | -internal struct EventDispatcher { |
| 24 | +internal class EventDispatcher { |
| 25 | + let client: Client |
| 26 | + let handler: EventHandler |
25 | 27 |
|
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]) { |
27 | 34 | let event = Event(event: event) |
28 | 35 | if let type = event.type { |
29 | 36 | switch type { |
30 | 37 | case .Hello: |
31 | | - EventHandler.connected() |
| 38 | + handler.connected() |
32 | 39 | case .Ok: |
33 | | - EventHandler.messageSent(event) |
| 40 | + handler.messageSent(event) |
34 | 41 | case .Message: |
35 | 42 | if (event.subtype != nil) { |
36 | 43 | messageDispatcher(event) |
37 | 44 | } else { |
38 | | - EventHandler.messageReceived(event) |
| 45 | + handler.messageReceived(event) |
39 | 46 | } |
40 | 47 | case .UserTyping: |
41 | | - EventHandler.userTyping(event) |
| 48 | + handler.userTyping(event) |
42 | 49 | case .ChannelMarked, .IMMarked, .GroupMarked: |
43 | | - EventHandler.channelMarked(event) |
| 50 | + handler.channelMarked(event) |
44 | 51 | case .ChannelCreated, .IMCreated: |
45 | | - EventHandler.channelCreated(event) |
| 52 | + handler.channelCreated(event) |
46 | 53 | case .ChannelJoined, .GroupJoined: |
47 | | - EventHandler.channelJoined(event) |
| 54 | + handler.channelJoined(event) |
48 | 55 | case .ChannelLeft, .GroupLeft: |
49 | | - EventHandler.channelLeft(event) |
| 56 | + handler.channelLeft(event) |
50 | 57 | case .ChannelDeleted: |
51 | | - EventHandler.channelDeleted(event) |
| 58 | + handler.channelDeleted(event) |
52 | 59 | case .ChannelRenamed, .GroupRename: |
53 | | - EventHandler.channelRenamed(event) |
| 60 | + handler.channelRenamed(event) |
54 | 61 | case .ChannelArchive, .GroupArchive: |
55 | | - EventHandler.channelArchived(event, archived: true) |
| 62 | + handler.channelArchived(event, archived: true) |
56 | 63 | case .ChannelUnarchive, .GroupUnarchive: |
57 | | - EventHandler.channelArchived(event, archived: false) |
| 64 | + handler.channelArchived(event, archived: false) |
58 | 65 | case .ChannelHistoryChanged, .IMHistoryChanged, .GroupHistoryChanged: |
59 | | - EventHandler.channelHistoryChanged(event) |
| 66 | + handler.channelHistoryChanged(event) |
60 | 67 | case .DNDUpdated: |
61 | | - EventHandler.doNotDisturbUpdated(event) |
| 68 | + handler.doNotDisturbUpdated(event) |
62 | 69 | case .DNDUpatedUser: |
63 | | - EventHandler.doNotDisturbUserUpdated(event) |
| 70 | + handler.doNotDisturbUserUpdated(event) |
64 | 71 | case .IMOpen, .GroupOpen: |
65 | | - EventHandler.open(event, open: true) |
| 72 | + handler.open(event, open: true) |
66 | 73 | case .IMClose, .GroupClose: |
67 | | - EventHandler.open(event, open: false) |
| 74 | + handler.open(event, open: false) |
68 | 75 | case .FileCreated: |
69 | | - EventHandler.processFile(event) |
| 76 | + handler.processFile(event) |
70 | 77 | case .FileShared: |
71 | | - EventHandler.processFile(event) |
| 78 | + handler.processFile(event) |
72 | 79 | case .FileUnshared: |
73 | | - EventHandler.processFile(event) |
| 80 | + handler.processFile(event) |
74 | 81 | case .FilePublic: |
75 | | - EventHandler.processFile(event) |
| 82 | + handler.processFile(event) |
76 | 83 | case .FilePrivate: |
77 | | - EventHandler.filePrivate(event) |
| 84 | + handler.filePrivate(event) |
78 | 85 | case .FileChanged: |
79 | | - EventHandler.processFile(event) |
| 86 | + handler.processFile(event) |
80 | 87 | case .FileDeleted: |
81 | | - EventHandler.deleteFile(event) |
| 88 | + handler.deleteFile(event) |
82 | 89 | case .FileCommentAdded: |
83 | | - EventHandler.fileCommentAdded(event) |
| 90 | + handler.fileCommentAdded(event) |
84 | 91 | case .FileCommentEdited: |
85 | | - EventHandler.fileCommentEdited(event) |
| 92 | + handler.fileCommentEdited(event) |
86 | 93 | case .FileCommentDeleted: |
87 | | - EventHandler.fileCommentDeleted(event) |
| 94 | + handler.fileCommentDeleted(event) |
88 | 95 | case .PinAdded: |
89 | | - EventHandler.pinAdded(event) |
| 96 | + handler.pinAdded(event) |
90 | 97 | case .PinRemoved: |
91 | | - EventHandler.pinRemoved(event) |
| 98 | + handler.pinRemoved(event) |
92 | 99 | case .PresenceChange: |
93 | | - EventHandler.presenceChange(event) |
| 100 | + handler.presenceChange(event) |
94 | 101 | case .ManualPresenceChange: |
95 | | - EventHandler.manualPresenceChange(event) |
| 102 | + handler.manualPresenceChange(event) |
96 | 103 | case .PrefChange: |
97 | | - EventHandler.changePreference(event) |
| 104 | + handler.changePreference(event) |
98 | 105 | case .UserChange: |
99 | | - EventHandler.userChange(event) |
| 106 | + handler.userChange(event) |
100 | 107 | case .TeamJoin: |
101 | | - EventHandler.teamJoin(event) |
| 108 | + handler.teamJoin(event) |
102 | 109 | case .StarAdded: |
103 | | - EventHandler.itemStarred(event, star: true) |
| 110 | + handler.itemStarred(event, star: true) |
104 | 111 | case .StarRemoved: |
105 | | - EventHandler.itemStarred(event, star: false) |
| 112 | + handler.itemStarred(event, star: false) |
106 | 113 | case .ReactionAdded: |
107 | | - EventHandler.addedReaction(event) |
| 114 | + handler.addedReaction(event) |
108 | 115 | case .ReactionRemoved: |
109 | | - EventHandler.removedReaction(event) |
| 116 | + handler.removedReaction(event) |
110 | 117 | case .EmojiChanged: |
111 | | - EventHandler.emojiChanged(event) |
| 118 | + handler.emojiChanged(event) |
112 | 119 | case .CommandsChanged: |
113 | 120 | // Not implemented per Slack documentation. |
114 | 121 | break |
115 | 122 | case .TeamPlanChange: |
116 | | - EventHandler.teamPlanChange(event) |
| 123 | + handler.teamPlanChange(event) |
117 | 124 | case .TeamPrefChange: |
118 | | - EventHandler.teamPreferenceChange(event) |
| 125 | + handler.teamPreferenceChange(event) |
119 | 126 | case .TeamRename: |
120 | | - EventHandler.teamNameChange(event) |
| 127 | + handler.teamNameChange(event) |
121 | 128 | case .TeamDomainChange: |
122 | | - EventHandler.teamDomainChange(event) |
| 129 | + handler.teamDomainChange(event) |
123 | 130 | case .EmailDomainChange: |
124 | | - EventHandler.emailDomainChange(event) |
| 131 | + handler.emailDomainChange(event) |
125 | 132 | case .BotAdded: |
126 | | - EventHandler.bot(event) |
| 133 | + handler.bot(event) |
127 | 134 | case .BotChanged: |
128 | | - EventHandler.bot(event) |
| 135 | + handler.bot(event) |
129 | 136 | case .AccountsChanged: |
130 | 137 | // Not implemented per Slack documentation. |
131 | 138 | break |
132 | 139 | case .TeamMigrationStarted: |
133 | 140 | Client.sharedInstance.connect() |
134 | 141 | case .SubteamCreated, .SubteamUpdated: |
135 | | - EventHandler.subteam(event) |
| 142 | + handler.subteam(event) |
136 | 143 | case .SubteamSelfAdded: |
137 | | - EventHandler.subteamAddedSelf(event) |
| 144 | + handler.subteamAddedSelf(event) |
138 | 145 | case.SubteamSelfRemoved: |
139 | | - EventHandler.subteamRemovedSelf(event) |
| 146 | + handler.subteamRemovedSelf(event) |
140 | 147 | case .Error: |
141 | 148 | print("Error: \(event)") |
142 | 149 | break |
143 | 150 | } |
144 | 151 | } |
145 | 152 | } |
146 | 153 |
|
147 | | - static func messageDispatcher(event:Event) { |
| 154 | + func messageDispatcher(event:Event) { |
148 | 155 | let subtype = MessageSubtype(rawValue: event.subtype!)! |
149 | 156 | switch subtype { |
150 | 157 | case .MessageChanged: |
151 | | - EventHandler.messageChanged(event) |
| 158 | + handler.messageChanged(event) |
152 | 159 | case .MessageDeleted: |
153 | | - EventHandler.messageDeleted(event) |
| 160 | + handler.messageDeleted(event) |
154 | 161 | default: |
155 | | - EventHandler.messageReceived(event) |
| 162 | + handler.messageReceived(event) |
156 | 163 | } |
157 | 164 | } |
158 | 165 |
|
|
0 commit comments