Browse Source

Add locking / unlocking on all handlers

Mathieu Acthernoene 4 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,16 +229,21 @@ RCT_EXPORT_MODULE();
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 249
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
@@ -286,20 +291,19 @@ RCT_REMAP_METHOD(check,
286 291
                  resolver:(RCTPromiseResolveBlock)resolve
287 292
                  rejecter:(RCTPromiseRejectBlock)reject) {
288 293
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289
-
290
-  NSString *randomId = [self insertHandler: handler];
294
+  NSString *lockId = [self lockHandler:handler];
291 295
 
292 296
   [handler checkWithResolver:^(RNPermissionStatus status) {
293 297
     NSString *strStatus = [self stringForStatus:status];
294 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 302
   } rejecter:^(NSError *error) {
299 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,20 +312,19 @@ RCT_REMAP_METHOD(request,
308 312
                  resolver:(RCTPromiseResolveBlock)resolve
309 313
                  rejecter:(RCTPromiseRejectBlock)reject) {
310 314
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
311
-
312
-  NSString *randomId = [self insertHandler: handler];
315
+  NSString *lockId = [self lockHandler:handler];
313 316
 
314 317
   [handler requestWithResolver:^(RNPermissionStatus status) {
315 318
     NSString *strStatus = [self stringForStatus:status];
316 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 323
   } rejecter:^(NSError *error) {
321 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,14 +333,19 @@ RCT_REMAP_METHOD(checkNotifications,
330 333
                  rejecter:(RCTPromiseRejectBlock)reject) {
331 334
 #if __has_include("RNPermissionHandlerNotifications.h")
332 335
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
336
+  NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
333 337
 
334 338
   [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
335 339
     NSString *strStatus = [self stringForStatus:status];
336 340
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
341
+
337 342
     resolve(@{ @"status": strStatus, @"settings": settings });
343
+    [self unlockHandler:lockId];
338 344
   } rejecter:^(NSError * _Nonnull error) {
339 345
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
346
+
340 347
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
348
+    [self unlockHandler:lockId];
341 349
   }];
342 350
 #else
343 351
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
@@ -350,14 +358,19 @@ RCT_REMAP_METHOD(requestNotifications,
350 358
                  rejecter:(RCTPromiseRejectBlock)reject) {
351 359
 #if __has_include("RNPermissionHandlerNotifications.h")
352 360
   RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
361
+  NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
353 362
 
354 363
   [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
355 364
     NSString *strStatus = [self stringForStatus:status];
356 365
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
366
+
357 367
     resolve(@{ @"status": strStatus, @"settings": settings });
368
+    [self unlockHandler:lockId];
358 369
   } rejecter:^(NSError * _Nonnull error) {
359 370
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
371
+
360 372
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
373
+    [self unlockHandler:lockId];
361 374
   } options:options];
362 375
 #else
363 376
   reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);