forked from livekit/livekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timed_version.go
60 lines (49 loc) · 991 Bytes
/
timed_version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package utils
import (
"sync"
"time"
"github.com/abdulhaseeb08/protocol/livekit"
)
type TimedVersion struct {
lock sync.RWMutex
at time.Time
ticks int32
}
func NewTimedVersion(at time.Time, ticks int32) *TimedVersion {
return &TimedVersion{
at: at,
ticks: ticks,
}
}
func NewTimedVersionFromProto(proto *livekit.TimedVersion) *TimedVersion {
return &TimedVersion{
at: time.UnixMicro(proto.UnixMicro),
ticks: proto.Ticks,
}
}
func (t *TimedVersion) Update(at time.Time) {
t.lock.Lock()
if at.After(t.at) {
t.at = at
t.ticks = 0
} else {
t.ticks++
}
t.lock.Unlock()
}
func (t *TimedVersion) After(other *TimedVersion) bool {
t.lock.RLock()
defer t.lock.RUnlock()
if t.at.Equal(other.at) {
return t.ticks > other.ticks
}
return t.at.After(other.at)
}
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
t.lock.RLock()
defer t.lock.RUnlock()
return &livekit.TimedVersion{
UnixMicro: t.at.UnixMicro(),
Ticks: t.ticks,
}
}