RNEventEmitter.m 1.43 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 11 12 13 14
    return @[RNRegistered,
             RNRegistrationFailed,
             RNPushKitRegistered,
             RNNotificationReceivedForeground,
             RNNotificationReceivedBackground,
             RNNotificationOpened,
             RNActionTriggered];
yogevbd's avatar
WIP  
yogevbd committed
15 16
}

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

yogevbd's avatar
WIP  
yogevbd committed
25 26
# pragma mark public

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

# pragma mark private

yogevbd's avatar
WIP  
yogevbd committed
35 36 37 38 39 40
- (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
41 42 43
    }
}

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

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


yogevbd's avatar
WIP  
yogevbd committed
53
@end