-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTBlockEventListener.m
68 lines (56 loc) · 1.42 KB
/
PTBlockEventListener.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
//
// PTBlockEventListener.m
// libPusher
//
// Created by Luke Redpath on 14/08/2011.
// Copyright 2011 LJR Software Limited. All rights reserved.
//
#import "PTBlockEventListener.h"
@interface PTBlockEventListener : NSObject <PTEventListener>
@end
@implementation PTBlockEventListener {
PTBlockEventListenerBlock _block;
dispatch_queue_t _queue;
BOOL _invalid;
}
- (id)initWithBlock:(PTBlockEventListenerBlock)aBlock dispatchQueue:(dispatch_queue_t)aQueue
{
NSParameterAssert(aBlock);
if ((self = [super init])) {
_block = [aBlock copy];
_queue = aQueue;
_invalid = NO;
#if !OS_OBJECT_USE_OBJC
dispatch_retain(_queue);
#endif
}
return self;
}
- (void)dealloc
{
#if !OS_OBJECT_USE_OBJC
dispatch_release(_queue);
#endif
}
- (void)invalidate
{
_invalid = YES;
}
- (void)dispatchEvent:(PTPusherEvent *)event
{
dispatch_async(_queue, ^{
if (!self->_invalid) {
self->_block(event);
}
});
}
@end
@implementation PTPusherEventDispatcher (PTBlockEventFactory)
- (PTPusherEventBinding *)addEventListenerForEventNamed:(NSString *)eventName
block:(PTBlockEventListenerBlock)block
queue:(dispatch_queue_t)queue
{
PTBlockEventListener *listener = [[PTBlockEventListener alloc] initWithBlock:block dispatchQueue:queue];
return [self addEventListener:listener forEventNamed:eventName];
}
@end