[database] Correctly differentiate limitToLast and other similar clauses

This commit is contained in:
Chris Bianca 2017-10-23 17:03:49 +01:00
parent a42194a32a
commit 9822cb34d4
4 changed files with 44 additions and 44 deletions

View File

@ -399,7 +399,7 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
*/ */
@ReactMethod @ReactMethod
public void on(String appName, ReadableMap props) { public void on(String appName, ReadableMap props) {
getInternalReferenceForApp(appName, props) getCachedInternalReferenceForApp(appName, props)
.on( .on(
props.getString("eventType"), props.getString("eventType"),
props.getMap("registration") props.getMap("registration")
@ -481,19 +481,13 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
* @return * @return
*/ */
private RNFirebaseDatabaseReference getInternalReferenceForApp(String appName, String key, String path, ReadableArray modifiers) { private RNFirebaseDatabaseReference getInternalReferenceForApp(String appName, String key, String path, ReadableArray modifiers) {
RNFirebaseDatabaseReference existingRef = references.get(key); return new RNFirebaseDatabaseReference(
getReactApplicationContext(),
if (existingRef == null) { appName,
existingRef = new RNFirebaseDatabaseReference( key,
getReactApplicationContext(), path,
appName, modifiers
key, );
path,
modifiers
);
}
return existingRef;
} }
/** /**
@ -503,7 +497,7 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
* @param props * @param props
* @return * @return
*/ */
private RNFirebaseDatabaseReference getInternalReferenceForApp(String appName, ReadableMap props) { private RNFirebaseDatabaseReference getCachedInternalReferenceForApp(String appName, ReadableMap props) {
String key = props.getString("key"); String key = props.getString("key");
String path = props.getString("path"); String path = props.getString("path");
ReadableArray modifiers = props.getArray("modifiers"); ReadableArray modifiers = props.getArray("modifiers");
@ -511,14 +505,7 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
RNFirebaseDatabaseReference existingRef = references.get(key); RNFirebaseDatabaseReference existingRef = references.get(key);
if (existingRef == null) { if (existingRef == null) {
existingRef = new RNFirebaseDatabaseReference( existingRef = getInternalReferenceForApp(appName, key, path, modifiers);
getReactApplicationContext(),
appName,
key,
path,
modifiers
);
references.put(key, existingRef); references.put(key, existingRef);
} }

View File

@ -39,7 +39,7 @@ RCT_EXPORT_METHOD(keepSynced:(NSString *) appName
path:(NSString *) path path:(NSString *) path
modifiers:(NSArray *) modifiers modifiers:(NSArray *) modifiers
state:(BOOL) state) { state:(BOOL) state) {
FIRDatabaseQuery *query = [self getInternalReferenceForApp:appName key:key path:path modifiers:modifiers keep:false].query; FIRDatabaseQuery *query = [self getInternalReferenceForApp:appName key:key path:path modifiers:modifiers].query;
[query keepSynced:state]; [query keepSynced:state];
} }
@ -233,13 +233,13 @@ RCT_EXPORT_METHOD(once:(NSString *) appName
eventName:(NSString *) eventName eventName:(NSString *) eventName
resolver:(RCTPromiseResolveBlock) resolve resolver:(RCTPromiseResolveBlock) resolve
rejecter:(RCTPromiseRejectBlock) reject) { rejecter:(RCTPromiseRejectBlock) reject) {
RNFirebaseDatabaseReference *ref = [self getInternalReferenceForApp:appName key:key path:path modifiers:modifiers keep:false]; RNFirebaseDatabaseReference *ref = [self getInternalReferenceForApp:appName key:key path:path modifiers:modifiers];
[ref once:eventName resolver:resolve rejecter:reject]; [ref once:eventName resolver:resolve rejecter:reject];
} }
RCT_EXPORT_METHOD(on:(NSString *) appName RCT_EXPORT_METHOD(on:(NSString *) appName
props:(NSDictionary *) props) { props:(NSDictionary *) props) {
RNFirebaseDatabaseReference *ref = [self getInternalReferenceForApp:appName key:props[@"key"] path:props[@"path"] modifiers:props[@"modifiers"] keep:false]; RNFirebaseDatabaseReference *ref = [self getCachedInternalReferenceForApp:appName props:props];
[ref on:props[@"eventType"] registration:props[@"registration"]]; [ref on:props[@"eventType"] registration:props[@"registration"]];
} }
@ -278,15 +278,20 @@ RCT_EXPORT_METHOD(off:(NSString *) key
return [[RNFirebaseDatabase getDatabaseForApp:appName] referenceWithPath:path]; return [[RNFirebaseDatabase getDatabaseForApp:appName] referenceWithPath:path];
} }
- (RNFirebaseDatabaseReference *)getInternalReferenceForApp:(NSString *)appName key:(NSString *)key path:(NSString *)path modifiers:(NSArray *)modifiers keep:(BOOL)keep { - (RNFirebaseDatabaseReference *)getInternalReferenceForApp:(NSString *)appName key:(NSString *)key path:(NSString *)path modifiers:(NSArray *)modifiers {
RNFirebaseDatabaseReference *ref = _dbReferences[key]; return [[RNFirebaseDatabaseReference alloc] initWithPathAndModifiers:self app:appName key:key refPath:path modifiers:modifiers];
}
- (RNFirebaseDatabaseReference *)getCachedInternalReferenceForApp:(NSString *)appName props:(NSDictionary *)props {
NSString *key = props[@"key"];
NSString *path = props[@"path"];
NSDictionary *modifiers = props[@"modifiers"];
RNFirebaseDatabaseReference *ref = _dbReferences[key];
if (ref == nil) { if (ref == nil) {
ref = [[RNFirebaseDatabaseReference alloc] initWithPathAndModifiers:self app:appName key:key refPath:path modifiers:modifiers]; ref = [[RNFirebaseDatabaseReference alloc] initWithPathAndModifiers:self app:appName key:key refPath:path modifiers:modifiers];
_dbReferences[key] = ref;
if (keep) {
_dbReferences[key] = ref;
}
} }
return ref; return ref;
} }

View File

@ -26,6 +26,7 @@ export default class Query {
*/ */
orderBy(name: string, key?: string) { orderBy(name: string, key?: string) {
this.modifiers.push({ this.modifiers.push({
id: `orderBy-${name}:${key}`,
type: 'orderBy', type: 'orderBy',
name, name,
key, key,
@ -42,6 +43,7 @@ export default class Query {
*/ */
limit(name: string, limit: number) { limit(name: string, limit: number) {
this.modifiers.push({ this.modifiers.push({
id: `limit-${name}:${limit}`,
type: 'limit', type: 'limit',
name, name,
limit, limit,
@ -59,6 +61,7 @@ export default class Query {
*/ */
filter(name: string, value: any, key?: string) { filter(name: string, value: any, key?: string) {
this.modifiers.push({ this.modifiers.push({
id: `filter-${name}:${objectToUniqueId(value)}:${key}`,
type: 'filter', type: 'filter',
name, name,
value, value,
@ -82,14 +85,21 @@ export default class Query {
* @return {*} * @return {*}
*/ */
queryIdentifier() { queryIdentifier() {
// convert query modifiers array into an object for generating a unique key // sort modifiers to enforce ordering
const object = {}; const sortedModifiers = this.getModifiers().sort((a, b) => {
if (a.id < b.id) return -1;
if (a.id > b.id) return 1;
return 0;
});
for (let i = 0, len = this.modifiers.length; i < len; i++) { // Convert modifiers to unique key
const { name, type, value } = this.modifiers[i]; let key = '{';
object[`${type}-${name}`] = value; for (let i = 0; i < sortedModifiers.length; i++) {
if (i !== 0) key += ',';
key += sortedModifiers[i].id;
} }
key += '}';
return objectToUniqueId(object); return key;
} }
} }

View File

@ -94,14 +94,13 @@ function issueTests({ describe, it, context, firebase }) {
return ref return ref
.orderByChild('number') .orderByChild('number')
.limitToLast(2) .limitToLast(1)
.once('value') .once('value')
.then((snapshot) => { .then((snapshot) => {
const val = snapshot.val(); const val = snapshot.val();
// Assertion // Assertion
val.key2.should.eql(DatabaseContents.ISSUES[521].key2);
val.key3.should.eql(DatabaseContents.ISSUES[521].key3); val.key3.should.eql(DatabaseContents.ISSUES[521].key3);
should.equal(Object.keys(val).length, 2); should.equal(Object.keys(val).length, 1);
return Promise.resolve(); return Promise.resolve();
}); });
@ -185,14 +184,13 @@ function issueTests({ describe, it, context, firebase }) {
return ref return ref
.orderByChild('string') .orderByChild('string')
.limitToLast(2) .limitToLast(1)
.once('value') .once('value')
.then((snapshot) => { .then((snapshot) => {
const val = snapshot.val(); const val = snapshot.val();
// Assertion // Assertion
val.key2.should.eql(DatabaseContents.ISSUES[521].key2);
val.key3.should.eql(DatabaseContents.ISSUES[521].key3); val.key3.should.eql(DatabaseContents.ISSUES[521].key3);
should.equal(Object.keys(val).length, 2); should.equal(Object.keys(val).length, 1);
return Promise.resolve(); return Promise.resolve();
}); });