summaryrefslogtreecommitdiffstats
path: root/updates.go
blob: 33f12a97179b0cc3cf002c616cd8a45477eac7c1 (plain)
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
package main

import (
	"time"
)

const (
	addAction      = 0
	removeAction   = 1
	moveUpAction   = 2
	moveDownAction = 3
)
type Update struct {
	Song *Song
	Action uint
	Timestamp int64
	Next *Update
}
var headUpdates map[int]*Update
var tailUpdates map[int]*Update

func init() {
	headUpdates = make(map[int]*Update)
	tailUpdates = make(map[int]*Update)
}

func addUpdate(pid int, action uint, song *Song) {
	update := new(Update)
	update.Song = song
	update.Action = action
	update.Timestamp = time.Nanoseconds()
	pup, ok := tailUpdates[pid]
	if ok {
		pup.Next = update
	} else {
		headUpdates[pid] = update
	}
	tailUpdates[pid] = update
}

func getUpdates(pid int, timestamp int64) *Update {
	pup, ok := headUpdates[pid]
	if !ok {
		return nil
	}
	for pup != nil {
		if pup.Timestamp > timestamp {
			return pup
		}
		pup = pup.Next
	}
	return nil
}