2017-07-19 15:02:26 +00:00
|
|
|
#import "RNFirebaseDatabaseReference.h"
|
|
|
|
|
|
|
|
@implementation RNFirebaseDatabaseReference
|
|
|
|
|
2017-08-11 15:07:32 +00:00
|
|
|
#if __has_include(<FirebaseDatabase/FIRDatabase.h>)
|
|
|
|
|
2017-08-02 15:51:00 +00:00
|
|
|
- (id)initWithPathAndModifiers:(RCTEventEmitter *)emitter
|
2018-03-04 23:45:37 +00:00
|
|
|
appDisplayName:(NSString *)appDisplayName
|
|
|
|
key:(NSString *)key
|
|
|
|
refPath:(NSString *)refPath
|
|
|
|
modifiers:(NSArray *)modifiers {
|
2017-07-19 15:02:26 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_emitter = emitter;
|
2018-01-03 20:00:38 +00:00
|
|
|
_appDisplayName = appDisplayName;
|
2017-08-22 16:29:02 +00:00
|
|
|
_key = key;
|
2017-08-02 15:51:00 +00:00
|
|
|
_path = refPath;
|
2017-07-19 15:02:26 +00:00
|
|
|
_listeners = [[NSMutableDictionary alloc] init];
|
2017-08-02 15:51:00 +00:00
|
|
|
_query = [self buildQueryAtPathWithModifiers:refPath modifiers:modifiers];
|
2017-07-19 15:02:26 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-22 16:29:02 +00:00
|
|
|
- (void)removeEventListener:(NSString *)eventRegistrationKey {
|
2018-03-04 23:45:37 +00:00
|
|
|
FIRDatabaseHandle handle = (FIRDatabaseHandle)[_listeners[eventRegistrationKey] integerValue];
|
2017-08-22 16:29:02 +00:00
|
|
|
if (handle) {
|
|
|
|
[_query removeObserverWithHandle:handle];
|
|
|
|
[_listeners removeObjectForKey:eventRegistrationKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (void)on:(NSString *)eventType registration:(NSDictionary *)registration {
|
2017-08-22 16:29:02 +00:00
|
|
|
NSString *eventRegistrationKey = registration[@"eventRegistrationKey"];
|
|
|
|
if (![self hasEventListener:eventRegistrationKey]) {
|
2017-07-19 15:02:26 +00:00
|
|
|
id andPreviousSiblingKeyWithBlock = ^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
|
2017-08-22 16:29:02 +00:00
|
|
|
[self handleDatabaseEvent:eventType registration:registration dataSnapshot:snapshot previousChildName:previousChildName];
|
2017-07-19 15:02:26 +00:00
|
|
|
};
|
|
|
|
id errorBlock = ^(NSError *_Nonnull error) {
|
|
|
|
NSLog(@"Error onDBEvent: %@", [error debugDescription]);
|
2017-08-22 16:29:02 +00:00
|
|
|
[self removeEventListener:eventRegistrationKey];
|
|
|
|
[self handleDatabaseError:registration error:error];
|
2017-07-19 15:02:26 +00:00
|
|
|
};
|
2017-08-22 16:29:02 +00:00
|
|
|
FIRDataEventType firDataEventType = (FIRDataEventType)[self eventTypeFromName:eventType];
|
|
|
|
FIRDatabaseHandle handle = [_query observeEventType:firDataEventType andPreviousSiblingKeyWithBlock:andPreviousSiblingKeyWithBlock withCancelBlock:errorBlock];
|
|
|
|
_listeners[eventRegistrationKey] = @(handle);
|
2017-07-19 15:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (void)once:(NSString *)eventType
|
|
|
|
resolver:(RCTPromiseResolveBlock)resolve
|
|
|
|
rejecter:(RCTPromiseRejectBlock)reject {
|
2017-08-22 16:29:02 +00:00
|
|
|
FIRDataEventType firDataEventType = (FIRDataEventType)[self eventTypeFromName:eventType];
|
2017-07-19 15:02:26 +00:00
|
|
|
[_query observeSingleEventOfType:firDataEventType andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
|
2017-08-22 16:29:02 +00:00
|
|
|
NSDictionary *data = [RNFirebaseDatabaseReference snapshotToDictionary:snapshot previousChildName:previousChildName];
|
2017-08-02 15:51:00 +00:00
|
|
|
resolve(data);
|
2017-07-19 15:02:26 +00:00
|
|
|
} withCancelBlock:^(NSError *_Nonnull error) {
|
|
|
|
NSLog(@"Error onDBEventOnce: %@", [error debugDescription]);
|
2017-08-02 15:51:00 +00:00
|
|
|
[RNFirebaseDatabase handlePromise:resolve rejecter:reject databaseError:error];
|
2017-07-19 15:02:26 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (void)handleDatabaseEvent:(NSString *)eventType
|
|
|
|
registration:(NSDictionary *)registration
|
|
|
|
dataSnapshot:(FIRDataSnapshot *)dataSnapshot
|
|
|
|
previousChildName:(NSString *)previousChildName {
|
2017-08-22 16:29:02 +00:00
|
|
|
NSMutableDictionary *event = [[NSMutableDictionary alloc] init];
|
|
|
|
NSDictionary *data = [RNFirebaseDatabaseReference snapshotToDictionary:dataSnapshot previousChildName:previousChildName];
|
2018-03-04 23:45:37 +00:00
|
|
|
|
2017-08-22 16:29:02 +00:00
|
|
|
[event setValue:data forKey:@"data"];
|
|
|
|
[event setValue:_key forKey:@"key"];
|
|
|
|
[event setValue:eventType forKey:@"eventType"];
|
|
|
|
[event setValue:registration forKey:@"registration"];
|
2018-03-04 23:45:37 +00:00
|
|
|
|
2017-10-26 10:55:07 +00:00
|
|
|
[RNFirebaseUtil sendJSEvent:self.emitter name:DATABASE_SYNC_EVENT body:event];
|
2017-07-19 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (void)handleDatabaseError:(NSDictionary *)registration
|
2017-08-22 16:29:02 +00:00
|
|
|
error:(NSError *)error {
|
|
|
|
NSMutableDictionary *event = [[NSMutableDictionary alloc] init];
|
|
|
|
[event setValue:_key forKey:@"key"];
|
|
|
|
[event setValue:[RNFirebaseDatabase getJSError:error] forKey:@"error"];
|
|
|
|
[event setValue:registration forKey:@"registration"];
|
2018-03-04 23:45:37 +00:00
|
|
|
|
2017-10-26 10:55:07 +00:00
|
|
|
[RNFirebaseUtil sendJSEvent:self.emitter name:DATABASE_SYNC_EVENT body:event];
|
2017-08-22 16:29:02 +00:00
|
|
|
}
|
2017-08-11 16:37:12 +00:00
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
+ (NSDictionary *)snapshotToDictionary:(FIRDataSnapshot *)dataSnapshot
|
|
|
|
previousChildName:(NSString *)previousChildName {
|
2017-08-22 16:29:02 +00:00
|
|
|
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
|
|
|
|
NSDictionary *snapshot = [RNFirebaseDatabaseReference snapshotToDict:dataSnapshot];
|
|
|
|
|
|
|
|
[result setValue:snapshot forKey:@"snapshot"];
|
|
|
|
[result setValue:previousChildName forKey:@"previousChildName"];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSDictionary *)snapshotToDict:(FIRDataSnapshot *)dataSnapshot {
|
|
|
|
NSMutableDictionary *snapshot = [[NSMutableDictionary alloc] init];
|
2018-03-04 23:45:37 +00:00
|
|
|
|
2017-08-02 15:51:00 +00:00
|
|
|
[snapshot setValue:dataSnapshot.key forKey:@"key"];
|
|
|
|
[snapshot setValue:@(dataSnapshot.exists) forKey:@"exists"];
|
|
|
|
[snapshot setValue:@(dataSnapshot.hasChildren) forKey:@"hasChildren"];
|
|
|
|
[snapshot setValue:@(dataSnapshot.childrenCount) forKey:@"childrenCount"];
|
|
|
|
[snapshot setValue:[RNFirebaseDatabaseReference getChildKeys:dataSnapshot] forKey:@"childKeys"];
|
|
|
|
[snapshot setValue:dataSnapshot.priority forKey:@"priority"];
|
2017-08-22 16:29:02 +00:00
|
|
|
[snapshot setValue:dataSnapshot.value forKey:@"value"];
|
2018-03-04 23:45:37 +00:00
|
|
|
|
2017-08-22 16:29:02 +00:00
|
|
|
return snapshot;
|
2017-08-02 15:51:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
+ (NSMutableArray *)getChildKeys:(FIRDataSnapshot *)snapshot {
|
2017-08-02 15:51:00 +00:00
|
|
|
NSMutableArray *childKeys = [NSMutableArray array];
|
|
|
|
if (snapshot.childrenCount > 0) {
|
|
|
|
NSEnumerator *children = [snapshot children];
|
|
|
|
FIRDataSnapshot *child;
|
|
|
|
while (child = [children nextObject]) {
|
|
|
|
[childKeys addObject:child.key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return childKeys;
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (FIRDatabaseQuery *)buildQueryAtPathWithModifiers:(NSString *)path
|
2017-08-02 15:51:00 +00:00
|
|
|
modifiers:(NSArray *)modifiers {
|
2018-01-03 20:00:38 +00:00
|
|
|
FIRDatabase *firebaseDatabase = [RNFirebaseDatabase getDatabaseForApp:_appDisplayName];
|
2017-08-02 15:51:00 +00:00
|
|
|
FIRDatabaseQuery *query = [[firebaseDatabase reference] child:path];
|
2017-07-19 15:02:26 +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];
|
|
|
|
}
|
|
|
|
} 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];
|
|
|
|
}
|
|
|
|
} else if ([type isEqualToString:@"filter"]) {
|
|
|
|
NSString *valueType = [modifier valueForKey:@"valueType"];
|
|
|
|
NSString *key = [modifier valueForKey:@"key"];
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)getIdValue:(NSString *)value type:(NSString *)type {
|
|
|
|
if ([type isEqualToString:@"number"]) {
|
|
|
|
return @(value.doubleValue);
|
|
|
|
} else if ([type isEqualToString:@"boolean"]) {
|
|
|
|
return @(value.boolValue);
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:45:37 +00:00
|
|
|
- (BOOL)hasEventListener:(NSString *)eventRegistrationKey {
|
2017-08-22 16:29:02 +00:00
|
|
|
return _listeners[eventRegistrationKey] != nil;
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:02:26 +00:00
|
|
|
- (BOOL)hasListeners {
|
|
|
|
return [[_listeners allKeys] count] > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)eventTypeFromName:(NSString *)name {
|
|
|
|
int eventType = FIRDataEventTypeValue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-11 15:07:32 +00:00
|
|
|
#endif
|
2017-07-19 15:02:26 +00:00
|
|
|
|
|
|
|
@end
|
2018-03-04 23:45:37 +00:00
|
|
|
|