Swiftgram/submodules/Display/Display/NotificationCenterUtils.m
Peter 8f5a4f7dc1 Add 'submodules/Display/' from commit '7bd11013ea936e3d49d937550d599f5816d32560'
git-subtree-dir: submodules/Display
git-subtree-mainline: 9bc996374ffdad37aef175427db72731c9551dcf
git-subtree-split: 7bd11013ea936e3d49d937550d599f5816d32560
2019-06-11 18:44:37 +01:00

71 lines
1.8 KiB
Objective-C

#import "NotificationCenterUtils.h"
#import "RuntimeUtils.h"
#import <UIKit/UIKit.h>
static NSMutableArray *notificationHandlers() {
static NSMutableArray *array = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
array = [[NSMutableArray alloc] init];
});
return array;
}
@interface NSNotificationCenter (_a65afc19)
@end
@implementation NSNotificationCenter (_a65afc19)
- (void)_a65afc19_postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo
{
if ([NSThread isMainThread]) {
for (NotificationHandlerBlock handler in notificationHandlers())
{
if (handler(aName, anObject, aUserInfo, ^{
[self _a65afc19_postNotificationName:aName object:anObject userInfo:aUserInfo];
})) {
return;
}
}
}
[self _a65afc19_postNotificationName:aName object:anObject userInfo:aUserInfo];
}
@end
@interface CATransaction (Swizzle)
+ (void)swizzle_flush;
@end
@implementation CATransaction (Swizzle)
+ (void)swizzle_flush {
//printf("===flush\n");
[self swizzle_flush];
}
@end
@implementation NotificationCenterUtils
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[RuntimeUtils swizzleInstanceMethodOfClass:[NSNotificationCenter class] currentSelector:@selector(postNotificationName:object:userInfo:) newSelector:@selector(_a65afc19_postNotificationName:object:userInfo:)];
//[RuntimeUtils swizzleClassMethodOfClass:[CATransaction class] currentSelector:@selector(flush) newSelector:@selector(swizzle_flush)];
});
}
+ (void)addNotificationHandler:(NotificationHandlerBlock)handler {
[notificationHandlers() addObject:[handler copy]];
}
@end