@live-change/dao 0.8.59 → 0.8.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/TimeSynchronization.js +19 -6
- package/package.json +2 -2
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import ObservableValue from './ObservableValue.js'
|
|
2
|
+
|
|
1
3
|
class TimeSynchronization {
|
|
2
4
|
constructor(settings) {
|
|
3
5
|
this.connection = null
|
|
@@ -24,9 +26,11 @@ class TimeSynchronization {
|
|
|
24
26
|
*/
|
|
25
27
|
this.maximalDiff = +Infinity // Difference calculated with zero send time assumption.
|
|
26
28
|
|
|
27
|
-
|
|
28
29
|
this.timeDiff = 0 // Calculated diff
|
|
29
30
|
|
|
31
|
+
this.timeDiffObservable = new ObservableValue(0)
|
|
32
|
+
this.synchronizedObservable = new ObservableValue(false)
|
|
33
|
+
|
|
30
34
|
this.pongCount = 0
|
|
31
35
|
|
|
32
36
|
settings = settings || {}
|
|
@@ -79,10 +83,16 @@ class TimeSynchronization {
|
|
|
79
83
|
if(this.timeDiff > this.maximalDiff) this.timeDiff = this.maximalDiff
|
|
80
84
|
}
|
|
81
85
|
|
|
86
|
+
this.timeDiffObservable.set(this.timeDiff)
|
|
87
|
+
|
|
82
88
|
//console.error("PING",ping,'PING DIFF', pingDiff,"ZERO REPLY",zeroReply,"ZERO SEND",zeroSend,"REAL DIFF",this.timeDiff)
|
|
83
89
|
|
|
84
90
|
this.pongCount++
|
|
85
|
-
if(this.pongCount
|
|
91
|
+
if(this.pongCount === this.minPongCount) {
|
|
92
|
+
this.synchronized = true
|
|
93
|
+
this.synchronizedObservable.set(true)
|
|
94
|
+
this.promiseCallback(this.timeDiff)
|
|
95
|
+
}
|
|
86
96
|
|
|
87
97
|
if(this.phases.length > this.nextPhaseId) {
|
|
88
98
|
let phase = this.phases[this.nextPhaseId]
|
|
@@ -104,13 +114,16 @@ class TimeSynchronization {
|
|
|
104
114
|
setTimeout(() => this.run(), interval)
|
|
105
115
|
}
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
getTimeDiff() {
|
|
108
118
|
if(this.pongCount < this.minPongCount) throw new Error("Time not synchronized")
|
|
109
|
-
return
|
|
119
|
+
return this.timeDiff
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
serverToLocal(ts) {
|
|
123
|
+
return ts - this.getTimeDiff()
|
|
110
124
|
}
|
|
111
125
|
localToServer(ts) {
|
|
112
|
-
|
|
113
|
-
return ts + this.timeDiff
|
|
126
|
+
return ts + this.getTimeDiff()
|
|
114
127
|
}
|
|
115
128
|
|
|
116
129
|
synchronizedPromise() {
|
package/package.json
CHANGED