Skip to content

Commit 2ff0039

Browse files
committed
Restore some of the Server Timestamp logic
Previous behavior for Server Timestamp was to return UNIX timestamp. This is the correct bahviour still for the client firebase.database library. This change restores this.
1 parent f4ac5ef commit 2ff0039

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/firebase.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var autoId = require('firebase-auto-ids');
77
var Query = require('./query');
88
var Snapshot = require('./snapshot');
99
var Queue = require('./queue').Queue;
10-
var Timestamp = require('./timestamp');
1110
var utils = require('./utils');
1211
var Auth = require('./firebase-auth');
1312
var validate = require('./validators');
@@ -187,11 +186,11 @@ MockFirebase.prototype.update = function (changes, callback) {
187186
self._defer('update', _.toArray(arguments), function () {
188187
if (!err) {
189188
var base = self.getData();
190-
var data = _.isPlainObject(base) ? base : {};
189+
var data = _.isObject(base) ? base : {};
191190
// operate as a multi-set
192191
_.keys(changes).forEach(function (key) {
193192
var val = changes[key];
194-
_.set(data, key.replace(/^\//, '').replace(/\//g, '.'), _.isPlainObject(val) ? utils.updateToRtdbObject(val) : val);
193+
_.set(data, key.replace(/^\//, '').replace(/\//g, '.'), _.isObject(val) ? utils.updateToRtdbObject(val) : val);
195194
});
196195
data = utils.removeEmptyRtdbProperties(data);
197196
self._dataChanged(data);
@@ -445,16 +444,16 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
445444
var data = utils.cleanData(unparsedData);
446445

447446
if (utils.isServerTimestamp(data)) {
448-
data = Timestamp.fromMillis(utils.getServerTime());
447+
data = utils.getServerTime();
449448
}
450449

451450
if (pri !== this.priority) {
452451
this._priChanged(pri);
453452
}
454453
if (!_.isEqual(data, this.data)) {
455454
// _.keys() in Lodash 3 automatically coerces non-object to object
456-
var oldKeys = _.isPlainObject(this.data) ? _.keys(this.data).sort() : [];
457-
var newKeys = _.isPlainObject(data) ? _.keys(data).sort() : [];
455+
var oldKeys = _.isObject(this.data) ? _.keys(this.data).sort() : [];
456+
var newKeys = _.isObject(data) ? _.keys(data).sort() : [];
458457
var keysToRemove = _.difference(oldKeys, newKeys);
459458
var keysToChange = _.difference(newKeys, keysToRemove);
460459
var events = [];
@@ -463,22 +462,22 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
463462
self._removeChild(key, events);
464463
});
465464

466-
if (!_.isPlainObject(data)) {
465+
if (!_.isObject(data)) {
467466
events.push(false);
468467
this.data = data;
469468
}
470469
else {
471470
keysToChange.forEach(function (key) {
472471
var childData = unparsedData[key];
473472
if (utils.isServerTimestamp(childData)) {
474-
childData = Timestamp.fromMillis(utils.getServerTime());
473+
childData = utils.getServerTime();
475474
}
476475
self._updateOrAdd(key, childData, events);
477476
});
478477
}
479478

480479
// update order of my child keys
481-
if (_.isPlainObject(this.data))
480+
if (_.isObject(this.data))
482481
this._resort();
483482

484483
// trigger parent notifications after all children have
@@ -489,7 +488,7 @@ MockFirebase.prototype._dataChanged = function (unparsedData) {
489488

490489
MockFirebase.prototype._priChanged = function (newPriority) {
491490
if (utils.isServerTimestamp(newPriority)) {
492-
newPriority = Timestamp.fromMillis(utils.getServerTime());
491+
newPriority = utils.getServerTime();
493492
}
494493
this.priority = newPriority;
495494
if (this.parent) {
@@ -574,7 +573,7 @@ MockFirebase.prototype._triggerAll = function (events) {
574573
};
575574

576575
MockFirebase.prototype._updateOrAdd = function (key, data, events) {
577-
var exists = _.isPlainObject(this.data) && this.data.hasOwnProperty(key);
576+
var exists = _.isObject(this.data) && this.data.hasOwnProperty(key);
578577
if (!exists) {
579578
return this._addChild(key, data, events);
580579
}
@@ -584,7 +583,7 @@ MockFirebase.prototype._updateOrAdd = function (key, data, events) {
584583
};
585584

586585
MockFirebase.prototype._addChild = function (key, data, events) {
587-
if (!_.isPlainObject(this.data)) {
586+
if (!_.isObject(this.data)) {
588587
this.data = {};
589588
}
590589
this._addKey(key);
@@ -611,7 +610,7 @@ MockFirebase.prototype._removeChild = function (key, events) {
611610

612611
MockFirebase.prototype._updateChild = function (key, data, events) {
613612
var cdata = utils.cleanData(data);
614-
if (_.isPlainObject(this.data) && _.has(this.data, key) && !_.isEqual(this.data[key], cdata)) {
613+
if (_.isObject(this.data) && _.has(this.data, key) && !_.isEqual(this.data[key], cdata)) {
615614
this.data[key] = cdata;
616615
var c = this.child(key);
617616
c._dataChanged(data);
@@ -630,7 +629,7 @@ MockFirebase.prototype._nextErr = function (type) {
630629
};
631630

632631
MockFirebase.prototype._hasChild = function (key) {
633-
return _.isPlainObject(this.data) && _.has(this.data, key);
632+
return _.isObject(this.data) && _.has(this.data, key);
634633
};
635634

636635
MockFirebase.prototype._childData = function (key) {

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ exports.restoreServerClock = function restoreServerTime() {
9696

9797
exports.isServerTimestamp = function isServerTimestamp(data) {
9898
return _.isObject(data) && data['.sv'] === 'timestamp';
99-
};
99+
}
100100

101101
exports.removeEmptyRtdbProperties = function removeEmptyRtdbProperties(obj) {
102102
var t = typeof obj;

test/unit/firebase.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('MockFirebase', function () {
7575
it('parses server timestamps', function () {
7676
ref.set(Firebase.ServerValue.TIMESTAMP);
7777
ref.flush();
78-
expect(ref.getData()).to.have.property('seconds', 0);
78+
expect(ref.getData()).to.equal(new Date().getTime());
7979
});
8080

8181
it('parses server timestamps in child data', function () {
@@ -84,26 +84,26 @@ describe('MockFirebase', function () {
8484
foo: Firebase.ServerValue.TIMESTAMP
8585
});
8686
ref.flush();
87-
expect(child.getData()).to.have.property('seconds', 0);
87+
expect(child.getData()).to.equal(new Date().getTime());
8888
});
8989

9090
it('parses server timestamps in priorities', function () {
9191
ref.setPriority(Firebase.ServerValue.TIMESTAMP);
9292
ref.flush();
93-
expect(ref.priority).to.have.property('seconds', 0);
93+
expect(ref).to.have.property('priority', new Date().getTime());
9494
});
9595

9696
describe('Firebase#setClock', function () {
9797

9898
afterEach(Firebase.restoreClock);
9999

100100
it('sets a timestamp factory function', function () {
101-
var customClock = sinon.stub().returns(10000);
101+
var customClock = sinon.stub().returns(10);
102102
Firebase.setClock(customClock);
103103
ref.set(Firebase.ServerValue.TIMESTAMP);
104104
ref.flush();
105105
expect(customClock.callCount).to.equal(1);
106-
expect(ref.getData()).to.have.property('seconds', 10);
106+
expect(ref.getData()).to.equal(10);
107107
});
108108

109109
});
@@ -116,7 +116,7 @@ describe('MockFirebase', function () {
116116
ref.set(Firebase.ServerValue.TIMESTAMP);
117117
ref.flush();
118118
expect(spy.called).to.equal(false);
119-
expect(ref.getData()).to.have.property('seconds', 0);
119+
expect(ref.getData()).to.equal(new Date().getTime());
120120
});
121121

122122
});

0 commit comments

Comments
 (0)