[both][database] all 'once' event types now supported - not just 'value'

This commit is contained in:
Salakar 2017-07-04 16:17:42 +01:00
parent d1196b98c4
commit 2f67e8b35b
3 changed files with 63 additions and 14 deletions

View File

@ -299,7 +299,12 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
@ReactMethod
public void once(final int refId, final String path, final ReadableArray modifiers, final String eventName, final Callback callback) {
RNFirebaseDatabaseReference ref = this.getDBHandle(refId, path, modifiers);
ref.addOnceValueEventListener(callback);
if (eventName.equals("value")) {
ref.addOnceValueEventListener(callback);
} else {
ref.addChildOnceEventListener(eventName, callback);
}
}
/**

View File

@ -5,6 +5,7 @@ import java.util.HashSet;
import java.util.List;
import android.support.annotation.Nullable;
import android.telecom.Call;
import android.util.Log;
import java.util.Map;
@ -132,10 +133,65 @@ public class RNFirebaseDatabaseReference {
callback.invoke(err);
}
};
mQuery.addListenerForSingleValueEvent(onceValueEventListener);
Log.d(TAG, "Added OnceValueEventListener for refId: " + mRefId);
}
void addChildOnceEventListener(final String eventName, final Callback callback) {
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_added".equals(eventName)) {
mQuery.removeEventListener(this);
WritableMap data = Utils.snapshotToMap("child_added", mRefId, null, mPath, dataSnapshot, previousChildName);
callback.invoke(null, data);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_changed".equals(eventName)) {
mQuery.removeEventListener(this);
WritableMap data = Utils.snapshotToMap("child_changed", mRefId, null, mPath, dataSnapshot, previousChildName);
callback.invoke(null, data);
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
if ("child_removed".equals(eventName)) {
mQuery.removeEventListener(this);
WritableMap data = Utils.snapshotToMap("child_removed", mRefId, null, mPath, dataSnapshot, null);
callback.invoke(null, data);
}
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
if ("child_moved".equals(eventName)) {
mQuery.removeEventListener(this);
WritableMap data = Utils.snapshotToMap("child_moved", mRefId, null, mPath, dataSnapshot, previousChildName);
callback.invoke(null, data);
}
}
@Override
public void onCancelled(DatabaseError error) {
mQuery.removeEventListener(this);
WritableMap err = Arguments.createMap();
err.putInt("refId", mRefId);
err.putString("path", mPath);
err.putInt("code", error.getCode());
err.putString("details", error.getDetails());
err.putString("message", error.getMessage());
callback.invoke(err);
}
};
mQuery.addChildEventListener(childEventListener);
}
void removeEventListener(int listenerId, String eventName) {
if ("value".equals(eventName)) {
this.removeValueEventListener(listenerId);

View File

@ -49,19 +49,7 @@
}
- (void)addSingleEventHandler:(NSString *)eventName callback:(RCTResponseSenderBlock)callback {
FIRDataEventType *firDataEventType;
if ([eventName isEqualToString:@"value"]) {
firDataEventType = (FIRDataEventType *) FIRDataEventTypeValue;
} else if ([eventName isEqualToString:@"child_added"]) {
firDataEventType = (FIRDataEventType *) FIRDataEventTypeChildAdded;
} else if ([eventName isEqualToString:@"child_removed"]) {
firDataEventType = (FIRDataEventType *) FIRDataEventTypeChildRemoved;
} else if ([eventName isEqualToString:@"child_changed"]) {
firDataEventType = (FIRDataEventType *) FIRDataEventTypeChildChanged;
} else if ([eventName isEqualToString:@"child_moved"]) {
firDataEventType = (FIRDataEventType *) FIRDataEventTypeChildMoved;
}
FIRDataEventType firDataEventType = (FIRDataEventType)[self eventTypeFromName:eventName];
[_query observeSingleEventOfType:firDataEventType andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *_Nonnull snapshot, NSString *_Nullable previousChildName) {
NSDictionary *props = [RNFirebaseDBReference snapshotToDict:snapshot];