Browse Source

Add locking / unlocking on all handlers

Mathieu Acthernoene 5 years ago
parent
commit
da062ff5ec
1 changed files with 35 additions and 22 deletions
  1. 35
    22
      ios/RNPermissions.m

+ 35
- 22
ios/RNPermissions.m View File

229
   }
229
   }
230
 }
230
 }
231
 
231
 
232
-- (NSString *)insertHandler:(id<RNPermissionHandler>)handler {
233
-    if(_handlers == nil){
234
-        _handlers = [NSMutableDictionary new];
235
-    }
236
-    
237
-    NSString *randomId = [[NSUUID UUID] UUIDString];
238
-    
239
-    [_handlers setObject:handler forKey:randomId];
240
-    
241
-    return randomId;
232
+- (NSString *)lockHandler:(id<RNPermissionHandler>)handler {
233
+  if (_handlers == nil) {
234
+    _handlers = [NSMutableDictionary new];
235
+  }
236
+
237
+  NSString *lockId = [[NSUUID UUID] UUIDString];
238
+  [_handlers setObject:handler forKey:lockId];
239
+
240
+  return lockId;
241
+}
242
+
243
+- (void)unlockHandler:(NSString * _Nonnull)lockId {
244
+  if (_handlers != nil) {
245
+    [self.handlers removeObjectForKey:lockId];
246
+  }
242
 }
247
 }
243
 
248
 
244
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
249
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
286
                  resolver:(RCTPromiseResolveBlock)resolve
291
                  resolver:(RCTPromiseResolveBlock)resolve
287
                  rejecter:(RCTPromiseRejectBlock)reject) {
292
                  rejecter:(RCTPromiseRejectBlock)reject) {
288
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
293
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289
-
290
-  NSString *randomId = [self insertHandler: handler];
294
+  NSString *lockId = [self lockHandler:handler];
291
 
295
 
292
   [handler checkWithResolver:^(RNPermissionStatus status) {
296
   [handler checkWithResolver:^(RNPermissionStatus status) {
293
     NSString *strStatus = [self stringForStatus:status];
297
     NSString *strStatus = [self stringForStatus:status];
294
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
298
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
295
-    resolve(strStatus);
296
 
299
 
297
-    [self.handlers removeObjectForKey:randomId];
300
+    resolve(strStatus);
301
+    [self unlockHandler:lockId];
298
   } rejecter:^(NSError *error) {
302
   } rejecter:^(NSError *error) {
299
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
303
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
300
-    reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
301
 
304
 
302
-    [self.handlers removeObjectForKey:randomId];
305
+    reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
306
+    [self unlockHandler:lockId];
303
   }];
307
   }];
304
 }
308
 }
305
 
309
 
308
                  resolver:(RCTPromiseResolveBlock)resolve
312
                  resolver:(RCTPromiseResolveBlock)resolve
309
                  rejecter:(RCTPromiseRejectBlock)reject) {
313
                  rejecter:(RCTPromiseRejectBlock)reject) {
310
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
314
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
311
-
312
-  NSString *randomId = [self insertHandler: handler];
315
+  NSString *lockId = [self lockHandler:handler];
313
 
316
 
314
   [handler requestWithResolver:^(RNPermissionStatus status) {
317
   [handler requestWithResolver:^(RNPermissionStatus status) {
315
     NSString *strStatus = [self stringForStatus:status];
318
     NSString *strStatus = [self stringForStatus:status];
316
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
319
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
317
-    resolve(strStatus);
318
 
320
 
319
-    [self.handlers removeObjectForKey:randomId];
321
+    resolve(strStatus);
322
+    [self unlockHandler:lockId];
320
   } rejecter:^(NSError *error) {
323
   } rejecter:^(NSError *error) {
321
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
324
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
322
-    reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
323
 
325
 
324
-    [self.handlers removeObjectForKey:randomId];
326
+    reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
327
+    [self unlockHandler:lockId];
325
   }];
328
   }];
326
 }
329
 }
327
 
330
 
330
                  rejecter:(RCTPromiseRejectBlock)reject) {
333
                  rejecter:(RCTPromiseRejectBlock)reject) {
331
 #if __has_include("RNPermissionHandlerNotifications.h")
334
 #if __has_include("RNPermissionHandlerNotifications.h")
332
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
335
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
336
+  NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
333
 
337
 
334
   [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
338
   [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
335
     NSString *strStatus = [self stringForStatus:status];
339
     NSString *strStatus = [self stringForStatus:status];
336
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
340
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
341
+
337
     resolve(@{ @"status": strStatus, @"settings": settings });
342
     resolve(@{ @"status": strStatus, @"settings": settings });
343
+    [self unlockHandler:lockId];
338
   } rejecter:^(NSError * _Nonnull error) {
344
   } rejecter:^(NSError * _Nonnull error) {
339
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
345
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
346
+
340
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
347
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
348
+    [self unlockHandler:lockId];
341
   }];
349
   }];
342
 #else
350
 #else
343
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
351
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
350
                  rejecter:(RCTPromiseRejectBlock)reject) {
358
                  rejecter:(RCTPromiseRejectBlock)reject) {
351
 #if __has_include("RNPermissionHandlerNotifications.h")
359
 #if __has_include("RNPermissionHandlerNotifications.h")
352
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
360
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
361
+  NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
353
 
362
 
354
   [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
363
   [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
355
     NSString *strStatus = [self stringForStatus:status];
364
     NSString *strStatus = [self stringForStatus:status];
356
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
365
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
366
+
357
     resolve(@{ @"status": strStatus, @"settings": settings });
367
     resolve(@{ @"status": strStatus, @"settings": settings });
368
+    [self unlockHandler:lockId];
358
   } rejecter:^(NSError * _Nonnull error) {
369
   } rejecter:^(NSError * _Nonnull error) {
359
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
370
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
371
+
360
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
372
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
373
+    [self unlockHandler:lockId];
361
   } options:options];
374
   } options:options];
362
 #else
375
 #else
363
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
376
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);