Skip to content

Commit

Permalink
Refactor FlipperKitDatabasePlugin to accept multiple database drivers.
Browse files Browse the repository at this point in the history
Reviewed By: lblasa

Differential Revision: D48316901

fbshipit-source-id: 0eb7b93f53ce115a0a031a58ecef3db963b35f09
  • Loading branch information
Fúlvio Abrahão de Paula authored and facebook-github-bot committed Aug 14, 2023
1 parent 661ed9e commit 2f5f491
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
@interface DatabasesManager : NSObject

@property(nonatomic, strong) id<FlipperConnection> connection;
@property(nonatomic, strong, readonly)
NSArray<id<DatabaseDriver>>* databaseDrivers;

- (instancetype)initWithDatabaseDrivers:
(NSArray<id<DatabaseDriver>>*)databaseDrivers;
- (instancetype)init;
- (void)setConnection:(id<FlipperConnection>)connection;
- (BOOL)isConnected;
- (void)addDatabaseDriver:(id<DatabaseDriver>)driver;
- (void)removeDatabaseDriver:(id<DatabaseDriver>)driver;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ @interface DatabasesManager ()
databaseDescriptorHolders;
@property(nonatomic, strong)
NSMutableSet<DatabaseDescriptorHolder*>* databaseDescriptorHolderSet;
@property(nonatomic, strong) NSMutableSet<id<DatabaseDriver>>* databaseDrivers;

@end

@implementation DatabasesManager

- (instancetype)initWithDatabaseDrivers:
(NSArray<id<DatabaseDriver>>*)databaseDrivers {
- (instancetype)init {
self = [super init];
if (self) {
_databaseDrivers = [databaseDrivers copy];
_databaseDescriptorHolders = [[NSMutableDictionary alloc] init];
_databaseDescriptorHolderSet = [[NSMutableSet alloc] init];
_databaseDrivers = [NSMutableSet new];
_databaseDescriptorHolders = [NSMutableDictionary new];
_databaseDescriptorHolderSet = [NSMutableSet new];
}
return self;
}
Expand Down Expand Up @@ -224,6 +224,20 @@ - (void)listenForCommands {
}];
}

- (void)addDatabaseDriver:(id<DatabaseDriver>)driver {
if ([self.databaseDrivers containsObject:driver]) {
return;
}
[self.databaseDrivers addObject:driver];
}

- (void)removeDatabaseDriver:(id<DatabaseDriver>)driver {
if (![self.databaseDrivers containsObject:driver]) {
return;
}
[self.databaseDrivers removeObject:driver];
}

+ (void)raiseInvalidRequestError:(id<FlipperResponder>)responder {
NSDictionary* errorResponse =
[ObjectMapper errorWithCode:DatabasesErrorCodesInvalidRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <FlipperKit/FlipperPlugin.h>
#import <Foundation/Foundation.h>
#import "DatabaseDriver.h"

@class DatabasesManager;

Expand All @@ -15,5 +16,7 @@

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)sharedInstance;
- (void)addDatabaseDriver:(id<DatabaseDriver>)driver;
- (void)removeDatabaseDriver:(id<DatabaseDriver>)driver;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ @implementation FlipperKitDatabasesPlugin

- (instancetype)init {
if (self = [super init]) {
NSArray<id<DatabaseDriver>>* databaseDrivers =
@[ [MockDatabaseDriver new] ];
_databasesManager =
[[DatabasesManager alloc] initWithDatabaseDrivers:databaseDrivers];
_databasesManager = [DatabasesManager new];
}
return self;
}
Expand Down Expand Up @@ -59,6 +56,14 @@ - (BOOL)runInBackground {
return NO;
}

- (void)addDatabaseDriver:(id<DatabaseDriver>)driver {
[self.databasesManager addDatabaseDriver:driver];
}

- (void)removeDatabaseDriver:(id<DatabaseDriver>)driver {
[self.databasesManager addDatabaseDriver:driver];
}

@end

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

#import <FlipperKitDatabasesPlugin/DatabaseDriver.h>
#import <Foundation/Foundation.h>
#import "DatabaseDriver.h"

@interface MockDatabaseDriver : NSObject<DatabaseDriver>

Expand Down

0 comments on commit 2f5f491

Please sign in to comment.