android: fixed once and on error handling
This commit is contained in:
parent
677b1d2836
commit
214928ba7f
|
@ -1,7 +1,6 @@
|
||||||
{
|
{
|
||||||
"ignore_dirs": [
|
"ignore_dirs": [
|
||||||
".git",
|
".git",
|
||||||
"node_modules",
|
|
||||||
"android/build",
|
"android/build",
|
||||||
"android/.idea",
|
"android/.idea",
|
||||||
"android/.gradle",
|
"android/.gradle",
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.content.Context;
|
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.facebook.react.bridge.Callback;
|
import com.facebook.react.bridge.Callback;
|
||||||
|
@ -31,6 +30,7 @@ interface KeySetterFn {
|
||||||
public class RNFirebaseModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
|
public class RNFirebaseModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
|
||||||
private static final String TAG = "RNFirebase";
|
private static final String TAG = "RNFirebase";
|
||||||
private FirebaseApp app;
|
private FirebaseApp app;
|
||||||
|
private ReactApplicationContext reactContext;
|
||||||
|
|
||||||
public RNFirebaseModule(ReactApplicationContext reactContext) {
|
public RNFirebaseModule(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
|
@ -146,7 +146,7 @@ public class RNFirebaseModule extends ReactContextBaseJavaModule implements Life
|
||||||
try {
|
try {
|
||||||
Log.i(TAG, "Configuring app");
|
Log.i(TAG, "Configuring app");
|
||||||
if (app == null) {
|
if (app == null) {
|
||||||
app = FirebaseApp.initializeApp(getReactApplicationContext().getBaseContext(), builder.build());
|
app = FirebaseApp.initializeApp(reactContext, builder.build());
|
||||||
}
|
}
|
||||||
Log.i(TAG, "Configured");
|
Log.i(TAG, "Configured");
|
||||||
|
|
||||||
|
|
|
@ -191,7 +191,7 @@ public class RNFirebaseDatabase extends ReactContextBaseJavaModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void onOnce(final String path,
|
public void once(final String path,
|
||||||
final String modifiersString,
|
final String modifiersString,
|
||||||
final ReadableArray modifiersArray,
|
final ReadableArray modifiersArray,
|
||||||
final String eventName,
|
final String eventName,
|
||||||
|
|
|
@ -112,9 +112,11 @@ public class RNFirebaseDatabaseReference {
|
||||||
@Override
|
@Override
|
||||||
public void onCancelled(DatabaseError error) {
|
public void onCancelled(DatabaseError error) {
|
||||||
WritableMap err = Arguments.createMap();
|
WritableMap err = Arguments.createMap();
|
||||||
err.putInt("errorCode", error.getCode());
|
err.putString("path", mPath);
|
||||||
err.putString("errorDetails", error.getDetails());
|
err.putInt("code", error.getCode());
|
||||||
err.putString("description", error.getMessage());
|
err.putString("modifiers", mModifiersString);
|
||||||
|
err.putString("details", error.getDetails());
|
||||||
|
err.putString("message", error.getMessage());
|
||||||
callback.invoke(err);
|
callback.invoke(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -168,19 +170,15 @@ public class RNFirebaseDatabaseReference {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleDatabaseError(final DatabaseError error) {
|
private void handleDatabaseError(final DatabaseError error) {
|
||||||
WritableMap err = Arguments.createMap();
|
WritableMap errMap = Arguments.createMap();
|
||||||
err.putString("eventName", "database_error");
|
|
||||||
err.putString("path", mPath);
|
|
||||||
err.putString("modifiersString", mModifiersString);
|
|
||||||
err.putInt("errorCode", error.getCode());
|
|
||||||
err.putString("errorDetails", error.getDetails());
|
|
||||||
err.putString("msg", error.getMessage());
|
|
||||||
|
|
||||||
WritableMap evt = Arguments.createMap();
|
errMap.putString("path", mPath);
|
||||||
evt.putString("eventName", "database_error");
|
errMap.putInt("code", error.getCode());
|
||||||
evt.putMap("body", err);
|
errMap.putString("modifiers", mModifiersString);
|
||||||
|
errMap.putString("details", error.getDetails());
|
||||||
|
errMap.putString("message", error.getMessage());
|
||||||
|
|
||||||
Utils.sendEvent(mReactContext, "database_error", evt);
|
Utils.sendEvent(mReactContext, "database_error", errMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Query buildDatabaseQueryAtPathAndModifiers(final FirebaseDatabase firebaseDatabase,
|
private Query buildDatabaseQueryAtPathAndModifiers(final FirebaseDatabase firebaseDatabase,
|
||||||
|
|
|
@ -2,4 +2,5 @@
|
||||||
* @flow
|
* @flow
|
||||||
*/
|
*/
|
||||||
import firebase from './lib/firebase';
|
import firebase from './lib/firebase';
|
||||||
|
|
||||||
export default firebase;
|
export default firebase;
|
||||||
|
|
1
index.js
1
index.js
|
@ -1,2 +1,3 @@
|
||||||
import Firebase from './firebase';
|
import Firebase from './firebase';
|
||||||
|
|
||||||
export default Firebase;
|
export default Firebase;
|
||||||
|
|
|
@ -148,17 +148,6 @@ export default class Firebase extends Singleton {
|
||||||
return this._messaging;
|
return this._messaging;
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteConfig() {
|
|
||||||
if (!this._remoteConfig) {
|
|
||||||
this._remoteConfig = new RemoteConfig(this);
|
|
||||||
}
|
|
||||||
return this._remoteConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
get ServerValue(): Promise<*> {
|
|
||||||
return promisify('serverValue', FirebaseModule)();
|
|
||||||
}
|
|
||||||
|
|
||||||
get apps(): Array<string> {
|
get apps(): Array<string> {
|
||||||
return Object.keys(instances);
|
return Object.keys(instances);
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,17 +213,41 @@ export default class Database extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Converts an native error object to a 'firebase like' error.
|
||||||
* @param err
|
* @param error
|
||||||
|
* @returns {Error}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_handleDatabaseError(err: Object) {
|
_toFirebaseError(error) {
|
||||||
const body = err.body || {};
|
const { path, message, modifiers, code, details } = error;
|
||||||
const { path, modifiersString, eventName, msg } = body;
|
let firebaseMessage = `FirebaseError: ${message}`;
|
||||||
const handle = this._handle(path, modifiersString);
|
|
||||||
|
|
||||||
this.log.debug('_handleDatabaseError ->', handle, eventName, err);
|
if (path) {
|
||||||
|
firebaseMessage = `${firebaseMessage}\r\nPath: /${path}\r\n`;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.errorSubscriptions[handle]) this.errorSubscriptions[handle].forEach((cb) => cb(new Error(msg)));
|
const firebaseError = new Error(firebaseMessage);
|
||||||
|
|
||||||
|
firebaseError.code = code;
|
||||||
|
firebaseError.path = path;
|
||||||
|
firebaseError.details = details;
|
||||||
|
firebaseError.modifiers = modifiers;
|
||||||
|
|
||||||
|
return firebaseError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_handleDatabaseError(error: Object = {}) {
|
||||||
|
const { path, modifiers } = error;
|
||||||
|
const handle = this._handle(path, modifiers);
|
||||||
|
const firebaseError = this._toFirebaseError(error);
|
||||||
|
|
||||||
|
this.log.debug('_handleDatabaseError ->', handle, 'database_error', error);
|
||||||
|
|
||||||
|
if (this.errorSubscriptions[handle]) this.errorSubscriptions[handle].forEach(listener => listener(firebaseError));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,33 +94,54 @@ export default class Reference extends ReferenceBase {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
on(eventName: string, cb: () => any, errorCb: () => any) {
|
/**
|
||||||
if (!isFunction(cb)) throw new Error('The specified callback must be a function');
|
*
|
||||||
if (errorCb && !isFunction(errorCb)) throw new Error('The specified error callback must be a function');
|
* @param eventType
|
||||||
|
* @param successCallback
|
||||||
|
* @param failureCallback
|
||||||
|
* @param context TODO
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
on(eventType: string, successCallback: () => any, failureCallback: () => any, context) {
|
||||||
|
if (!isFunction(successCallback)) throw new Error('The specified callback must be a function');
|
||||||
|
if (failureCallback && !isFunction(failureCallback)) throw new Error('The specified error callback must be a function');
|
||||||
const path = this._dbPath();
|
const path = this._dbPath();
|
||||||
const modifiers = this.query.getModifiers();
|
const modifiers = this.query.getModifiers();
|
||||||
const modifiersString = this.query.getModifiersString();
|
const modifiersString = this.query.getModifiersString();
|
||||||
this.log.debug('adding reference.on', path, modifiersString, eventName);
|
this.log.debug('adding reference.on', path, modifiersString, eventType);
|
||||||
return this.db.on(path, modifiersString, modifiers, eventName, cb, errorCb);
|
return this.db.on(path, modifiersString, modifiers, eventType, successCallback, failureCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
once(eventName: string = 'once', cb: (snapshot: Object) => void) {
|
/**
|
||||||
|
*
|
||||||
|
* @param eventType
|
||||||
|
* @param successCallback
|
||||||
|
* @param failureCallback
|
||||||
|
* @param context TODO
|
||||||
|
* @returns {Promise.<TResult>}
|
||||||
|
*/
|
||||||
|
once(eventType: string = 'value', successCallback: (snapshot: Object) => void, failureCallback: (error: Error) => void) {
|
||||||
const path = this._dbPath();
|
const path = this._dbPath();
|
||||||
const modifiers = this.query.getModifiers();
|
const modifiers = this.query.getModifiers();
|
||||||
const modifiersString = this.query.getModifiersString();
|
const modifiersString = this.query.getModifiersString();
|
||||||
return promisify('onOnce', FirebaseDatabase)(path, modifiersString, modifiers, eventName)
|
return promisify('once', FirebaseDatabase)(path, modifiersString, modifiers, eventType)
|
||||||
.then(({ snapshot }) => new Snapshot(this, snapshot))
|
.then(({ snapshot }) => new Snapshot(this, snapshot))
|
||||||
.then((snapshot) => {
|
.then((snapshot) => {
|
||||||
if (isFunction(cb)) cb(snapshot);
|
if (isFunction(successCallback)) successCallback(snapshot);
|
||||||
return snapshot;
|
return snapshot;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
const firebaseError = this.db._toFirebaseError(error);
|
||||||
|
if (isFunction(failureCallback)) failureCallback(firebaseError);
|
||||||
|
return Promise.reject(failureCallback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
off(eventName?: string = '', origCB?: () => any) {
|
off(eventType?: string = '', origCB?: () => any) {
|
||||||
const path = this._dbPath();
|
const path = this._dbPath();
|
||||||
const modifiersString = this.query.getModifiersString();
|
const modifiersString = this.query.getModifiersString();
|
||||||
this.log.debug('ref.off(): ', path, modifiersString, eventName);
|
this.log.debug('ref.off(): ', path, modifiersString, eventType);
|
||||||
return this.db.off(path, modifiersString, eventName, origCB);
|
return this.db.off(path, modifiersString, eventType, origCB);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue