diff --git a/pkg/utils/timed_version.go b/pkg/utils/timed_version.go index f18316058..72e382442 100644 --- a/pkg/utils/timed_version.go +++ b/pkg/utils/timed_version.go @@ -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() diff --git a/pkg/utils/timed_version_test.go b/pkg/utils/timed_version_test.go index 42857939f..c11a2fa79 100644 --- a/pkg/utils/timed_version_test.go +++ b/pkg/utils/timed_version_test.go @@ -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)) }