-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSObject+BTPubSub.m
281 lines (244 loc) · 10.5 KB
/
NSObject+BTPubSub.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
//
// NSObject+BTPubSub.m
// GoodLawyer_UserClient
//
// Created by truestyle on 16/5/11.
//
#import "NSObject+BTPubSub.h"
#import <objc/runtime.h>
#pragma mark - Utilities
static BOOL isEmptyString(NSString *string)
{
return (![string isKindOfClass:[NSString class]] ||
!string ||
[string isEqual:[NSNull null]] ||
([string isKindOfClass:[NSString class]] && [@"" isEqualToString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]));
}
static void swizzlingMethods(NSString *originalSelectorName, NSString *swizzledSelectorName, id obj){
if(!obj)
return;
if(isEmptyString(originalSelectorName) || isEmptyString(swizzledSelectorName)){
return;
}
Class aClass = [obj class];
SEL originalSelector = NSSelectorFromString(originalSelectorName);
SEL swizzledSelector = NSSelectorFromString(swizzledSelectorName);
Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector);
BOOL success = class_addMethod(aClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(aClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@implementation BTEvent
- (id)initWithName:(NSString *)name obj:(id)obj data:(id)data {
if (self = [super init]) {
self.name = name;
self.obj = obj;
self.data = data;
}
return self;
}
@end
#define weakify(o) autoreleasepool{} __weak typeof(o) o##Weak = o;
#define strongify(o) autoreleasepool{} __strong typeof(o) o = o##Weak;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation NSObject(BTPubSub)
static NSOperationQueue *_pubSubQueue = nil;
static NSString * const kBTPubSubDataKey = @"BTPubSubData";
static char kBTPubSubSubscriptionsKey;
#pragma mark - Class Methods
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
swizzlingMethods(@"dealloc",@"bt_dealloc",self);
});
}
+ (void)setPubSubQueue:(NSOperationQueue *)queue {
_pubSubQueue = queue;
}
#pragma mark - Publish Methods
- (void)publish:(NSString *)name {
[self publish:name data:nil];
}
- (void)publish:(NSString *)name data:(id)data {
if(isEmptyString(name))
return;
NSDictionary *userInfo = nil;
if (data != nil) {
userInfo = @{kBTPubSubDataKey: data};
}
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:userInfo];
}
- (void)subscribeEvent:(NSString *)eventName{
if(!isEmptyString(eventName))
[self subscribeEvents:@[eventName]];
}
- (void)subscribeEvents:(NSArray<NSString *> *)eventNameArray{
if(nil == eventNameArray){
return;
}
for(NSString *eventName in eventNameArray){
if(isEmptyString(eventName))
break;
@autoreleasepool{
@weakify(self);
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:eventName object:nil queue:_pubSubQueue usingBlock:^(NSNotification *note) {
@strongify(self);
id data = [note.userInfo objectForKey:kBTPubSubDataKey] ? [note.userInfo objectForKey:kBTPubSubDataKey] : note.userInfo;
BTEvent *event = [[BTEvent alloc] initWithName:eventName obj:note.object data:data];
if([self respondsToSelector:@selector(handlePublishedEvent:)])
[self handlePublishedEvent:event];
}];
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions) {
subscriptions = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, &kBTPubSubSubscriptionsKey, subscriptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSMutableSet *observers = [subscriptions objectForKey:eventName];
if (!observers) {
observers = [[NSMutableSet alloc] init];
[subscriptions setObject:observers forKey:eventName];
}
[observers addObject:observer];
}
}
}
- (void)subscribeOnceEvent:(NSString *)eventName{
if(!isEmptyString(eventName))
[self subscribeOnceEvents:@[eventName]];
}
- (void)subscribeOnceEvents:(NSArray<NSString *> *)eventNameArray{
if(nil == eventNameArray){
return;
}
for(NSString *eventName in eventNameArray){
if(isEmptyString(eventName))
break;
@autoreleasepool{
@weakify(self);
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:eventName object:nil queue:_pubSubQueue usingBlock:^(NSNotification *note) {
@strongify(self);
id data = [note.userInfo objectForKey:kBTPubSubDataKey] ? [note.userInfo objectForKey:kBTPubSubDataKey] : note.userInfo;
BTEvent *event = [[BTEvent alloc] initWithName:eventName obj:note.object data:data];
if([self respondsToSelector:@selector(handlePublishedEvent:)]){
[self handlePublishedEvent:event];
[self unsubscribeEvent:event.name];
}
}];
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions) {
subscriptions = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, &kBTPubSubSubscriptionsKey, subscriptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSMutableSet *observers = [subscriptions objectForKey:eventName];
if (!observers) {
observers = [[NSMutableSet alloc] init];
[subscriptions setObject:observers forKey:eventName];
}
[observers addObject:observer];
}
}
}
- (void)subscribeEvent:(NSString *)eventName handler:(BTEventHandler)handler{
if(!isEmptyString(eventName))
[self subscribeEvents:@[eventName] handler:handler];
}
- (void)subscribeEvents:(NSArray<NSString *> *)eventNameArray handler:(BTEventHandler)handler{
if(nil == eventNameArray){
return;
}
for(NSString *eventName in eventNameArray){
if(isEmptyString(eventName))
break;
@autoreleasepool{
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:eventName object:nil queue:_pubSubQueue usingBlock:^(NSNotification *note) {
id data = [note.userInfo objectForKey:kBTPubSubDataKey] ? [note.userInfo objectForKey:kBTPubSubDataKey] : note.userInfo;
BTEvent *event = [[BTEvent alloc] initWithName:eventName obj:note.object data:data];
if(handler){
handler(event);
}
}];
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions) {
subscriptions = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, &kBTPubSubSubscriptionsKey, subscriptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSMutableSet *observers = [subscriptions objectForKey:eventName];
if (!observers) {
observers = [[NSMutableSet alloc] init];
[subscriptions setObject:observers forKey:eventName];
}
[observers addObject:observer];
}
}
}
- (void)subscribeOnceEvent:(NSString *)eventName handler:(BTEventHandler)handler{
if(!isEmptyString(eventName))
[self subscribeOnceEvents:@[eventName] handler:handler];
}
- (void)subscribeOnceEvents:(NSArray<NSString *> *)eventNameArray handler:(BTEventHandler)handler{
if(nil == eventNameArray){
return;
}
for(NSString *eventName in eventNameArray){
if(isEmptyString(eventName))
break;
@autoreleasepool{
@weakify(self);
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:eventName object:nil queue:_pubSubQueue usingBlock:^(NSNotification *note) {
@strongify(self);
id data = [note.userInfo objectForKey:kBTPubSubDataKey] ? [note.userInfo objectForKey:kBTPubSubDataKey] : note.userInfo;
BTEvent *event = [[BTEvent alloc] initWithName:eventName obj:note.object data:data];
if(handler){
handler(event);
[self unsubscribeEvent:event.name];
}
}];
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions) {
subscriptions = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, &kBTPubSubSubscriptionsKey, subscriptions, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSMutableSet *observers = [subscriptions objectForKey:eventName];
if (!observers) {
observers = [[NSMutableSet alloc] init];
[subscriptions setObject:observers forKey:eventName];
}
[observers addObject:observer];
}
}
}
- (void)unsubscribeEvent:(NSString *)eventName{
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions)
return;
NSMutableSet *observers = [subscriptions objectForKey:eventName];
if (observers) {
for (id observer in observers) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
[subscriptions removeObjectForKey:eventName];
}
}
- (void)unsubscribeAllEvents{
NSMutableDictionary *subscriptions = (NSMutableDictionary *)objc_getAssociatedObject(self, &kBTPubSubSubscriptionsKey);
if (!subscriptions)
return;
for (NSString *eventName in subscriptions) {
NSMutableSet *observers = [subscriptions objectForKey:eventName];
for (id observer in observers) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
}
[subscriptions removeAllObjects];
}
- (void)bt_dealloc{
[self unsubscribeAllEvents];
[self bt_dealloc];
}
@end
#pragma clang diagnostic pop