add compare function to timedversion (#1422)

* add compare function to timedversion

* cleanup
This commit is contained in:
Paul Wells
2023-02-14 21:05:55 -08:00
committed by GitHub
parent c133b9f2e3
commit 24d8ad0da9
2 changed files with 29 additions and 0 deletions
+25
View File
@@ -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()