Skip to content

Commit dba8c11

Browse files
committed
Resolve promise and send callback appropriate result.
1 parent 15e49a3 commit dba8c11

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

ios/Firestack/FirestackDatabase.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ @interface FirestackDBReference : NSObject
2121
@property FIRDatabaseHandle childRemovedHandler;
2222
@property FIRDatabaseHandle childMovedHandler;
2323
@property FIRDatabaseHandle childValueHandler;
24+
+ (NSDictionary *) snapshotToDict:(FIRDataSnapshot *) snapshot;
2425
@end
2526

2627
@implementation FirestackDBReference
@@ -46,7 +47,7 @@ - (void) addEventHandler:(NSString *) eventName
4647
{
4748
if (![self isListeningTo:eventName]) {
4849
id withBlock = ^(FIRDataSnapshot * _Nonnull snapshot) {
49-
NSDictionary *props = [self snapshotToDict:snapshot];
50+
NSDictionary *props = [FirestackDBReference snapshotToDict:snapshot];
5051
[self sendJSEvent:DATABASE_DATA_EVENT
5152
title:eventName
5253
props: @{
@@ -74,7 +75,7 @@ - (void) addSingleEventHandler:(RCTResponseSenderBlock) callback
7475
{
7576
[_query observeSingleEventOfType:FIRDataEventTypeValue
7677
withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
77-
NSDictionary *props = [self snapshotToDict:snapshot];
78+
NSDictionary *props = [FirestackDBReference snapshotToDict:snapshot];
7879
callback(@[[NSNull null], @{
7980
@"eventName": @"value",
8081
@"path": _path,
@@ -131,7 +132,7 @@ - (void) removeEventHandler:(NSString *) name
131132
[self unsetListeningOn:name];
132133
}
133134

134-
- (NSDictionary *) snapshotToDict:(FIRDataSnapshot *) snapshot
135+
+ (NSDictionary *) snapshotToDict:(FIRDataSnapshot *) snapshot
135136
{
136137
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
137138
[dict setValue:snapshot.key forKey:@"key"];
@@ -485,8 +486,22 @@ - (id) init
485486
currentData.value = value;
486487
return [FIRTransactionResult successWithValue:currentData];
487488
}
488-
} andCompletionBlock:^(NSError * _Nullable error, BOOL committed, FIRDataSnapshot * _Nullable snapshot) {
489-
[self handleCallback:@"transaction" callback:onComplete databaseError:error];
489+
} andCompletionBlock:^(NSError * _Nullable databaseError, BOOL committed, FIRDataSnapshot * _Nullable snapshot) {
490+
if (databaseError != nil) {
491+
NSDictionary *evt = @{
492+
@"errorCode": [NSNumber numberWithInt:[databaseError code]],
493+
@"errorDetails": [databaseError debugDescription],
494+
@"description": [databaseError description]
495+
};
496+
onComplete(@[evt]);
497+
} else {
498+
onComplete(@[[NSNull null], @{
499+
@"committed": [NSNumber numberWithBool:committed],
500+
@"snapshot": [FirestackDBReference snapshotToDict:snapshot],
501+
@"status": @"success",
502+
@"method": @"transaction"
503+
}]);
504+
}
490505
} withLocalEvents:applyLocally];
491506
});
492507
}

lib/modules/database/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,17 @@ export default class Database extends Base {
164164
this.transactions[id] = updateCallback;
165165
return new Promise((resolve, reject) => {
166166
FirestackDatabase.beginTransaction(path, id, applyLocally || false, (error, result) => {
167-
onComplete && onComplete(error);
168-
if (error)
167+
let snapshot;
168+
if (result.snapshot)
169+
snapshot = new Snapshot(new Reference(this, path.split('/'), null), result.snapshot);
170+
onComplete && onComplete(error, snapshot);
171+
if (error) {
169172
reject(error);
170-
else
171-
resolve();
173+
}
174+
else {
175+
let {committed} = result;
176+
resolve({committed, snapshot});
177+
}
172178
delete this.transactions[id];
173179
});
174180
});

0 commit comments

Comments
 (0)