-
Notifications
You must be signed in to change notification settings - Fork 25
/
CJNotificationCenter.m
59 lines (48 loc) · 1.26 KB
/
CJNotificationCenter.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
//
// CJNotification.m
// RNProjectPlayground
//
// Created by cookiej on 2018/5/19.
// Copyright © 2018年 Facebook. All rights reserved.
//
#import "CJNotificationCenter.h"
#define NATIVE_TO_RN_EVENT_KEY @"NATIVE_TO_RN"
@implementation CJNotificationCenter
{
BOOL hasListeners;
}
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents {
return @[NATIVE_TO_RN_EVENT_KEY];
}
- (void)startObserving {
hasListeners = YES;
}
- (void)stopObserving {
hasListeners = NO;
}
+ (instancetype)center {
static CJNotificationCenter *_center = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_center = [[CJNotificationCenter alloc] init];
});
return _center;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static CJNotificationCenter *_center = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_center = [super allocWithZone:zone];
});
return _center;
}
- (void)sendRNEventWithName:(NSString *)notification body:(NSDictionary *)body {
NSMutableDictionary *newInfo = [NSMutableDictionary dictionary];
[newInfo setObject:notification forKey:@"eventName"];
[newInfo setObject:body forKey:@"body"];
if (hasListeners) {
[self sendEventWithName:NATIVE_TO_RN_EVENT_KEY body:newInfo];
}
}
@end