Skip to content

Commit

Permalink
[database] serverTimeOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Oct 5, 2017
1 parent 870cb4e commit 701491d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/modules/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ 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);
}

/**
*
* @return {number}
*/
getServerTime() {
return new Date().getTime() + this._serverTimeOffset;
return new Date(Date.now() + this._serverTimeOffset);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/database/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 701491d

Please sign in to comment.