From 783e72a8d079f8479db72d258cab0049fd3b1ef4 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Mon, 29 Apr 2019 16:24:04 -0400 Subject: [PATCH] Remove the RN bridge from MattermostManaged (#2741) * Remove the RN bridge from MattermostManaged * Fix NativeEventEmitter * Optimize for zero listeners --- app/fetch_preconfig.js | 2 +- .../mattermost-managed.ios.js | 5 ++- ios/Mattermost/MattermostManaged.h | 4 +- ios/Mattermost/MattermostManaged.m | 45 +++++++++++++------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app/fetch_preconfig.js b/app/fetch_preconfig.js index 33f78d877..09369443e 100644 --- a/app/fetch_preconfig.js +++ b/app/fetch_preconfig.js @@ -21,7 +21,7 @@ const HEADER_TOKEN = 'Token'; let managedConfig; -mattermostManaged.addEventListener('fetch_managed_config', (config) => { +mattermostManaged.addEventListener('managedConfigDidChange', (config) => { managedConfig = config; }); diff --git a/app/mattermost_managed/mattermost-managed.ios.js b/app/mattermost_managed/mattermost-managed.ios.js index 1e0bb973b..eef8e1129 100644 --- a/app/mattermost_managed/mattermost-managed.ios.js +++ b/app/mattermost_managed/mattermost-managed.ios.js @@ -1,17 +1,18 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import {NativeModules, DeviceEventEmitter} from 'react-native'; +import {NativeModules, NativeEventEmitter} from 'react-native'; import LocalAuth from 'react-native-local-auth'; import JailMonkey from 'jail-monkey'; const {BlurAppScreen, MattermostManaged} = NativeModules; +const mattermostManagedEmitter = new NativeEventEmitter(MattermostManaged); const listeners = []; let localConfig; export default { addEventListener: (name, callback) => { - const listener = DeviceEventEmitter.addListener(name, (config) => { + const listener = mattermostManagedEmitter.addListener(name, (config) => { localConfig = config; if (callback && typeof callback === 'function') { callback(config); diff --git a/ios/Mattermost/MattermostManaged.h b/ios/Mattermost/MattermostManaged.h index 3300ffb4a..4a580a2dc 100644 --- a/ios/Mattermost/MattermostManaged.h +++ b/ios/Mattermost/MattermostManaged.h @@ -7,9 +7,9 @@ // #import +#import - -@interface MattermostManaged : NSObject +@interface MattermostManaged : RCTEventEmitter - (NSUserDefaults *)bucketByName:(NSString*)name; + (void)sendConfigChangedEvent; diff --git a/ios/Mattermost/MattermostManaged.m b/ios/Mattermost/MattermostManaged.m index 6a5160934..985b617ad 100644 --- a/ios/Mattermost/MattermostManaged.m +++ b/ios/Mattermost/MattermostManaged.m @@ -6,12 +6,12 @@ // See License.txt for license information. // -#import -#import #import "RCTUITextView.h" #import "MattermostManaged.h" -@implementation MattermostManaged +@implementation MattermostManaged { + bool hasListeners; +} RCT_EXPORT_MODULE(); @@ -20,7 +20,13 @@ RCT_EXPORT_MODULE(); return YES; } -@synthesize bridge = _bridge; +- (void)startObserving { + hasListeners = YES; +} + +- (void)stopObserving { + hasListeners = NO; +} - (void)dealloc { @@ -28,15 +34,22 @@ RCT_EXPORT_MODULE(); [[NSNotificationCenter defaultCenter] removeObserver:NSUserDefaultsDidChangeNotification]; } -- (void)setBridge:(RCTBridge *)bridge +- (NSArray *)supportedEvents { - _bridge = bridge; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedConfigDidChange:) name:@"managedConfigDidChange" object:nil]; - [[NSNotificationCenter defaultCenter] addObserverForName:NSUserDefaultsDidChangeNotification - object:nil - queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { - [self remoteConfigChanged]; - }]; + return @[@"managedConfigDidChange"]; +} + +- (instancetype)init { + self = [super init]; + if (self) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(managedConfigDidChange:) name:@"managedConfigDidChange" object:nil]; + [[NSNotificationCenter defaultCenter] addObserverForName:NSUserDefaultsDidChangeNotification + object:nil + queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { + [self remoteConfigChanged]; + }]; + } + return self; } + (void)sendConfigChangedEvent { @@ -55,12 +68,16 @@ static NSString * const feedbackKey = @"com.apple.feedback.managed"; - (void)managedConfigDidChange:(NSNotification *)notification { NSDictionary *response = [[NSUserDefaults standardUserDefaults] dictionaryForKey:configurationKey]; - [_bridge.eventDispatcher sendDeviceEventWithName:@"managedConfigDidChange" body:response]; + if (hasListeners) { + [self sendEventWithName:@"managedConfigDidChange" body:response]; + } } - (void) remoteConfigChanged { NSDictionary *response = [[NSUserDefaults standardUserDefaults] dictionaryForKey:configurationKey]; - [_bridge.eventDispatcher sendDeviceEventWithName:@"managedConfigDidChange" body:response]; + if (hasListeners) { + [self sendEventWithName:@"managedConfigDidChange" body:response]; + } } RCT_EXPORT_METHOD(getConfig:(RCTPromiseResolveBlock)resolve