diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-04 10:34:37 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-04 10:34:37 -0500 |
| commit | 34831bbc59b0cb62ec837e1791457cf3bd4574ce (patch) | |
| tree | 0d8219e60ae50fbc3971a598098f74a837dd965c /record.go | |
| parent | ac8fdf42da0c607686e76c68840f38cfd21f83a5 (diff) | |
| download | soen422-34831bbc59b0cb62ec837e1791457cf3bd4574ce.zip | |
server: humidity record for each room
Diffstat (limited to 'record.go')
| -rw-r--r-- | record.go | 60 |
1 files changed, 0 insertions, 60 deletions
diff --git a/record.go b/record.go deleted file mode 100644 index 9051146..0000000 --- a/record.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "time" -) - -type Record[T any] struct { - put chan<- T - get chan<- chan T - getRecent chan<- chan T -} - -type entry[T any] struct { - t time.Time - v T -} - -func newRecord[T any]() Record[T] { - put := make(chan T) - get := make(chan chan T) - getRecent := make(chan chan T) - - go func() { - var entries []entry[T] - - for { - select { - case v, ok := <-put: - if !ok { - return - } - entries = append(entries, entry[T]{time.Now(), v}) - case c, ok := <-get: - if !ok { - return - } - for _, e := range entries { - c <- e.v - } - close(c) - case c, ok := <-getRecent: - if !ok { - return - } - if len(entries) > 0 { - c <- entries[len(entries)-1].v - } - close(c) - } - } - }() - - return Record[T]{put, get, getRecent} -} - -func (l Record[T]) Close() { - close(l.put) - close(l.get) - close(l.getRecent) -} |