Skip to content

Commit e77ea3e

Browse files
committed
Move server clock handling out to utils
1 parent c0aee17 commit e77ea3e

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/firebase.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,12 @@ MockFirebase.ServerValue = {
4444
}
4545
};
4646

47-
var getServerTime, defaultClock;
48-
getServerTime = defaultClock = function () {
49-
return new Date().getTime();
50-
};
51-
5247
MockFirebase.setClock = function (fn) {
53-
getServerTime = fn;
48+
utils.setServerClock(fn);
5449
};
5550

5651
MockFirebase.restoreClock = function () {
57-
getServerTime = defaultClock;
52+
utils.restoreServerClock();
5853
};
5954

6055
MockFirebase.autoId = function () {
@@ -449,7 +444,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
449444
var data = utils.cleanData(unparsedData);
450445

451446
if (utils.isServerTimestamp(data)) {
452-
data = getServerTime();
447+
data = utils.getServerTime();
453448
}
454449

455450
if (pri !== this.priority) {
@@ -475,7 +470,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
475470
keysToChange.forEach(function (key) {
476471
var childData = unparsedData[key];
477472
if (utils.isServerTimestamp(childData)) {
478-
childData = getServerTime();
473+
childData = utils.getServerTime();
479474
}
480475
self._updateOrAdd(key, childData, events);
481476
});
@@ -493,7 +488,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
493488

494489
MockFirebase.prototype._priChanged = function (newPriority) {
495490
if (utils.isServerTimestamp(newPriority)) {
496-
newPriority = getServerTime();
491+
newPriority = utils.getServerTime();
497492
}
498493
this.priority = newPriority;
499494
if (this.parent) {

src/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ exports.priorityComparator = function priorityComparator(a, b) {
7575
return 0;
7676
};
7777

78+
var serverClock, defaultClock;
79+
80+
serverClock = defaultClock = function () {
81+
return new Date().getTime();
82+
};
83+
84+
exports.getServerTime = function getServerTime() {
85+
return serverClock();
86+
};
87+
88+
exports.setServerClock = function setServerTime(fn) {
89+
serverClock = fn;
90+
};
91+
92+
exports.restoreServerClock = function restoreServerTime() {
93+
serverClock = defaultClock;
94+
};
95+
7896
exports.isServerTimestamp = function isServerTimestamp(data) {
7997
return _.isObject(data) && data['.sv'] === 'timestamp';
8098
};

0 commit comments

Comments
 (0)