2018-01-05 18:23:38 +00:00
|
|
|
/**
|
2017-03-02 11:40:08 +00:00
|
|
|
* @flow
|
|
|
|
* Database representation wrapper
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2017-03-24 22:53:56 +00:00
|
|
|
import Reference from './reference';
|
|
|
|
import TransactionHandler from './transaction';
|
2018-01-05 17:20:02 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
import type App from '../core/firebase-app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2017-12-22 15:57:33 +00:00
|
|
|
const NATIVE_EVENTS = [
|
|
|
|
'database_transaction_event',
|
|
|
|
// 'database_server_offset', // TODO
|
|
|
|
];
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseDatabase';
|
|
|
|
export const NAMESPACE = 'database';
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* @class Database
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class Database extends ModuleBase {
|
2017-12-04 12:07:41 +00:00
|
|
|
_offsetRef: Reference;
|
|
|
|
_serverTimeOffset: number;
|
|
|
|
_transactionHandler: TransactionHandler;
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App, options: Object = {}) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: true,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-07-30 06:34:41 +00:00
|
|
|
this._transactionHandler = new TransactionHandler(this);
|
2017-08-02 09:38:30 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
if (options.persistence) {
|
|
|
|
getNativeModule(this).setPersistence(options.persistence);
|
2017-08-02 09:38:30 +00:00
|
|
|
}
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2017-10-05 11:45:54 +00:00
|
|
|
// server time listener
|
|
|
|
// setTimeout used to avoid setPersistence race conditions
|
2017-10-05 11:53:47 +00:00
|
|
|
// todo move this and persistence to native side, create a db configure() method natively perhaps?
|
|
|
|
// todo and then native can call setPersistence and then emit offset events
|
2017-10-05 11:45:54 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
this._serverTimeOffset = 0;
|
|
|
|
this._offsetRef = this.ref('.info/serverTimeOffset');
|
2018-01-25 18:25:39 +00:00
|
|
|
this._offsetRef.on('value', snapshot => {
|
2017-10-05 11:45:54 +00:00
|
|
|
this._serverTimeOffset = snapshot.val() || this._serverTimeOffset;
|
|
|
|
});
|
|
|
|
}, 1);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 09:38:30 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {number}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getServerTime(): number {
|
2017-10-05 11:45:54 +00:00
|
|
|
return new Date(Date.now() + this._serverTimeOffset);
|
2017-08-02 09:38:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
goOnline(): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).goOnline();
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
goOffline(): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).goOffline();
|
2017-03-07 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 11:40:08 +00:00
|
|
|
/**
|
2017-07-30 06:34:41 +00:00
|
|
|
* Returns a new firebase reference instance
|
|
|
|
* @param path
|
|
|
|
* @returns {Reference}
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
ref(path: string): Reference {
|
2017-07-30 06:34:41 +00:00
|
|
|
return new Reference(this, path);
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 20:15:02 +00:00
|
|
|
|
|
|
|
export const statics = {
|
2018-01-25 18:25:39 +00:00
|
|
|
ServerValue: NativeModules.RNFirebaseDatabase
|
|
|
|
? {
|
|
|
|
TIMESTAMP: NativeModules.RNFirebaseDatabase.serverValueTimestamp || {
|
|
|
|
'.sv': 'timestamp',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {},
|
2017-12-04 12:07:41 +00:00
|
|
|
enableLogging(enabled: boolean) {
|
2018-01-03 20:00:38 +00:00
|
|
|
if (NativeModules[MODULE_NAME]) {
|
|
|
|
NativeModules[MODULE_NAME].enableLogging(enabled);
|
2017-11-08 15:58:44 +00:00
|
|
|
}
|
|
|
|
},
|
2017-03-22 20:15:02 +00:00
|
|
|
};
|