Remove the RN bridge from MattermostManaged (#2741)
* Remove the RN bridge from MattermostManaged * Fix NativeEventEmitter * Optimize for zero listeners
This commit is contained in:
parent
0764523b92
commit
783e72a8d0
4 changed files with 37 additions and 19 deletions
|
|
@ -21,7 +21,7 @@ const HEADER_TOKEN = 'Token';
|
|||
|
||||
let managedConfig;
|
||||
|
||||
mattermostManaged.addEventListener('fetch_managed_config', (config) => {
|
||||
mattermostManaged.addEventListener('managedConfigDidChange', (config) => {
|
||||
managedConfig = config;
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@
|
|||
//
|
||||
|
||||
#import <React/RCTBridgeModule.h>
|
||||
#import <React/RCTEventEmitter.h>
|
||||
|
||||
|
||||
@interface MattermostManaged : NSObject <RCTBridgeModule>
|
||||
@interface MattermostManaged : RCTEventEmitter <RCTBridgeModule>
|
||||
- (NSUserDefaults *)bucketByName:(NSString*)name;
|
||||
+ (void)sendConfigChangedEvent;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
// See License.txt for license information.
|
||||
//
|
||||
|
||||
#import <React/RCTBridge.h>
|
||||
#import <React/RCTEventDispatcher.h>
|
||||
#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<NSString *> *)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
|
||||
|
|
|
|||
Loading…
Reference in a new issue