Skip to content

Commit 7b5cbcd

Browse files
author
Morten Henriksen
committed
Add servertime
Meteor currently added localforage twice.. #176
1 parent a2e7393 commit 7b5cbcd

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

lib/client/ground.db.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Ground = {};
2323

2424
import localforage from 'localforage';
2525
import { ProgressCount } from './pending.jobs';
26+
import ServerTime from './servertime';
2627

2728
function strId(id) {
2829
if (id && id._str) {

lib/client/servertime.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
////////////////////////// GET SERVER TIME DIFFERENCE //////////////////////////
2+
import localforage from 'localforage';
3+
4+
ServerTime = {};
5+
6+
ServerTime._serverTimeDiff = 0; // Time difference in ms
7+
8+
ServerTime.now = function() {
9+
return Date.now() + ServerTime._serverTimeDiff;
10+
};
11+
12+
// At server startup we figure out the time difference between server and
13+
// client time - this includes lag and timezone
14+
15+
// Use the ground store to handle storage for us
16+
var _storage = localforage.createInstance({
17+
name: 'ServerTime',
18+
version: 1.0,
19+
});
20+
21+
// Initialize the ServerTime._serverTimeDiff
22+
_storage.getItem('diff', function(err, time) {
23+
if (!err) {
24+
25+
// Set the time
26+
ServerTime._serverTimeDiff = time || 0;
27+
}
28+
29+
});
30+
31+
// Call the server method an get server time
32+
// XXX: Use http call instead creating less overhead
33+
Meteor.call('getServerTime', function(error, result) {
34+
if (!error) {
35+
// Update our server time diff
36+
ServerTime._serverTimeDiff = result - Date.now();// - lag or/and timezone
37+
// Update the localstorage
38+
_storage.setItem('diff', ServerTime._serverTimeDiff, function(/* err, result */) {
39+
// XXX:
40+
});
41+
}
42+
}); // EO Server call
43+
44+
export default ServerTime;

lib/server/ground.db.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ServerTime from './servertime';
2+
13
Ground = {};
24

35
Ground.Collection = class GroundCollection {

lib/server/servertime.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
////////////////////////// GET SERVER TIME DIFFERENCE //////////////////////////
2+
3+
ServerTime = {};
4+
5+
// XXX: TODO use a http rest point instead - creates less overhead
6+
Meteor.methods({
7+
'getServerTime': function() {
8+
return Date.now();
9+
}
10+
});
11+
12+
// Unify client / server api
13+
ServerTime.now = function() {
14+
return Date.now();
15+
};
16+
17+
export default ServerTime;

package.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Package.onUse(function (api) {
1616
api.use([
1717
'underscore',
1818
'ejson',
19-
19+
// 'ground:[email protected]', // now embedded due to #176
2020
2121
], ['client', 'server']);
2222

0 commit comments

Comments
 (0)