[js][statics] implemented staticsOrInstance helper - fixes #12

This commit is contained in:
Salakar 2017-03-22 20:15:02 +00:00
parent 2f12569c60
commit d9023f55b8
3 changed files with 59 additions and 39 deletions

View File

@ -9,8 +9,8 @@ import { isObject } from './utils';
// modules
import Auth from './modules/auth';
import Storage from './modules/storage';
import Database from './modules/database';
import Storage, { statics as StorageStatics } from './modules/storage';
import Database, { statics as DatabaseStatics } from './modules/database';
import Messaging from './modules/messaging';
import Analytics from './modules/analytics';
import Crash from './modules/crash';
@ -23,6 +23,17 @@ const FirebaseModuleEvt = new NativeEventEmitter(FirebaseModule);
* @class Firebase
*/
export default class Firebase {
_log: ?Object;
_auth: ?Object;
_store: ?Object;
_storage: ?Object;
_database: ?Object;
_presence: ?Object;
_analytics: ?Object;
_constants: ?Object;
_messaging: ?Object;
_remoteConfig: ?Object;
_crash: ?Object;
/**
*
@ -43,19 +54,34 @@ export default class Firebase {
if (this.options.errorOnMissingPlayServices && !this.googleApiAvailability.isAvailable) {
throw new Error(`Google Play Services is required to run this application but no valid installation was found (Code ${this.googleApiAvailability.status}).`);
}
this.storage = this._staticsOrInstance('storage', StorageStatics, Storage);
this.database = this._staticsOrInstance('database', DatabaseStatics, Database);
}
_db: ?Object;
_log: ?Object;
_auth: ?Object;
_store: ?Object;
_storage: ?Object;
_presence: ?Object;
_analytics: ?Object;
_constants: ?Object;
_messaging: ?Object;
_remoteConfig: ?Object;
_crash: ?Object;
/**
*
* @param name
* @param statics
* @param InstanceClass
* @returns {function()}
* @private
*/
_staticsOrInstance(name, statics, InstanceClass) {
const getInstance = () => {
const internalPropName = `_${name}`;
if (!this[internalPropName]) {
this[internalPropName] = new InstanceClass(this);
}
return this[internalPropName];
};
Object.assign(getInstance, statics || {});
return getInstance;
}
/**
* Support web version of initApp.
@ -91,13 +117,6 @@ export default class Firebase {
return this._auth;
}
database() {
if (!this._db) {
this._db = new Database(this);
}
return this._db;
}
analytics() {
if (!this._analytics) {
this._analytics = new Analytics(this);
@ -105,14 +124,6 @@ export default class Firebase {
return this._analytics;
}
// storage
storage() {
if (!this._storage) {
this._storage = new Storage(this);
}
return this._storage;
}
messaging() {
if (!this._messaging) {
this._messaging = new Messaging(this);

View File

@ -47,17 +47,6 @@ export default class Database extends Base {
this.log.debug('Created new Database instance', this.options);
}
/**
* https://firebase.google.com/docs/reference/js/firebase.database.ServerValue
* @returns {{TIMESTAMP: (*|{[.sv]: string})}}
* @constructor
*/
get ServerValue(): Object {
return {
TIMESTAMP: FirebaseDatabase.serverValueTimestamp || { '.sv': 'timestamp' },
};
}
/**
* Returns a new firebase reference instance
* @param path
@ -252,3 +241,10 @@ export default class Database extends Base {
if (this.errorSubscriptions[handle]) this.errorSubscriptions[handle].forEach(listener => listener(firebaseError));
}
}
export const statics = {
ServerValue: {
TIMESTAMP: FirebaseDatabase.serverValueTimestamp || { '.sv': 'timestamp' },
},
};

View File

@ -108,3 +108,16 @@ export default class Storage extends Base {
}
}
export const statics = {
TaskEvent: {
STATE_CHANGED: 'state_changed',
},
TaskState: {
RUNNING: 'RUNNING',
PAUSED: 'PAUSED',
SUCCESS: 'SUCCESS',
CANCELED: 'CANCELED',
ERROR: 'ERROR',
},
};