1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#if __has_include(<EXErrorRecovery/EXErrorRecoveryModule.h>)
4#import "EXScopedErrorRecoveryModule.h"
5
6@interface EXScopedErrorRecoveryModule ()
7
8@property (nonatomic, strong) NSString *experienceId;
9
10@end
11
12@implementation EXScopedErrorRecoveryModule
13
14- (instancetype)initWithExperienceId:(NSString *)experienceId
15{
16  if (self = [super init]) {
17    _experienceId = experienceId;
18  }
19  return self;
20}
21
22- (BOOL)setRecoveryProps:(NSString *)props
23{
24  NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
25  NSDictionary *errorRecoveryStore = [preferences dictionaryForKey:[self userDefaultsKey]] ?: @{};
26  NSMutableDictionary *newStore = [errorRecoveryStore mutableCopy];
27  newStore[_experienceId] = props;
28  [preferences setObject:newStore forKey:[self userDefaultsKey]];
29  return [preferences synchronize];
30}
31
32- (NSString *)consumeRecoveryProps
33{
34  NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
35  NSDictionary *errorRecoveryStore = [preferences dictionaryForKey:[self userDefaultsKey]];
36  if (errorRecoveryStore) {
37    NSString *props = errorRecoveryStore[_experienceId];
38    if (props) {
39      NSMutableDictionary *storeWithRemovedProps = [errorRecoveryStore mutableCopy];
40      [storeWithRemovedProps removeObjectForKey:_experienceId];
41      [preferences setObject:storeWithRemovedProps forKey:[self userDefaultsKey]];
42      [preferences synchronize];
43      return props;
44    }
45  }
46  return nil;
47}
48
49@end
50#endif
51