@@ -229,16 +229,21 @@ - (NSString *)stringForStatus:(RNPermissionStatus)status {
229229 }
230230}
231231
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+ }
242247}
243248
244249+ (bool )isFlaggedAsRequested : (NSString * _Nonnull)handlerId {
@@ -286,20 +291,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
286291 resolver:(RCTPromiseResolveBlock)resolve
287292 rejecter:(RCTPromiseRejectBlock)reject) {
288293 id <RNPermissionHandler> handler = [self handlerForPermission: permission];
289-
290- NSString *randomId = [self insertHandler: handler];
294+ NSString *lockId = [self lockHandler: handler];
291295
292296 [handler checkWithResolver: ^(RNPermissionStatus status) {
293297 NSString *strStatus = [self stringForStatus: status];
294298 NSLog (@" [react-native-permissions] %@ permission checked: %@ " , [[handler class ] handlerUniqueId ], strStatus);
295- resolve (strStatus);
296299
297- [self .handlers removeObjectForKey: randomId];
300+ resolve (strStatus);
301+ [self unlockHandler: lockId];
298302 } rejecter: ^(NSError *error) {
299303 NSLog (@" [react-native-permissions] %@ permission failed: %@ " , [[handler class ] handlerUniqueId ], error.localizedDescription );
300- reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
301304
302- [self .handlers removeObjectForKey: randomId];
305+ reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
306+ [self unlockHandler: lockId];
303307 }];
304308}
305309
@@ -308,20 +312,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
308312 resolver:(RCTPromiseResolveBlock)resolve
309313 rejecter:(RCTPromiseRejectBlock)reject) {
310314 id <RNPermissionHandler> handler = [self handlerForPermission: permission];
311-
312- NSString *randomId = [self insertHandler: handler];
315+ NSString *lockId = [self lockHandler: handler];
313316
314317 [handler requestWithResolver: ^(RNPermissionStatus status) {
315318 NSString *strStatus = [self stringForStatus: status];
316319 NSLog (@" [react-native-permissions] %@ permission checked: %@ " , [[handler class ] handlerUniqueId ], strStatus);
317- resolve (strStatus);
318320
319- [self .handlers removeObjectForKey: randomId];
321+ resolve (strStatus);
322+ [self unlockHandler: lockId];
320323 } rejecter: ^(NSError *error) {
321324 NSLog (@" [react-native-permissions] %@ permission failed: %@ " , [[handler class ] handlerUniqueId ], error.localizedDescription );
322- reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
323325
324- [self .handlers removeObjectForKey: randomId];
326+ reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
327+ [self unlockHandler: lockId];
325328 }];
326329}
327330
@@ -330,14 +333,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
330333 rejecter:(RCTPromiseRejectBlock)reject) {
331334#if __has_include("RNPermissionHandlerNotifications.h")
332335 RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new ];
336+ NSString *lockId = [self lockHandler: (id <RNPermissionHandler>)handler];
333337
334338 [handler checkWithResolver: ^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
335339 NSString *strStatus = [self stringForStatus: status];
336340 NSLog (@" [react-native-permissions] %@ permission checked: %@ " , [[handler class ] handlerUniqueId ], strStatus);
341+
337342 resolve (@{ @" status" : strStatus, @" settings" : settings });
343+ [self unlockHandler: lockId];
338344 } rejecter: ^(NSError * _Nonnull error) {
339345 NSLog (@" [react-native-permissions] %@ permission failed: %@ " , [[handler class ] handlerUniqueId ], error.localizedDescription );
346+
340347 reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
348+ [self unlockHandler: lockId];
341349 }];
342350#else
343351 reject (@" notifications_pod_missing" , @" Notifications permission pod is missing" , nil );
@@ -350,14 +358,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
350358 rejecter:(RCTPromiseRejectBlock)reject) {
351359#if __has_include("RNPermissionHandlerNotifications.h")
352360 RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new ];
361+ NSString *lockId = [self lockHandler: (id <RNPermissionHandler>)handler];
353362
354363 [handler requestWithResolver: ^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
355364 NSString *strStatus = [self stringForStatus: status];
356365 NSLog (@" [react-native-permissions] %@ permission checked: %@ " , [[handler class ] handlerUniqueId ], strStatus);
366+
357367 resolve (@{ @" status" : strStatus, @" settings" : settings });
368+ [self unlockHandler: lockId];
358369 } rejecter: ^(NSError * _Nonnull error) {
359370 NSLog (@" [react-native-permissions] %@ permission failed: %@ " , [[handler class ] handlerUniqueId ], error.localizedDescription );
371+
360372 reject ([NSString stringWithFormat: @" %ld " , (long )error.code], error.localizedDescription , error);
373+ [self unlockHandler: lockId];
361374 } options: options];
362375#else
363376 reject (@" notifications_pod_missing" , @" Notifications permission pod is missing" , nil );
0 commit comments