2017-03-09 15:26:28 +00:00
|
|
|
#import "RNFirebaseDatabase.h"
|
|
|
|
#import "RNFirebaseEvents.h"
|
2017-05-31 08:54:02 +00:00
|
|
|
#import "Firebase.h"
|
2017-05-25 15:00:12 +00:00
|
|
|
|
|
|
|
#if __has_include(<FirebaseDatabase/FIRDatabase.h>)
|
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
@interface RNFirebaseDBReference : NSObject
|
|
|
|
@property RCTEventEmitter *emitter;
|
|
|
|
@property FIRDatabaseQuery *query;
|
2017-04-26 16:34:16 +00:00
|
|
|
@property NSNumber *refId;
|
2017-03-09 15:26:28 +00:00
|
|
|
@property NSString *path;
|
|
|
|
@property NSMutableDictionary *listeners;
|
2017-05-25 15:00:12 +00:00
|
|
|
|
|
|
|
+ (NSDictionary *)snapshotToDict:(FIRDataSnapshot *)snapshot;
|
2017-03-09 15:26:28 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RNFirebaseDBReference
|
|
|
|
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (id)initWithPathAndModifiers:(RCTEventEmitter *)emitter database:(FIRDatabase *)database refId:(NSNumber *)refId path:(NSString *)path modifiers:(NSArray *)modifiers {
|
2017-03-25 00:59:27 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_emitter = emitter;
|
2017-04-26 16:34:16 +00:00
|
|
|
_refId = refId;
|
2017-03-25 00:59:27 +00:00
|
|
|
_path = path;
|
|
|
|
_query = [self buildQueryAtPathWithModifiers:database path:path modifiers:modifiers];
|
|
|
|
_listeners = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (void)addEventHandler:(NSNumber *)listenerId eventName:(NSString *)eventName {
|
|
|
|
if (!_listeners[listenerId]) {
|
2017-06-15 13:55:52 +00:00
|
|
|
id andPreviousSiblingKeyWithBlock = ^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
|
2017-03-25 00:59:27 +00:00
|
|
|
NSDictionary *props = [RNFirebaseDBReference snapshotToDict:snapshot];
|
2017-07-04 13:13:49 +00:00
|
|
|
[self sendJSEvent:DATABASE_DATA_EVENT title:eventName props:@{@"eventName": eventName, @"refId": _refId, @"listenerId": listenerId, @"path": _path, @"snapshot": props, @"previousChildName": previousChildName != nil ? previousChildName : [NSNull null]}];
|
2017-03-09 15:26:28 +00:00
|
|
|
};
|
2017-05-25 15:00:12 +00:00
|
|
|
id errorBlock = ^(NSError *_Nonnull error) {
|
2017-03-09 15:26:28 +00:00
|
|
|
NSLog(@"Error onDBEvent: %@", [error debugDescription]);
|
2017-04-26 16:34:16 +00:00
|
|
|
[self removeEventHandler:listenerId eventName:eventName];
|
2017-05-25 15:00:12 +00:00
|
|
|
[self getAndSendDatabaseError:error listenerId:listenerId];
|
2017-03-09 15:26:28 +00:00
|
|
|
};
|
|
|
|
int eventType = [self eventTypeFromName:eventName];
|
2017-06-15 13:55:52 +00:00
|
|
|
FIRDatabaseHandle handle = [_query observeEventType:eventType andPreviousSiblingKeyWithBlock:andPreviousSiblingKeyWithBlock withCancelBlock:errorBlock];
|
2017-05-25 15:00:12 +00:00
|
|
|
_listeners[listenerId] = @(handle);
|
2017-03-09 15:26:28 +00:00
|
|
|
} else {
|
2017-04-26 16:34:16 +00:00
|
|
|
NSLog(@"Warning Trying to add duplicate listener for refId: %@ listenerId: %@", _refId, listenerId);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 13:13:49 +00:00
|
|
|
- (void)addSingleEventHandler:(NSString *)eventName callback:(RCTResponseSenderBlock)callback {
|
2017-07-04 15:17:42 +00:00
|
|
|
FIRDataEventType firDataEventType = (FIRDataEventType)[self eventTypeFromName:eventName];
|
2017-07-04 13:13:49 +00:00
|
|
|
|
|
|
|
[_query observeSingleEventOfType:firDataEventType andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
|
|
|
|
NSDictionary *props = [RNFirebaseDBReference snapshotToDict:snapshot];
|
|
|
|
callback(@[[NSNull null], @{@"eventName": eventName, @"path": _path, @"refId": _refId, @"snapshot": props, @"previousChildName": previousChildName != nil ? previousChildName : [NSNull null]}]);
|
|
|
|
} withCancelBlock:^(NSError *_Nonnull error) {
|
|
|
|
NSLog(@"Error onDBEventOnce: %@", [error debugDescription]);
|
|
|
|
callback(@[@{@"eventName": DATABASE_ERROR_EVENT, @"path": _path, @"refId": _refId, @"code": @([error code]), @"details": [error debugDescription], @"message": [error localizedDescription], @"description": [error description]}]);
|
|
|
|
}];
|
2017-05-25 15:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeEventHandler:(NSNumber *)listenerId eventName:(NSString *)eventName {
|
|
|
|
FIRDatabaseHandle handle = (FIRDatabaseHandle) [_listeners[listenerId] integerValue];
|
2017-04-26 16:34:16 +00:00
|
|
|
if (handle) {
|
|
|
|
[_listeners removeObjectForKey:listenerId];
|
|
|
|
[_query removeObserverWithHandle:handle];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
+ (NSDictionary *)snapshotToDict:(FIRDataSnapshot *)snapshot {
|
2017-03-09 15:26:28 +00:00
|
|
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
|
|
|
[dict setValue:snapshot.key forKey:@"key"];
|
|
|
|
NSDictionary *val = snapshot.value;
|
2017-05-25 15:00:12 +00:00
|
|
|
dict[@"value"] = val;
|
2017-03-09 15:26:28 +00:00
|
|
|
// Snapshot ordering
|
|
|
|
NSMutableArray *childKeys = [NSMutableArray array];
|
|
|
|
if (snapshot.childrenCount > 0) {
|
|
|
|
// Since JS does not respect object ordering of keys
|
|
|
|
// we keep a list of the keys and their ordering
|
|
|
|
// in the snapshot event
|
|
|
|
NSEnumerator *children = [snapshot children];
|
|
|
|
FIRDataSnapshot *child;
|
2017-05-25 15:00:12 +00:00
|
|
|
while (child = [children nextObject]) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[childKeys addObject:child.key];
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 15:00:12 +00:00
|
|
|
dict[@"childKeys"] = childKeys;
|
2017-03-09 15:26:28 +00:00
|
|
|
[dict setValue:@(snapshot.hasChildren) forKey:@"hasChildren"];
|
|
|
|
[dict setValue:@(snapshot.exists) forKey:@"exists"];
|
|
|
|
[dict setValue:@(snapshot.childrenCount) forKey:@"childrenCount"];
|
|
|
|
[dict setValue:snapshot.priority forKey:@"priority"];
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (NSDictionary *)getAndSendDatabaseError:(NSError *)error listenerId:(NSNumber *)listenerId {
|
|
|
|
NSDictionary *event = @{@"eventName": DATABASE_ERROR_EVENT, @"path": _path, @"refId": _refId, @"listenerId": listenerId, @"code": @([error code]), @"details": [error debugDescription], @"message": [error localizedDescription], @"description": [error description]};
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
@try {
|
|
|
|
[_emitter sendEventWithName:DATABASE_ERROR_EVENT body:event];
|
2017-05-25 15:00:12 +00:00
|
|
|
} @catch (NSException *err) {
|
2017-03-09 15:26:28 +00:00
|
|
|
NSLog(@"An error occurred in getAndSendDatabaseError: %@", [err debugDescription]);
|
|
|
|
NSLog(@"Tried to send: %@ with %@", DATABASE_ERROR_EVENT, event);
|
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (void)sendJSEvent:(NSString *)type title:(NSString *)title props:(NSDictionary *)props {
|
2017-03-09 15:26:28 +00:00
|
|
|
@try {
|
2017-05-25 15:00:12 +00:00
|
|
|
[_emitter sendEventWithName:type body:@{@"eventName": title, @"body": props}];
|
|
|
|
} @catch (NSException *err) {
|
2017-03-09 15:26:28 +00:00
|
|
|
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);
|
|
|
|
NSLog(@"Tried to send: %@ with %@", title, props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (FIRDatabaseQuery *)buildQueryAtPathWithModifiers:(FIRDatabase *)database path:(NSString *)path modifiers:(NSArray *)modifiers {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseQuery *query = [[database reference] child:path];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
|
|
|
for (NSDictionary *modifier in modifiers) {
|
|
|
|
NSString *type = [modifier valueForKey:@"type"];
|
|
|
|
NSString *name = [modifier valueForKey:@"name"];
|
|
|
|
if ([type isEqualToString:@"orderBy"]) {
|
|
|
|
if ([name isEqualToString:@"orderByKey"]) {
|
|
|
|
query = [query queryOrderedByKey];
|
|
|
|
} else if ([name isEqualToString:@"orderByPriority"]) {
|
|
|
|
query = [query queryOrderedByPriority];
|
|
|
|
} else if ([name isEqualToString:@"orderByValue"]) {
|
|
|
|
query = [query queryOrderedByValue];
|
|
|
|
} else if ([name isEqualToString:@"orderByChild"]) {
|
|
|
|
NSString *key = [modifier valueForKey:@"key"];
|
|
|
|
query = [query queryOrderedByChild:key];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
} else if ([type isEqualToString:@"limit"]) {
|
|
|
|
int limit = [[modifier valueForKey:@"limit"] integerValue];
|
|
|
|
if ([name isEqualToString:@"limitToLast"]) {
|
|
|
|
query = [query queryLimitedToLast:limit];
|
|
|
|
} else if ([name isEqualToString:@"limitToFirst"]) {
|
|
|
|
query = [query queryLimitedToFirst:limit];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
} else if ([type isEqualToString:@"filter"]) {
|
2017-05-25 15:00:12 +00:00
|
|
|
NSString *valueType = [modifier valueForKey:@"valueType"];
|
|
|
|
NSString *key = [modifier valueForKey:@"key"];
|
2017-04-26 16:34:16 +00:00
|
|
|
id value = [self getIdValue:[modifier valueForKey:@"value"] type:valueType];
|
|
|
|
if ([name isEqualToString:@"equalTo"]) {
|
|
|
|
if (key != nil) {
|
|
|
|
query = [query queryEqualToValue:value childKey:key];
|
|
|
|
} else {
|
|
|
|
query = [query queryEqualToValue:value];
|
|
|
|
}
|
|
|
|
} else if ([name isEqualToString:@"endAt"]) {
|
|
|
|
if (key != nil) {
|
|
|
|
query = [query queryEndingAtValue:value childKey:key];
|
|
|
|
} else {
|
|
|
|
query = [query queryEndingAtValue:value];
|
|
|
|
}
|
|
|
|
} else if ([name isEqualToString:@"startAt"]) {
|
|
|
|
if (key != nil) {
|
|
|
|
query = [query queryStartingAtValue:value childKey:key];
|
|
|
|
} else {
|
|
|
|
query = [query queryStartingAtValue:value];
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (id)getIdValue:(NSString *)value type:(NSString *)type {
|
2017-03-09 15:26:28 +00:00
|
|
|
if ([type isEqualToString:@"number"]) {
|
2017-05-25 15:00:12 +00:00
|
|
|
return @(value.doubleValue);
|
2017-03-09 15:26:28 +00:00
|
|
|
} else if ([type isEqualToString:@"boolean"]) {
|
2017-05-25 15:00:12 +00:00
|
|
|
return @(value.boolValue);
|
2017-03-09 15:26:28 +00:00
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (BOOL)hasListeners {
|
2017-03-09 15:26:28 +00:00
|
|
|
return [[_listeners allKeys] count] > 0;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (int)eventTypeFromName:(NSString *)name {
|
2017-03-09 15:26:28 +00:00
|
|
|
int eventType = FIRDataEventTypeValue;
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
if ([name isEqualToString:DATABASE_VALUE_EVENT]) {
|
|
|
|
eventType = FIRDataEventTypeValue;
|
|
|
|
} else if ([name isEqualToString:DATABASE_CHILD_ADDED_EVENT]) {
|
|
|
|
eventType = FIRDataEventTypeChildAdded;
|
|
|
|
} else if ([name isEqualToString:DATABASE_CHILD_MODIFIED_EVENT]) {
|
|
|
|
eventType = FIRDataEventTypeChildChanged;
|
|
|
|
} else if ([name isEqualToString:DATABASE_CHILD_REMOVED_EVENT]) {
|
|
|
|
eventType = FIRDataEventTypeChildRemoved;
|
|
|
|
} else if ([name isEqualToString:DATABASE_CHILD_MOVED_EVENT]) {
|
|
|
|
eventType = FIRDataEventTypeChildMoved;
|
|
|
|
}
|
|
|
|
return eventType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
@implementation RNFirebaseDatabase
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_MODULE();
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (id)init {
|
2017-03-09 15:26:28 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
|
|
|
_dbReferences = [[NSMutableDictionary alloc] init];
|
2017-03-25 00:59:27 +00:00
|
|
|
_transactions = [[NSMutableDictionary alloc] init];
|
|
|
|
_transactionQueue = dispatch_queue_create("io.invertase.react-native-firebase", DISPATCH_QUEUE_CONCURRENT);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (void)sendTransactionEvent:(NSString *)type body:(id)body {
|
2017-03-25 00:59:27 +00:00
|
|
|
@try {
|
|
|
|
[self sendEventWithName:type body:body];
|
2017-05-25 15:00:12 +00:00
|
|
|
} @catch (NSException *err) {
|
2017-03-25 00:59:27 +00:00
|
|
|
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);
|
|
|
|
NSLog(@"Tried to send: %@ with %@", type, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(startTransaction:
|
|
|
|
(NSString *) path
|
|
|
|
identifier:
|
|
|
|
(NSString *) identifier
|
|
|
|
applyLocally:
|
|
|
|
(BOOL) applyLocally) {
|
2017-03-25 00:59:27 +00:00
|
|
|
dispatch_async(_transactionQueue, ^{
|
|
|
|
NSMutableDictionary *transactionState = [NSMutableDictionary new];
|
|
|
|
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
2017-06-16 08:30:02 +00:00
|
|
|
transactionState[@"semaphore"] = sema;
|
2017-03-25 00:59:27 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref runTransactionBlock:^FIRTransactionResult * _Nonnull(FIRMutableData *
|
|
|
|
_Nonnull currentData) {
|
|
|
|
dispatch_barrier_async(_transactionQueue, ^{
|
|
|
|
[_transactions setValue:transactionState forKey:identifier];
|
|
|
|
[self sendTransactionEvent:DATABASE_TRANSACTION_EVENT body:@{@"id": identifier, @"type": @"update", @"value": currentData.value}];
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for the js event handler to call tryCommitTransaction
|
|
|
|
// this wait occurs on the Firebase Worker Queue
|
|
|
|
// so if the tryCommitTransaction fails to signal the semaphore
|
|
|
|
// no further blocks will be executed by Firebase until the timeout expires
|
|
|
|
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC);
|
|
|
|
BOOL timedout = dispatch_semaphore_wait(sema, delayTime) != 0;
|
|
|
|
|
|
|
|
BOOL abort = [transactionState valueForKey:@"abort"] || timedout;
|
|
|
|
id value = [transactionState valueForKey:@"value"];
|
|
|
|
|
|
|
|
dispatch_barrier_async(_transactionQueue, ^{
|
|
|
|
[_transactions removeObjectForKey:identifier];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (abort) {
|
|
|
|
return [FIRTransactionResult abort];
|
|
|
|
} else {
|
|
|
|
currentData.value = value;
|
|
|
|
return [FIRTransactionResult successWithValue:currentData];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
andCompletionBlock:
|
|
|
|
^(NSError *_Nullable databaseError, BOOL committed, FIRDataSnapshot *_Nullable snapshot) {
|
2017-03-25 00:59:27 +00:00
|
|
|
if (databaseError != nil) {
|
2017-05-25 15:00:12 +00:00
|
|
|
[self sendTransactionEvent:DATABASE_TRANSACTION_EVENT body:@{@"id": identifier, @"type": @"error", @"code": @([databaseError code]), @"message": [databaseError description]}];
|
2017-03-25 00:59:27 +00:00
|
|
|
} else {
|
2017-05-25 15:00:12 +00:00
|
|
|
[self sendTransactionEvent:DATABASE_TRANSACTION_EVENT body:@{@"id": identifier, @"type": @"complete", @"committed": @(committed), @"snapshot": [RNFirebaseDBReference snapshotToDict:snapshot],}];
|
2017-03-25 00:59:27 +00:00
|
|
|
}
|
2017-05-25 15:00:12 +00:00
|
|
|
}
|
|
|
|
withLocalEvents:
|
|
|
|
applyLocally];
|
2017-03-25 00:59:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(tryCommitTransaction:
|
|
|
|
(NSString *) identifier
|
|
|
|
withData:
|
|
|
|
(NSDictionary *) data) {
|
2017-03-25 00:59:27 +00:00
|
|
|
__block NSMutableDictionary *transactionState;
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
dispatch_sync(_transactionQueue, ^{
|
2017-05-25 15:00:12 +00:00
|
|
|
transactionState = _transactions[identifier];
|
2017-03-25 00:59:27 +00:00
|
|
|
});
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
if (!transactionState) {
|
|
|
|
NSLog(@"tryCommitTransaction for unknown ID %@", identifier);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
dispatch_semaphore_t sema = [transactionState valueForKey:@"semaphore"];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
BOOL abort = [[data valueForKey:@"abort"] boolValue];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
if (abort) {
|
|
|
|
[transactionState setValue:@true forKey:@"abort"];
|
|
|
|
} else {
|
|
|
|
id newValue = [data valueForKey:@"value"];
|
|
|
|
[transactionState setValue:newValue forKey:@"value"];
|
|
|
|
}
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-25 00:59:27 +00:00
|
|
|
dispatch_semaphore_signal(sema);
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(enablePersistence:
|
|
|
|
(BOOL) enable
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
BOOL isEnabled = [FIRDatabase database].persistenceEnabled;
|
2017-05-25 15:00:12 +00:00
|
|
|
if (isEnabled != enable) {
|
2017-05-09 16:20:06 +00:00
|
|
|
@try {
|
2017-05-25 15:00:12 +00:00
|
|
|
[FIRDatabase database].persistenceEnabled = enable;
|
2017-05-09 16:20:06 +00:00
|
|
|
} @catch (NSException *exception) {
|
|
|
|
// do nothing - for RN packager reloads
|
|
|
|
}
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"result": @"success"}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(keepSynced:
|
|
|
|
(NSString *) path
|
|
|
|
withEnable:
|
|
|
|
(BOOL) enable
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
|
|
|
[ref keepSynced:enable];
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"path": path}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(set:
|
|
|
|
(NSString *) path
|
|
|
|
data:
|
|
|
|
(NSDictionary *) data
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref setValue:[data valueForKey:@"value"] withCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[self handleCallback:@"set" callback:callback databaseError:error];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(update:
|
|
|
|
(NSString *) path
|
|
|
|
value:
|
|
|
|
(NSDictionary *) value
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref updateChildValues:value withCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[self handleCallback:@"update" callback:callback databaseError:error];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(remove:
|
|
|
|
(NSString *) path
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref removeValueWithCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[self handleCallback:@"remove" callback:callback databaseError:error];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(push:
|
|
|
|
(NSString *) path
|
|
|
|
data:
|
|
|
|
(NSDictionary *) data
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
|
|
|
FIRDatabaseReference *newRef = [ref childByAutoId];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
NSURL *url = [NSURL URLWithString:newRef.URL];
|
|
|
|
NSString *newPath = [url path];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
if ([data count] > 0) {
|
2017-05-25 15:00:12 +00:00
|
|
|
[newRef setValue:[data valueForKey:@"value"] withCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
if (error != nil) {
|
|
|
|
// Error handling
|
2017-05-25 15:00:12 +00:00
|
|
|
NSDictionary *evt = @{@"code": @([error code]), @"details": [error debugDescription], @"message": [error localizedDescription], @"description": [error description]};
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
callback(@[evt]);
|
|
|
|
} else {
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"ref": newPath}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
} else {
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"ref": newPath}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(on:
|
|
|
|
(nonnull
|
|
|
|
NSNumber *) refId
|
|
|
|
path:(NSString *) path
|
|
|
|
modifiers:(NSArray *) modifiers
|
|
|
|
listenerId:(nonnull NSNumber *) listenerId
|
|
|
|
name:(NSString *) eventName
|
|
|
|
callback:(RCTResponseSenderBlock) callback) {
|
2017-04-26 16:34:16 +00:00
|
|
|
RNFirebaseDBReference *ref = [self getDBHandle:refId path:path modifiers:modifiers];
|
|
|
|
[ref addEventHandler:listenerId eventName:eventName];
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"refId": refId, @"handle": path}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(once:
|
|
|
|
(nonnull
|
|
|
|
NSNumber *) refId
|
|
|
|
path:(NSString *) path
|
|
|
|
modifiers:(NSArray *) modifiers
|
|
|
|
eventName:(NSString *) eventName
|
|
|
|
callback:(RCTResponseSenderBlock) callback) {
|
2017-04-26 16:34:16 +00:00
|
|
|
RNFirebaseDBReference *ref = [self getDBHandle:refId path:path modifiers:modifiers];
|
2017-07-04 13:13:49 +00:00
|
|
|
[ref addSingleEventHandler:eventName callback:callback];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(off:
|
|
|
|
(nonnull
|
|
|
|
NSNumber *) refId
|
|
|
|
listeners:(NSArray *) listeners
|
|
|
|
callback:(RCTResponseSenderBlock) callback) {
|
|
|
|
RNFirebaseDBReference *ref = _dbReferences[refId];
|
2017-04-26 16:34:16 +00:00
|
|
|
if (ref != nil) {
|
|
|
|
for (NSDictionary *listener in listeners) {
|
|
|
|
NSNumber *listenerId = [listener valueForKey:@"listenerId"];
|
|
|
|
NSString *eventName = [listener valueForKey:@"eventName"];
|
|
|
|
[ref removeEventHandler:listenerId eventName:eventName];
|
2017-03-09 15:26:28 +00:00
|
|
|
if (![ref hasListeners]) {
|
2017-04-26 16:34:16 +00:00
|
|
|
[_dbReferences removeObjectForKey:refId];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"refId": refId,}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// On disconnect
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(onDisconnectSet:
|
|
|
|
(NSString *) path
|
|
|
|
props:
|
|
|
|
(NSDictionary *) props
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref onDisconnectSetValue:props[@"value"] withCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-07-04 13:13:49 +00:00
|
|
|
[self handleCallback:@"onDisconnectSetObject" callback:callback databaseError:error];
|
|
|
|
}];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(onDisconnectRemove:
|
|
|
|
(NSString *) path
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref onDisconnectRemoveValueWithCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[self handleCallback:@"onDisconnectRemove" callback:callback databaseError:error];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(onDisconnectCancel:
|
|
|
|
(NSString *) path
|
|
|
|
callback:
|
|
|
|
(RCTResponseSenderBlock) callback) {
|
2017-03-09 15:26:28 +00:00
|
|
|
FIRDatabaseReference *ref = [self getPathRef:path];
|
2017-05-25 15:00:12 +00:00
|
|
|
[ref cancelDisconnectOperationsWithCompletionBlock:^(NSError *_Nullable error, FIRDatabaseReference *_Nonnull _ref) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[self handleCallback:@"onDisconnectCancel" callback:callback databaseError:error];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(goOffline) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[FIRDatabase database].goOffline;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
RCT_EXPORT_METHOD(goOnline) {
|
2017-03-09 15:26:28 +00:00
|
|
|
[FIRDatabase database].goOnline;
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (FIRDatabaseReference *)getPathRef:(NSString *)path {
|
2017-03-09 15:26:28 +00:00
|
|
|
return [[[FIRDatabase database] reference] child:path];
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (void)handleCallback:(NSString *)methodName callback:(RCTResponseSenderBlock)callback databaseError:(NSError *)databaseError {
|
2017-03-09 15:26:28 +00:00
|
|
|
if (databaseError != nil) {
|
2017-05-25 15:00:12 +00:00
|
|
|
NSDictionary *evt = @{@"code": @([databaseError code]), @"details": [databaseError debugDescription], @"message": [databaseError localizedDescription], @"description": [databaseError description]};
|
2017-03-09 15:26:28 +00:00
|
|
|
callback(@[evt]);
|
|
|
|
} else {
|
2017-05-25 15:00:12 +00:00
|
|
|
callback(@[[NSNull null], @{@"status": @"success", @"method": methodName}]);
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
- (RNFirebaseDBReference *)getDBHandle:(NSNumber *)refId path:(NSString *)path modifiers:(NSArray *)modifiers {
|
|
|
|
RNFirebaseDBReference *ref = _dbReferences[refId];
|
2017-04-26 16:34:16 +00:00
|
|
|
|
2017-03-09 15:26:28 +00:00
|
|
|
if (ref == nil) {
|
2017-05-25 15:00:12 +00:00
|
|
|
ref = [[RNFirebaseDBReference alloc] initWithPathAndModifiers:self database:[FIRDatabase database] refId:refId path:path modifiers:modifiers];
|
|
|
|
_dbReferences[refId] = ref;
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not sure how to get away from this... yet
|
|
|
|
- (NSArray<NSString *> *)supportedEvents {
|
2017-03-25 00:59:27 +00:00
|
|
|
return @[DATABASE_DATA_EVENT, DATABASE_ERROR_EVENT, DATABASE_TRANSACTION_EVENT];
|
2017-03-09 15:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
@end
|
2017-03-09 15:26:28 +00:00
|
|
|
|
2017-05-25 15:00:12 +00:00
|
|
|
#else
|
|
|
|
@implementation RNFirebaseDatabase
|
2017-03-09 15:26:28 +00:00
|
|
|
@end
|
2017-05-25 15:00:12 +00:00
|
|
|
#endif
|