mirror of
https://github.com/livekit/livekit.git
synced 2026-03-31 00:15:38 +00:00
add compare function to timedversion (#1422)
* add compare function to timedversion * cleanup
This commit is contained in:
@@ -56,6 +56,12 @@ func NewTimedVersionFromProto(ptv *livekit.TimedVersion) *TimedVersion {
|
||||
}
|
||||
}
|
||||
|
||||
func NewTimedVersionFromTime(t time.Time) *TimedVersion {
|
||||
return &TimedVersion{
|
||||
ts: t.UnixMicro(),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TimedVersion) Update(other *TimedVersion) {
|
||||
t.lock.Lock()
|
||||
if other.After(t) {
|
||||
@@ -76,6 +82,25 @@ func (t *TimedVersion) After(other *TimedVersion) bool {
|
||||
return t.ts > other.ts
|
||||
}
|
||||
|
||||
func (t *TimedVersion) Compare(other *TimedVersion) int {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
if t.ts == other.ts {
|
||||
if t.ticks == other.ticks {
|
||||
return 0
|
||||
}
|
||||
if t.ticks > other.ticks {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
if t.ts > other.ts {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
@@ -36,4 +36,8 @@ func TestTimedVersion(t *testing.T) {
|
||||
assert.True(t, tv2.After(tv1))
|
||||
// tv3 and tv2 are equivalent after update
|
||||
assert.False(t, tv3.After(tv2))
|
||||
|
||||
assert.Equal(t, 0, tv1.Compare(tv1))
|
||||
assert.Equal(t, -1, tv1.Compare(tv2))
|
||||
assert.Equal(t, 1, tv2.Compare(tv1))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user