1// Copyright 2015-present 650 Industries. All rights reserved.
2
3#import <ExpoFileSystem/EXSessionHandler.h>
4
5#import <ExpoModulesCore/EXDefines.h>
6
7@interface EXSessionHandler ()
8
9@property (nonatomic, strong) NSMutableDictionary<NSString *, void (^)(void)> *completionHandlers;
10
11@end
12
13@implementation EXSessionHandler
14
15EX_REGISTER_SINGLETON_MODULE(SessionHandler);
16
17- (instancetype)init
18{
19  if (self = [super init]) {
20    _completionHandlers = [NSMutableDictionary dictionary];
21  }
22
23  return self;
24}
25
26- (void)invokeCompletionHandlerForSessionIdentifier:(NSString *)identifier
27{
28  if (!identifier) {
29    return;
30  }
31
32  void (^completionHandler)(void) = _completionHandlers[identifier];
33  if (completionHandler) {
34    // We need to run completionHandler explicite on the main thread because is's part of UIKit
35    dispatch_async(dispatch_get_main_queue(), ^{
36      completionHandler();
37    });
38    [_completionHandlers removeObjectForKey:identifier];
39  }
40}
41
42#pragma mark - AppDelegate
43
44- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
45{
46  _completionHandlers[identifier] = completionHandler;
47}
48
49@end
50