RNEventEmitter.m 1.44 KB
Newer Older
yogevbd's avatar
WIP  
yogevbd committed
1 2 3 4 5 6 7
#import "RNEventEmitter.h"

@implementation RNEventEmitter

RCT_EXPORT_MODULE();

-(NSArray<NSString *> *)supportedEvents {
yogevbd's avatar
WIP  
yogevbd committed
8 9 10
    return @[RNRegistered,
             RNRegistrationFailed,
             RNPushKitRegistered,
yogevbd's avatar
yogevbd committed
11
             RNNotificationReceived,
yogevbd's avatar
yogevbd committed
12 13
             RNNotificationOpened,
             RNPushKitNotificationReceived];
yogevbd's avatar
WIP  
yogevbd committed
14 15
}

yogevbd's avatar
WIP  
yogevbd committed
16 17 18 19 20 21
- (instancetype)init {
    self = [super init];
    for (NSString *event in [self supportedEvents]) {
        [self addListener:event];
    }
    return self;
yogevbd's avatar
WIP  
yogevbd committed
22 23
}

yogevbd's avatar
WIP  
yogevbd committed
24 25 26 27
+ (BOOL)requiresMainQueueSetup {
    return YES;
}

yogevbd's avatar
WIP  
yogevbd committed
28 29
# pragma mark public

yogevbd's avatar
WIP  
yogevbd committed
30
+ (void)sendEvent:(NSString *)event body:(NSDictionary *)body {
yogevbd's avatar
WIP  
yogevbd committed
31 32 33
    [[NSNotificationCenter defaultCenter] postNotificationName:event
                                                        object:self
                                                      userInfo:body];
yogevbd's avatar
WIP  
yogevbd committed
34 35 36 37
}

# pragma mark private

yogevbd's avatar
WIP  
yogevbd committed
38 39 40 41 42 43
- (void)startObserving {
    for (NSString *event in [self supportedEvents]) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleNotification:)
                                                     name:event
                                                   object:nil];
yogevbd's avatar
WIP  
yogevbd committed
44 45 46
    }
}

yogevbd's avatar
WIP  
yogevbd committed
47 48 49 50 51 52 53 54 55
- (void)stopObserving {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleNotification:(NSNotification *)notification {
    [self sendEventWithName:notification.name body:notification.userInfo];
}


yogevbd's avatar
WIP  
yogevbd committed
56
@end