1717#import < React/RCTUtils.h>
1818
1919static NSString *const RCTStorageDirectory = @" RCTAsyncLocalStorage_V1" ;
20- static NSString *const RCTOldStorageDirectory = @" RNCAsyncLocalStorage_V1" ;
2120static NSString *const RCTManifestFileName = @" manifest.json" ;
2221static const NSUInteger RCTInlineValueThreshold = 1024 ;
22+ static const NSUInteger RCTStorageCacheTotalCostLimit = 2 * 1024 * 1024 ; // 2 MB
2323
2424#pragma mark - Static helper functions
2525
@@ -161,17 +161,22 @@ static dispatch_queue_t RCTGetMethodQueue()
161161 static dispatch_once_t onceToken;
162162 dispatch_once (&onceToken, ^{
163163 cache = [NSCache new ];
164- cache.totalCostLimit = 2 * 1024 * 1024 ; // 2MB
164+ cache.totalCostLimit = RCTStorageCacheTotalCostLimit;
165165
166166 // Clear cache in the event of a memory warning
167- [[NSNotificationCenter defaultCenter ] addObserverForName: UIApplicationDidReceiveMemoryWarningNotification object: nil queue: nil usingBlock: ^(__unused NSNotification *note) {
168- [cache removeAllObjects ];
169- }];
167+ [[NSNotificationCenter defaultCenter ]
168+ addObserverForName: UIApplicationDidReceiveMemoryWarningNotification
169+ object: nil
170+ queue: nil
171+ usingBlock: ^(__unused NSNotification *note) {
172+ [cache removeAllObjects ];
173+ }];
170174 });
171175 return cache;
172176}
173177
174178static BOOL RCTHasCreatedStorageDirectory = NO ;
179+
175180static NSDictionary *RCTDeleteStorageDirectory ()
176181{
177182 NSError *error;
@@ -180,74 +185,6 @@ static dispatch_queue_t RCTGetMethodQueue()
180185 return error ? RCTMakeError (@" Failed to delete storage directory." , error, nil ) : nil ;
181186}
182187
183- static NSDate *RCTManifestModificationDate (NSString *manifestFilePath)
184- {
185- NSDictionary *attributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: manifestFilePath error: nil ];
186- return [attributes fileModificationDate ];
187- }
188-
189- /* *
190- * Creates an NSException used during Storage Directory Migration.
191- */
192- static void RCTStorageDirectoryMigrationLogError (NSString *reason, NSError *error)
193- {
194- RCTLogWarn (@" %@ : %@ " , reason, error ? error.description : @" " );
195- }
196-
197- static void RCTStorageDirectoryCleanupOld ()
198- {
199- NSError *error;
200- if (![[NSFileManager defaultManager ] removeItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) error: &error]) {
201- RCTStorageDirectoryMigrationLogError (@" Failed to remove old storage directory during migration" , error);
202- }
203- }
204-
205- static void RCTStorageDirectoryMigrate ()
206- {
207- NSError *error;
208- // Migrate data by copying old storage directory to new storage directory location
209- if (![[NSFileManager defaultManager ] copyItemAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) toPath: RCTGetStorageDirectory () error: &error]) {
210- RCTStorageDirectoryMigrationLogError (@" Failed to copy old storage directory to new storage directory location during migration" , error);
211- } else {
212- // If copying succeeds, remove old storage directory
213- RCTStorageDirectoryCleanupOld ();
214- }
215- }
216-
217- /* *
218- * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
219- * Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
220- */
221- static void RCTStorageDirectoryMigrationCheck ()
222- {
223- static dispatch_once_t onceToken;
224- dispatch_once (&onceToken, ^{
225- NSError *error;
226- BOOL isDir;
227- // If the old directory exists, it means we may need to migrate old data to the new directory
228- if ([[NSFileManager defaultManager ] fileExistsAtPath: RCTCreateStorageDirectoryPath (RCTOldStorageDirectory) isDirectory: &isDir] && isDir) {
229- // Check if the new storage directory location already exists
230- if ([[NSFileManager defaultManager ] fileExistsAtPath: RCTGetStorageDirectory ()]) {
231- // If new storage location exists, check if the new storage has been modified sooner
232- if ([RCTManifestModificationDate (RCTGetManifestFilePath ()) compare: RCTManifestModificationDate (RCTCreateManifestFilePath (RCTOldStorageDirectory))] == 1 ) {
233- // If new location has been modified more recently, simply clean out old data
234- RCTStorageDirectoryCleanupOld ();
235- } else {
236- // If old location has been modified more recently, remove new storage and migrate
237- if (![[NSFileManager defaultManager ] removeItemAtPath: RCTGetStorageDirectory () error: &error]) {
238- RCTStorageDirectoryMigrationLogError (@" Failed to remove new storage directory during migration" , error);
239- } else {
240- RCTStorageDirectoryMigrate ();
241- }
242- }
243- } else {
244- // If new storage location doesn't exist, migrate data
245- RCTStorageDirectoryMigrate ();
246- }
247- }
248- });
249- }
250-
251188#pragma mark - RNCAsyncStorage
252189
253190@implementation RNCAsyncStorage
@@ -264,15 +201,6 @@ + (BOOL)requiresMainQueueSetup
264201 return NO ;
265202}
266203
267- - (instancetype )init
268- {
269- if (!(self = [super init ])) {
270- return nil ;
271- }
272- // RCTStorageDirectoryMigrationCheck();
273- return self;
274- }
275-
276204RCT_EXPORT_MODULE ()
277205
278206- (dispatch_queue_t )methodQueue
0 commit comments