-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTPusherChannel.m
444 lines (353 loc) · 11.3 KB
/
PTPusherChannel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//
// PTPusherClient.m
// libPusher
//
// Created by Luke Redpath on 23/04/2010.
// Copyright 2010 LJR Software Limited. All rights reserved.
//
#import "PTPusherChannel.h"
#import "PTPusher.h"
#import "PTPusherEvent.h"
#import "PTPusherEventDispatcher.h"
#import "PTTargetActionEventListener.h"
#import "PTBlockEventListener.h"
#import "PTPusherErrors.h"
#import "PTJSON.h"
#import "NSDictionary+StringValue.h"
@interface PTPusher ()
- (void)__unsubscribeFromChannel:(PTPusherChannel *)channel;
@end
@interface PTPusherChannel ()
@property (nonatomic, weak) PTPusher *pusher;
@property (nonatomic, strong) PTPusherEventDispatcher *dispatcher;
@property (nonatomic, assign, readwrite) BOOL subscribed;
@property (nonatomic, readonly) NSMutableArray *internalBindings;
@end
#pragma mark -
@implementation PTPusherChannel
+ (instancetype)channelWithName:(NSString *)name pusher:(PTPusher *)pusher
{
if ([name hasPrefix:@"private-"]) {
return [[PTPusherPrivateChannel alloc] initWithName:name pusher:pusher];
}
if ([name hasPrefix:@"presence-"]) {
return [[PTPusherPresenceChannel alloc] initWithName:name pusher:pusher];
}
return [[self alloc] initWithName:name pusher:pusher];
}
- (id)initWithName:(NSString *)channelName pusher:(PTPusher *)aPusher
{
if (self = [super init]) {
_name = [channelName copy];
_pusher = aPusher;
_dispatcher = [[PTPusherEventDispatcher alloc] init];
_internalBindings = [[NSMutableArray alloc] init];
/*
Set up event handlers for pre-defined channel events
We *must* use block-based bindings with a weak reference to the channel.
Using a target-action binding will create a retain cycle between the channel
and the target/action binding object.
*/
__weak PTPusherChannel *weakChannel = self;
[self.internalBindings addObject:
[self bindToEventNamed:@"pusher_internal:subscription_succeeded"
handleWithBlock:^(PTPusherEvent *event) {
[weakChannel handleSubscribeEvent:event];
}]];
[self.internalBindings addObject:
[self bindToEventNamed:@"subscription_error"
handleWithBlock:^(PTPusherEvent *event) {
[weakChannel handleSubcribeErrorEvent:event];
}]];
}
return self;
}
- (void)dealloc
{
[self.internalBindings enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[self.dispatcher removeBinding:object];
}];
}
- (BOOL)isPrivate
{
return NO;
}
- (BOOL)isPresence
{
return NO;
}
#pragma mark - Subscription events
- (void)handleSubscribeEvent:(PTPusherEvent *)event
{
self.subscribed = YES;
if ([self.pusher.delegate respondsToSelector:@selector(pusher:didSubscribeToChannel:)]) {
[self.pusher.delegate pusher:self.pusher didSubscribeToChannel:self];
}
}
- (void)handleSubcribeErrorEvent:(PTPusherEvent *)event
{
if ([self.pusher.delegate respondsToSelector:@selector(pusher:didFailToSubscribeToChannel:withError:)]) {
NSDictionary *userInfo = @{PTPusherErrorUnderlyingEventKey: event};
NSError *error = [NSError errorWithDomain:PTPusherErrorDomain code:PTPusherSubscriptionError userInfo:userInfo];
[self.pusher.delegate pusher:self.pusher didFailToSubscribeToChannel:self withError:error];
}
}
#pragma mark - Binding to events
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName target:(id)target action:(SEL)selector
{
return [self.dispatcher addEventListenerForEventNamed:eventName target:target action:selector];
}
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName handleWithBlock:(PTPusherEventBlockHandler)block
{
return [self bindToEventNamed:eventName handleWithBlock:block queue:dispatch_get_main_queue()];
}
- (PTPusherEventBinding *)bindToEventNamed:(NSString *)eventName handleWithBlock:(PTPusherEventBlockHandler)block queue:(dispatch_queue_t)queue
{
return [self.dispatcher addEventListenerForEventNamed:eventName block:block queue:queue];
}
- (void)removeBinding:(PTPusherEventBinding *)binding
{
[self.dispatcher removeBinding:binding];
}
- (void)removeAllBindings
{
NSMutableArray *bindingsToRemove = [NSMutableArray array];
// need to unpack the bindings from the nested arrays, so we can
// iterate over them safely whilst removing them from the dispatcher
for (NSArray *bindingsArray in [self.dispatcher.bindings allValues]) {
for (PTPusherEventBinding *binding in bindingsArray) {
if (![self.internalBindings containsObject:binding]) {
[bindingsToRemove addObject:binding];
}
}
}
for (PTPusherEventBinding *binding in bindingsToRemove) {
[self.dispatcher removeBinding:binding];
}
}
#pragma mark - Dispatching events
- (void)dispatchEvent:(PTPusherEvent *)event
{
[[NSNotificationCenter defaultCenter]
postNotificationName:PTPusherEventReceivedNotification
object:self
userInfo:@{PTPusherEventUserInfoKey: event}];
[self.dispatcher dispatchEvent:event];
}
#pragma mark - Internal use only
- (void)subscribeWithAuthorization:(NSDictionary *)authData
{
if (self.isSubscribed) return;
[self.pusher sendEventNamed:@"pusher:subscribe"
data:@{@"channel": self.name}
channel:nil];
}
- (void)unsubscribe
{
[self.pusher __unsubscribeFromChannel:self];
}
- (void)handleDisconnect
{
self.subscribed = NO;
}
@end
#pragma mark -
@implementation PTPusherPrivateChannel {
NSOperationQueue *_clientEventQueue;
}
- (id)initWithName:(NSString *)channelName pusher:(PTPusher *)aPusher
{
if ((self = [super initWithName:channelName pusher:aPusher])) {
_clientEventQueue = [[NSOperationQueue alloc] init];
_clientEventQueue.maxConcurrentOperationCount = 1;
_clientEventQueue.name = @"com.pusher.libPusher.clientEventQueue";
_clientEventQueue.suspended = YES;
}
return self;
}
- (void)handleSubscribeEvent:(PTPusherEvent *)event
{
[super handleSubscribeEvent:event];
[_clientEventQueue setSuspended:NO];
}
- (void)handleDisconnect
{
[super handleDisconnect];
[_clientEventQueue setSuspended:YES];
}
- (BOOL)isPrivate
{
return YES;
}
- (void)subscribeWithAuthorization:(NSDictionary *)authData
{
if (self.isSubscribed) return;
NSMutableDictionary *eventData = [authData mutableCopy];
eventData[@"channel"] = self.name;
[self.pusher sendEventNamed:@"pusher:subscribe"
data:eventData
channel:nil];
}
#pragma mark - Triggering events
- (void)triggerEventNamed:(NSString *)eventName data:(id)eventData
{
if (![eventName hasPrefix:@"client-"]) {
eventName = [@"client-" stringByAppendingString:eventName];
}
__weak PTPusherChannel *weakSelf = self;
[_clientEventQueue addOperationWithBlock:^{
[weakSelf.pusher sendEventNamed:eventName data:eventData channel:weakSelf.name];
}];
}
@end
#pragma mark -
@interface PTPusherChannelMembers ()
@property (nonatomic, copy, readwrite) NSString *myID;
- (void)reset;
- (void)handleSubscription:(NSDictionary *)subscriptionData;
- (PTPusherChannelMember *)handleMemberAdded:(NSDictionary *)memberData;
- (PTPusherChannelMember *)handleMemberRemoved:(NSDictionary *)memberData;
@end
@implementation PTPusherPresenceChannel
- (id)initWithName:(NSString *)channelName pusher:(PTPusher *)aPusher
{
if ((self = [super initWithName:channelName pusher:aPusher])) {
_members = [[PTPusherChannelMembers alloc] init];
/* Set up event handlers for pre-defined channel events.
As above, use blocks as proxies to a weak channel reference to avoid retain cycles.
*/
__weak PTPusherPresenceChannel *weakChannel = self;
[self.internalBindings addObject:
[self bindToEventNamed:@"pusher_internal:member_added"
handleWithBlock:^(PTPusherEvent *event) {
[weakChannel handleMemberAddedEvent:event];
}]];
[self.internalBindings addObject:
[self bindToEventNamed:@"pusher_internal:member_removed"
handleWithBlock:^(PTPusherEvent *event) {
[weakChannel handleMemberRemovedEvent:event];
}]];
}
return self;
}
- (void)handleDisconnect
{
[super handleDisconnect];
[self.members reset];
}
- (void)subscribeWithAuthorization:(NSDictionary *)authData
{
[super subscribeWithAuthorization:authData];
NSDictionary *channelData = [[PTJSON JSONParser] objectFromJSONString:authData[@"channel_data"]];
self.members.myID = [channelData stringValueForKey:@"user_id"];
}
- (void)handleSubscribeEvent:(PTPusherEvent *)event
{
[super handleSubscribeEvent:event];
[self.members handleSubscription:event.data];
[self.presenceDelegate presenceChannelDidSubscribe:self];
}
- (BOOL)isPresence
{
return YES;
}
- (void)handleMemberAddedEvent:(PTPusherEvent *)event
{
PTPusherChannelMember *member = [self.members handleMemberAdded:event.data];
[self.presenceDelegate presenceChannel:self memberAdded:member];
}
- (void)handleMemberRemovedEvent:(PTPusherEvent *)event
{
PTPusherChannelMember *member = [self.members handleMemberRemoved:event.data];
[self.presenceDelegate presenceChannel:self memberRemoved:member];
}
@end
#pragma mark -
@implementation PTPusherChannelMember
- (id)initWithUserID:(NSString *)userID userInfo:(NSDictionary *)userInfo
{
if ((self = [super init])) {
_userID = [userID copy];
_userInfo = [userInfo copy];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<PTPusherChannelMember id:%@ info:%@>", self.userID, self.userInfo];
}
- (id)objectForKeyedSubscript:(id <NSCopying>)key
{
return self.userInfo[key];
}
@end
@implementation PTPusherChannelMembers {
NSMutableDictionary *_members;
}
- (id)init
{
self = [super init];
if (self) {
_members = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)reset
{
_members = [[NSMutableDictionary alloc] init];
self.myID = nil;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<PTPusherChannelMembers members:%@>", _members];
}
- (NSInteger)count
{
return _members.count;
}
- (id)objectForKeyedSubscript:(id <NSCopying>)key
{
return _members[key];
}
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block
{
[_members enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
block(obj, stop);
}];
}
- (PTPusherChannelMember *)me
{
return self[self.myID];
}
- (PTPusherChannelMember *)memberWithID:(NSString *)userID
{
return self[userID];
}
#pragma mark - Channel event handling
- (void)handleSubscription:(NSDictionary *)subscriptionData
{
NSDictionary *memberHash = subscriptionData[@"presence"][@"hash"];
[memberHash enumerateKeysAndObjectsUsingBlock:^(NSString *userID, NSDictionary *userInfo, BOOL *stop) {
PTPusherChannelMember *member = [[PTPusherChannelMember alloc] initWithUserID:userID userInfo:userInfo];
self->_members[userID] = member;
}];
}
- (PTPusherChannelMember *)handleMemberAdded:(NSDictionary *)memberData
{
NSString *userID = [memberData stringValueForKey:@"user_id"];
PTPusherChannelMember *member = [self memberWithID:userID];
if (member == nil) {
member = [[PTPusherChannelMember alloc] initWithUserID:userID userInfo:memberData[@"user_info"]];
_members[member.userID] = member;
}
return member;
}
- (PTPusherChannelMember *)handleMemberRemoved:(NSDictionary *)memberData
{
PTPusherChannelMember *member = [self memberWithID:[memberData stringValueForKey:@"user_id"]];
if (member) {
[_members removeObjectForKey:member.userID];
}
return member;
}
@end