forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASHandle.m
130 lines (104 loc) · 3.05 KB
/
ASHandle.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
#import "ASHandle.h"
#import "ASWatcher.h"
#import "NSObject+TGLock.h"
@interface ASHandle ()
{
TG_SYNCHRONIZED_DEFINE(_delegate);
}
@end
@implementation ASHandle
@synthesize delegate = _delegate;
@synthesize releaseOnMainThread = _releaseOnMainThread;
- (id)initWithDelegate:(id<ASWatcher>)delegate
{
self = [super init];
if (self != nil)
{
TG_SYNCHRONIZED_INIT(_delegate);
_delegate = delegate;
}
return self;
}
- (id)initWithDelegate:(id<ASWatcher>)delegate releaseOnMainThread:(bool)releaseOnMainThread
{
self = [super init];
if (self != nil)
{
TG_SYNCHRONIZED_INIT(_delegate);
_delegate = delegate;
_releaseOnMainThread = releaseOnMainThread;
}
return self;
}
- (void)reset
{
TG_SYNCHRONIZED_BEGIN(_delegate);
_delegate = nil;
TG_SYNCHRONIZED_END(_delegate);
}
- (bool)hasDelegate
{
bool result = false;
TG_SYNCHRONIZED_BEGIN(_delegate);
result = _delegate != nil;
TG_SYNCHRONIZED_END(_delegate);
return result;
}
- (id<ASWatcher>)delegate
{
id<ASWatcher> result = nil;
TG_SYNCHRONIZED_BEGIN(_delegate);
result = _delegate;
TG_SYNCHRONIZED_END(_delegate);
return result;
}
- (void)setDelegate:(id<ASWatcher>)delegate
{
TG_SYNCHRONIZED_BEGIN(_delegate);
_delegate = delegate;
TG_SYNCHRONIZED_END(_delegate);
}
- (void)requestAction:(NSString *)action options:(id)options
{
__strong id<ASWatcher> delegate = self.delegate;
if (delegate != nil && [delegate respondsToSelector:@selector(actionStageActionRequested:options:)])
[delegate actionStageActionRequested:action options:options];
if (_releaseOnMainThread && ![NSThread isMainThread])
{
dispatch_async(dispatch_get_main_queue(), ^
{
[delegate class];
});
}
}
- (void)receiveActorMessage:(NSString *)path messageType:(NSString *)messageType message:(id)message
{
__strong id<ASWatcher> delegate = self.delegate;
if (delegate != nil && [delegate respondsToSelector:@selector(actorMessageReceived:messageType:message:)])
[delegate actorMessageReceived:path messageType:messageType message:message];
if (_releaseOnMainThread && ![NSThread isMainThread])
{
dispatch_async(dispatch_get_main_queue(), ^
{
[delegate class];
});
}
}
- (void)notifyResourceDispatched:(NSString *)path resource:(id)resource
{
[self notifyResourceDispatched:path resource:resource arguments:nil];
}
- (void)notifyResourceDispatched:(NSString *)path resource:(id)resource arguments:(id)arguments
{
__strong id<ASWatcher> delegate = self.delegate;
if (delegate != nil && [delegate respondsToSelector:@selector(actionStageResourceDispatched:resource:arguments:)])
[delegate actionStageResourceDispatched:path resource:resource arguments:arguments];
if (_releaseOnMainThread && ![NSThread isMainThread])
{
dispatch_async(dispatch_get_main_queue(), ^
{
[delegate class];
});
}
}
@end