Skip to content

Commit

Permalink
[both][database] all 'once' event types now supported - not just 'value'
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Jul 4, 2017
1 parent d1196b9 commit 2f67e8b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ public void on(final int refId, final String path, final ReadableArray modifiers
@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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import android.support.annotation.Nullable;
import android.telecom.Call;
import android.util.Log;

import java.util.Map;
Expand Down Expand Up @@ -132,10 +133,65 @@ public void onCancelled(DatabaseError error) {
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);
Expand Down
14 changes: 1 addition & 13 deletions ios/RNFirebase/database/RNFirebaseDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,7 @@ - (void)addEventHandler:(NSNumber *)listenerId eventName:(NSString *)eventName {
}

- (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];
Expand Down

0 comments on commit 2f67e8b

Please sign in to comment.