[auth] Pass previousChildName back to callback

This commit is contained in:
Elliot Hesp 2017-06-15 14:55:52 +01:00
parent 840f6b0ced
commit 3809fa8f63
3 changed files with 18 additions and 13 deletions

View File

@ -3,6 +3,8 @@ package io.invertase.firebase.database;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.Map;
@ -50,28 +52,28 @@ public class RNFirebaseDatabaseReference {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_added".equals(eventName)) {
handleDatabaseEvent("child_added", listenerId, dataSnapshot);
handleDatabaseEvent("child_added", listenerId, dataSnapshot, previousChildName);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_changed".equals(eventName)) {
handleDatabaseEvent("child_changed", listenerId, dataSnapshot);
handleDatabaseEvent("child_changed", listenerId, dataSnapshot, previousChildName);
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
if ("child_removed".equals(eventName)) {
handleDatabaseEvent("child_removed", listenerId, dataSnapshot);
handleDatabaseEvent("child_removed", listenerId, dataSnapshot, null);
}
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_moved".equals(eventName)) {
handleDatabaseEvent("child_moved", listenerId, dataSnapshot);
handleDatabaseEvent("child_moved", listenerId, dataSnapshot, previousChildName);
}
}
@ -94,7 +96,7 @@ public class RNFirebaseDatabaseReference {
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
handleDatabaseEvent("value", listenerId, dataSnapshot);
handleDatabaseEvent("value", listenerId, dataSnapshot, null);
}
@Override
@ -166,11 +168,14 @@ public class RNFirebaseDatabaseReference {
}
}
private void handleDatabaseEvent(final String name, final Integer listenerId, final DataSnapshot dataSnapshot) {
private void handleDatabaseEvent(final String name, final Integer listenerId, final DataSnapshot dataSnapshot, @Nullable String previousChildName) {
WritableMap data = Utils.snapshotToMap(name, mRefId, listenerId, mPath, dataSnapshot);
WritableMap evt = Arguments.createMap();
evt.putString("eventName", name);
evt.putMap("body", data);
if (previousChildName != null) {
evt.putString("previousChildName", previousChildName);
}
Utils.sendEvent(mReactContext, "database_event", evt);
}

View File

@ -31,9 +31,9 @@
- (void)addEventHandler:(NSNumber *)listenerId eventName:(NSString *)eventName {
if (!_listeners[listenerId]) {
id withBlock = ^(FIRDataSnapshot *_Nonnull snapshot) {
id andPreviousSiblingKeyWithBlock = ^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
NSDictionary *props = [RNFirebaseDBReference snapshotToDict:snapshot];
[self sendJSEvent:DATABASE_DATA_EVENT title:eventName props:@{@"eventName": eventName, @"refId": _refId, @"listenerId": listenerId, @"path": _path, @"snapshot": props}];
[self sendJSEvent:DATABASE_DATA_EVENT title:eventName props:@{@"eventName": eventName, @"refId": _refId, @"listenerId": listenerId, @"path": _path, @"snapshot": props, @"previousChildName":previousChildName}];
};
id errorBlock = ^(NSError *_Nonnull error) {
NSLog(@"Error onDBEvent: %@", [error debugDescription]);
@ -41,7 +41,7 @@
[self getAndSendDatabaseError:error listenerId:listenerId];
};
int eventType = [self eventTypeFromName:eventName];
FIRDatabaseHandle handle = [_query observeEventType:eventType withBlock:withBlock withCancelBlock:errorBlock];
FIRDatabaseHandle handle = [_query observeEventType:eventType andPreviousSiblingKeyWithBlock:andPreviousSiblingKeyWithBlock withCancelBlock:errorBlock];
_listeners[listenerId] = @(handle);
} else {
NSLog(@"Warning Trying to add duplicate listener for refId: %@ listenerId: %@", _refId, listenerId);
@ -49,9 +49,9 @@
}
- (void)addSingleEventHandler:(RCTResponseSenderBlock)callback {
[_query observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *_Nonnull snapshot) {
[_query observeSingleEventOfType:FIRDataEventTypeValue andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
NSDictionary *props = [RNFirebaseDBReference snapshotToDict:snapshot];
callback(@[[NSNull null], @{@"eventName": @"value", @"path": _path, @"refId": _refId, @"snapshot": props}]);
callback(@[[NSNull null], @{@"eventName": @"value", @"path": _path, @"refId": _refId, @"snapshot": props, @"previousChildName":previousChildName}]);
} 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]}]);

View File

@ -155,11 +155,11 @@ export default class Database extends Base {
*/
_handleDatabaseEvent(event: Object) {
const body = event.body || {};
const { refId, listenerId, path, eventName, snapshot } = body;
const { refId, listenerId, path, eventName, snapshot, previousChildName } = body;
this.log.debug('_handleDatabaseEvent: ', refId, listenerId, path, eventName, snapshot && snapshot.key);
if (this.references[refId] && this.references[refId].refListeners[listenerId]) {
const cb = this.references[refId].refListeners[listenerId].successCallback;
cb(new Snapshot(this.references[refId], snapshot));
cb(new Snapshot(this.references[refId], snapshot), previousChildName);
} else {
FirebaseDatabase.off(refId, [{ listenerId, eventName }], () => {
this.log.debug('_handleDatabaseEvent: No JS listener registered, removed native listener', refId, listenerId, eventName);