Skip to content

Commit b0eedfa

Browse files
committed
Update firestackModule setAt,getAt,updateAt,removeAt, added Log shim
1 parent df52637 commit b0eedfa

File tree

4 files changed

+108
-18
lines changed

4 files changed

+108
-18
lines changed

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.util.Log;
55
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
67

78
import com.facebook.react.bridge.Arguments;
89
import com.facebook.react.bridge.ReactApplicationContext;
@@ -38,6 +39,7 @@ class FirestackModule extends ReactContextBaseJavaModule {
3839
public FirestackModule(ReactApplicationContext reactContext) {
3940
super(reactContext);
4041
this.context = reactContext;
42+
Log.d(TAG, "New FirestackModule instance");
4143
}
4244

4345
@Override
@@ -46,38 +48,53 @@ public String getName() {
4648
}
4749

4850
@ReactMethod
49-
public void configureWithOptions(ReadableMap params, final Callback onComplete) {
50-
System.out.println("Calling configureWithOptions");
51-
Log.i(TAG, "configureWithOptions");
51+
public void configureWithOptions(ReadableMap params, @Nullable final Callback onComplete) {
52+
Log.d(TAG, "configureWithOptions");
53+
5254
ReactContext mCtx = getReactApplicationContext();
5355
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
5456

57+
if (params.hasKey("applicationId")) {
58+
final String applicationId = params.getString("applicationId");
59+
Log.d(TAG, "Setting applicationId from params " + applicationId);
60+
builder.setApplicationId(applicationId);
61+
}
5562
if (params.hasKey("apiKey")) {
56-
builder.setApiKey(params.getString("apiKey"));
63+
final String apiKey = params.getString("apiKey");
64+
Log.d(TAG, "Setting API key from params " + apiKey);
65+
builder.setApiKey(apiKey);
5766
}
5867
if (params.hasKey("gcmSenderID")) {
59-
builder.setGcmSenderId(params.getString("gcmSenderID"));
68+
final String gcmSenderID = params.getString("gcmSenderID");
69+
Log.d(TAG, "Setting gcmSenderID from params" + gcmSenderID );
70+
builder.setGcmSenderId(gcmSenderID);
6071
}
6172
if (params.hasKey("storageBucket")) {
62-
builder.setStorageBucket(params.getString("storageBucket"));
73+
final String storageBucket = params.getString("storageBucket");
74+
Log.d(TAG, "Setting storageBucket from params" + storageBucket);
75+
builder.setStorageBucket(storageBucket);
6376
}
6477
if (params.hasKey("databaseURL")) {
65-
builder.setDatabaseUrl(params.getString("databaseURL"));
78+
final String databaseURL = params.getString("databaseURL");
79+
Log.d(TAG, "Setting databaseURL from params" + databaseURL);
80+
builder.setDatabaseUrl(databaseURL);
6681
}
6782
if (params.hasKey("clientID")) {
68-
builder.setApplicationId(params.getString("clientID"));
83+
final String clientID = params.getString("clientID");
84+
Log.d(TAG, "Setting clientID from params" + clientID);
85+
builder.setApplicationId(clientID);
6986
}
7087

7188
try {
7289
Log.i(TAG, "Configuring");
7390
FirebaseApp app = FirebaseApp.initializeApp(mCtx, builder.build());
7491
Log.i(TAG, "Configured");
75-
onComplete.invoke();
92+
onComplete.invoke(app);
7693
}
7794
catch (Exception ex){
7895
Log.e(TAG, "ERROR configureWithOptions");
7996
Log.e(TAG, ex.getMessage());
80-
onComplete.invoke();
97+
onComplete.invoke(ex.getMessage());
8198
}
8299
}
83100

lib/debug_shim.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// document hack
2+
if (!global.document) {
3+
global.document = { documentElement: { style: { WebkitAppearance: true } } }
4+
}
5+
if(!window.localStorage) window.localStorage = {};
6+
7+
let debug = () => {};
8+
9+
export class Log {
10+
11+
constructor(namespace) {
12+
this._namespace = namespace || 'firestack';
13+
this.l = null;
14+
this.loggers = {};
15+
}
16+
17+
enable(booleanOrStringDebug) {
18+
if (booleanOrStringDebug) {
19+
window.localStorage.debug =
20+
typeof booleanOrStringDebug === 'string' ? booleanOrStringDebug : '*';
21+
}
22+
}
23+
24+
info(...args) {
25+
this._logger('info')(...args);
26+
}
27+
28+
error(...args) {
29+
this._logger('error')(...args);
30+
}
31+
32+
_logger(level) {
33+
if (!this.loggers[level]) {
34+
this.loggers[level] = this._debug()(`${this._namespace}:${level}`)
35+
}
36+
return this.loggers[level];
37+
}
38+
39+
_debug() {
40+
if (!this.l) {
41+
this.l = debug = require('debug');
42+
}
43+
return this.l;
44+
}
45+
}
46+
47+
export default Log;

lib/firestack.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @providesModule Firestack
33
* @flow
44
*/
5+
import Log from './debug_shim'
6+
57
const firebase = require('firebase');
68

79
const app = require('firebase/app');
@@ -16,20 +18,26 @@ const FirebaseHelper = NativeModules.Firestack;
1618
import promisify from './promisify'
1719
import RemoteConfig from './remoteConfig'
1820

21+
let log;
1922
export class Firestack {
2023

2124
constructor(options) {
2225
this.options = options || {};
26+
this._debug = options.debug || false;
27+
log = this._log = new Log('firestack');
28+
29+
log.enable(this._debug);
30+
log.info('Creating new instance');
2331

2432
this._remoteConfig = options.remoteConfig || {};
2533
delete options.remoteConfig;
2634

2735
this.configured = this.options.configure || false;
28-
this._debug = options.debug || false;
2936
this.auth = null;
3037

3138
this.eventHandlers = {};
3239

40+
log.info('Calling configure with options', options);
3341
this.configure(options);
3442
}
3543

@@ -38,17 +46,19 @@ export class Firestack {
3846
const firestackOptions = Object.assign({}, this.options, opts);
3947
try {
4048
this.appInstance = app.initializeApp(firestackOptions);
49+
log.info('JS app initialized');
4150
} catch (e) {
42-
console.log('Error occurred while calling configure', e);
51+
log.error('JS error while calling initializeApp', e);
4352
}
4453

54+
log.info('Calling native configureWithOptions')
4555
return promisify('configureWithOptions')(firestackOptions)
4656
.then((...args) => {
47-
console.log('Native configureWithOptions success', args);
57+
log.info('Native configureWithOptions success', args);
4858
this.configured = true;
4959
return args;
5060
}).catch((err) => {
51-
console.log('Native error occurred while calling configure', e);
61+
log.error('Native error occurred while calling configure', e);
5262
})
5363
}
5464

lib/firestackModule.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import invariant from 'invariant'
55
const createTypes = (prefix) => {
66
const c = (str) => `${prefix.toUpperCase()}_${str.toUpperCase()}`
77
return {
8+
ACTION_CALL: c('call_function'),
9+
ACTION_SUCCESS: c('call_function_success'),
10+
ACTION_FAIL: c('call_function_failure'),
11+
812
ACTION_LISTEN: c('listen'),
913
ACTION_UNLISTEN: c('unlisten'),
1014
ACTION_REMOVE: c('remove'),
@@ -159,9 +163,10 @@ export class FirestackModule {
159163
const toObject = this._toObject;
160164

161165
return new Promise((resolve, reject) => {
162-
ref.set(value, error => {
166+
ref.set(value, (error, snapshot) => {
163167
this._handleUpdate(T.ACTION_SET, null, (state) => {
164168
if (cb) {
169+
console.log('error ->', error, snapshot);
165170
cb(toObject(snapshot, state));
166171
}
167172
return error ? reject(error) : resolve(value)
@@ -176,7 +181,7 @@ export class FirestackModule {
176181
const toObject = this._toObject;
177182

178183
return new Promise((resolve, reject) => {
179-
ref.update(value, error => {
184+
ref.update(value, (error, snapshot) => {
180185
this._handleUpdate(T.ACTION_UPDATE, null, (state) => {
181186
if (cb) {
182187
cb(toObject(snapshot, state));
@@ -193,7 +198,7 @@ export class FirestackModule {
193198
const toObject = this._toObject;
194199

195200
return new Promise((resolve, reject) => {
196-
ref.remove(value, error => {
201+
ref.remove(value, (error, snapshot) => {
197202
this._handleUpdate(T.ACTION_SET, null, (state) => {
198203
if (cb) {
199204
cb(toObject(snapshot, state));
@@ -206,13 +211,24 @@ export class FirestackModule {
206211

207212
// hackish, for now
208213
get actions() {
214+
const T = this._types;
209215
return [
210216
'listen', 'unlisten',
211217
'getAt', 'setAt', 'updateAt', 'removeAt'
212218
].reduce((sum, name) => {
213219
return {
214220
...sum,
215-
[name]: this[name].bind(this)
221+
[name]: (...args) => (dispatch, getState) => {
222+
const fn = this[name].bind(this);
223+
fn.apply(this, args)
224+
.then((resp) => {
225+
dispatch({type: T.ACTION_SUCCESS, payload: resp});
226+
})
227+
.catch((err) => {
228+
dispatch({type: T.ACTION_FAIL, payload: err});
229+
});
230+
return {type: T.ACTION_CALL, payload: name}
231+
}
216232
}
217233
}, {})
218234
}

0 commit comments

Comments
 (0)