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

View File

@ -39,7 +39,7 @@ RCT_EXPORT_METHOD(keepSynced:(NSString *) appName
path:(NSString *) path
modifiers:(NSArray *) modifiers
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];
}
@ -233,13 +233,13 @@ RCT_EXPORT_METHOD(once:(NSString *) appName
eventName:(NSString *) eventName
resolver:(RCTPromiseResolveBlock) resolve
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];
}
RCT_EXPORT_METHOD(on:(NSString *) appName
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"]];
}
@ -278,15 +278,20 @@ RCT_EXPORT_METHOD(off:(NSString *) key
return [[RNFirebaseDatabase getDatabaseForApp:appName] referenceWithPath:path];
}
- (RNFirebaseDatabaseReference *)getInternalReferenceForApp:(NSString *)appName key:(NSString *)key path:(NSString *)path modifiers:(NSArray *)modifiers keep:(BOOL)keep {
RNFirebaseDatabaseReference *ref = _dbReferences[key];
- (RNFirebaseDatabaseReference *)getInternalReferenceForApp:(NSString *)appName key:(NSString *)key path:(NSString *)path modifiers:(NSArray *)modifiers {
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) {
ref = [[RNFirebaseDatabaseReference alloc] initWithPathAndModifiers:self app:appName key:key refPath:path modifiers:modifiers];
if (keep) {
_dbReferences[key] = ref;
}
_dbReferences[key] = ref;
}
return ref;
}

View File

@ -26,6 +26,7 @@ export default class Query {
*/
orderBy(name: string, key?: string) {
this.modifiers.push({
id: `orderBy-${name}:${key}`,
type: 'orderBy',
name,
key,
@ -42,6 +43,7 @@ export default class Query {
*/
limit(name: string, limit: number) {
this.modifiers.push({
id: `limit-${name}:${limit}`,
type: 'limit',
name,
limit,
@ -59,6 +61,7 @@ export default class Query {
*/
filter(name: string, value: any, key?: string) {
this.modifiers.push({
id: `filter-${name}:${objectToUniqueId(value)}:${key}`,
type: 'filter',
name,
value,
@ -82,14 +85,21 @@ export default class Query {
* @return {*}
*/
queryIdentifier() {
// convert query modifiers array into an object for generating a unique key
const object = {};
// sort modifiers to enforce ordering
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++) {
const { name, type, value } = this.modifiers[i];
object[`${type}-${name}`] = value;
// Convert modifiers to unique key
let key = '{';
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
.orderByChild('number')
.limitToLast(2)
.limitToLast(1)
.once('value')
.then((snapshot) => {
const val = snapshot.val();
// Assertion
val.key2.should.eql(DatabaseContents.ISSUES[521].key2);
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();
});
@ -185,14 +184,13 @@ function issueTests({ describe, it, context, firebase }) {
return ref
.orderByChild('string')
.limitToLast(2)
.limitToLast(1)
.once('value')
.then((snapshot) => {
const val = snapshot.val();
// Assertion
val.key2.should.eql(DatabaseContents.ISSUES[521].key2);
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();
});