[database] serverTimeOffset
This commit is contained in:
parent
870cb4ebe1
commit
701491df18
@ -23,8 +23,15 @@ export default class Database extends ModuleBase {
|
||||
this._native.setPersistence(this._options.persistence);
|
||||
}
|
||||
|
||||
// todo serverTimeOffset event/listener - make ref natively and switch to events
|
||||
this._serverTimeOffset = 0; // TODO ----^
|
||||
// server time listener
|
||||
// setTimeout used to avoid setPersistence race conditions
|
||||
setTimeout(() => {
|
||||
this._serverTimeOffset = 0;
|
||||
this._offsetRef = this.ref('.info/serverTimeOffset');
|
||||
this._offsetRef.on('value', (snapshot) => {
|
||||
this._serverTimeOffset = snapshot.val() || this._serverTimeOffset;
|
||||
});
|
||||
}, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -32,7 +39,7 @@ export default class Database extends ModuleBase {
|
||||
* @return {number}
|
||||
*/
|
||||
getServerTime() {
|
||||
return new Date().getTime() + this._serverTimeOffset;
|
||||
return new Date(Date.now() + this._serverTimeOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -250,10 +250,10 @@ export default class Reference extends ReferenceBase {
|
||||
*/
|
||||
push(value: any, onComplete?: Function): Reference | Promise {
|
||||
if (value === null || value === undefined) {
|
||||
return new Reference(this._database, `${this.path}/${generatePushID(this._database.serverTimeOffset)}`);
|
||||
return new Reference(this._database, `${this.path}/${generatePushID(this._database._serverTimeOffset)}`);
|
||||
}
|
||||
|
||||
const newRef = new Reference(this._database, `${this.path}/${generatePushID(this._database.serverTimeOffset)}`);
|
||||
const newRef = new Reference(this._database, `${this.path}/${generatePushID(this._database._serverTimeOffset)}`);
|
||||
const promise = newRef.set(value);
|
||||
|
||||
// if callback provided then internally call the set promise with value
|
||||
|
Loading…
x
Reference in New Issue
Block a user