Skip to content

Commit 6f946fd

Browse files
committed
feat(async-storage): legacy ios codegen
1 parent b5c1f9e commit 6f946fd

File tree

3 files changed

+974
-7
lines changed

3 files changed

+974
-7
lines changed

packages/async-storage/apple/AsyncStorage.mm

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "AsyncStorage.h"
22
#import "AsyncStorage-Swift.h"
3+
#import "RNCAsyncStorage.h" // legacy storage
34

45
@implementation AsyncStorage
56
RCT_EXPORT_MODULE(RNAsyncStorage)
@@ -41,24 +42,106 @@ - (void)getKeys:(nonnull NSString *)dbName resolve:(nonnull RCTPromiseResolveBlo
4142

4243

4344
- (void)legacy_multiGet:(nonnull NSArray *)keys resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
44-
resolve(nil);
45-
45+
RNCAsyncStorage *legacy = [RNCAsyncStorage sharedInstance];
46+
47+
48+
dispatch_async([legacy methodQueue], ^{
49+
NSError *error = nil;
50+
NSDictionary<NSString *, NSString *> *result = [legacy multiGet:keys error:&error];
51+
if (error) {
52+
dispatch_async(dispatch_get_main_queue(), ^{
53+
reject(@"AsyncStorageError", @"Failed to get values for keys", error);
54+
});
55+
return;
56+
}
57+
58+
NSMutableArray *formatted = [NSMutableArray arrayWithCapacity:keys.count];
59+
for (NSString *key in keys) {
60+
id value = result[key];
61+
if (value == [NSNull null] || value == nil) {
62+
value = (id)kCFNull;
63+
}
64+
[formatted addObject:@[key, value]];
65+
}
66+
67+
dispatch_async(dispatch_get_main_queue(), ^{
68+
resolve(formatted);
69+
});
70+
71+
});
4672
}
4773

4874
- (void)legacy_multiSet:(nonnull NSArray *)kvPairs resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
49-
resolve(nil);
75+
RNCAsyncStorage *legacy = [RNCAsyncStorage sharedInstance];
76+
77+
dispatch_async([legacy methodQueue], ^{
78+
NSError *error = nil;
79+
BOOL success = [legacy multiSet:kvPairs error:&error];
80+
81+
if (!success) {
82+
dispatch_async(dispatch_get_main_queue(), ^{
83+
reject(@"AsyncStorageError", @"Failed to set key-value pairs", error);
84+
});
85+
return;
86+
}
87+
88+
dispatch_async(dispatch_get_main_queue(), ^{
89+
resolve(nil);
90+
});
91+
});
5092
}
5193

5294
- (void)legacy_multiRemove:(nonnull NSArray *)keys resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
53-
resolve(nil);
95+
RNCAsyncStorage *legacy = [RNCAsyncStorage sharedInstance];
96+
97+
dispatch_async([legacy methodQueue], ^{
98+
NSError *error = nil;
99+
BOOL success = [legacy multiRemove:keys error:&error];
100+
101+
102+
dispatch_async(dispatch_get_main_queue(), ^{
103+
if (!success) {
104+
reject(@"AsyncStorageError", @"Failed to remove keys", error);
105+
} else {
106+
resolve(nil);
107+
}
108+
});
109+
});
54110
}
55111

56-
- (void)legacy_getAllKeys:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
57-
resolve(nil);
112+
- (void)legacy_getAllKeys:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
113+
RNCAsyncStorage *legacy = [RNCAsyncStorage sharedInstance];
114+
115+
dispatch_async([legacy methodQueue], ^{
116+
NSError *error = nil;
117+
NSArray<NSString *> *keys = [legacy getAllKeys:&error];
118+
119+
dispatch_async(dispatch_get_main_queue(), ^{
120+
if (!keys) {
121+
reject(@"AsyncStorageError", @"Failed to get all keys", error);
122+
} else {
123+
resolve(keys);
124+
}
125+
});
126+
});
58127
}
59128

60129
- (void)legacy_clear:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
61-
resolve(nil);
130+
131+
RNCAsyncStorage *legacy = [RNCAsyncStorage sharedInstance];
132+
133+
dispatch_async([legacy methodQueue], ^{
134+
NSError *error = nil;
135+
BOOL success = [legacy clear:&error];
136+
137+
dispatch_async(dispatch_get_main_queue(), ^{
138+
if (!success) {
139+
reject(@"AsyncStorageError", @"Failed to clear storage", error);
140+
} else {
141+
resolve(nil);
142+
}
143+
});
144+
});
62145
}
63146

64147
- (void)legacy_multiMerge:(nonnull NSArray *)kvPairs resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
#import <React/RCTBridgeModule.h>
11+
#import <React/RCTInvalidating.h>
12+
13+
/**
14+
* A simple, asynchronous, persistent, key-value storage system designed as a
15+
* backend to the AsyncStorage JS module, which is modeled after LocalStorage.
16+
*
17+
* Current implementation stores small values in serialized dictionary and
18+
* larger values in separate files. Since we use a serial file queue
19+
* `RKFileQueue`, reading/writing from multiple threads should be perceived as
20+
* being atomic, unless someone bypasses the `RNCAsyncStorage` API.
21+
*
22+
* Keys and values must always be strings or an error is returned.
23+
*/
24+
25+
NS_ASSUME_NONNULL_BEGIN
26+
27+
@interface RNCAsyncStorage : NSObject<RCTInvalidating>
28+
29+
@property (nonatomic, assign) BOOL clearOnInvalidate;
30+
31+
@property (nonatomic, readonly, getter=isValid) BOOL valid;
32+
33+
+ (instancetype)sharedInstance;
34+
35+
- (dispatch_queue_t)methodQueue;
36+
37+
// Clear the RNCAsyncStorage data from native code
38+
- (void)clearAllData;
39+
40+
// For clearing data when the bridge may not exist, e.g. when logging out.
41+
+ (void)clearAllData;
42+
43+
- (nullable NSDictionary<NSString *, NSString *> *)multiGet:(NSArray<NSString *> *)keys
44+
error:(NSError **)error;
45+
46+
- (BOOL)multiSet:(NSArray<NSArray<NSString *> *> *)kvPairs
47+
error:(NSError **)error;
48+
49+
- (BOOL)multiRemove:(NSArray<NSString *> *)keys
50+
error:(NSError **)error;
51+
52+
- (nullable NSArray<NSString *> *)getAllKeys:(NSError **)error;
53+
54+
- (BOOL)clear:(NSError **)error;
55+
56+
@end
57+
58+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)