package main import ( "log" "git.sr.ht/~michalr/go-satel" ) func FilterByType(ev <-chan satel.Event, allowedTypes []satel.ChangeType) <-chan satel.Event { returnChan := make(chan satel.Event) if len(allowedTypes) == 0 { // no allowed types == all types are allowed go func() { for e := range ev { returnChan <- e } close(returnChan) }() } else { go func() { for e := range ev { for _, allowedType := range allowedTypes { if allowedType == e.Type { returnChan <- e continue } } } close(returnChan) }() } return returnChan } func FilterByIndex(ev <-chan satel.Event, allowedIndexes []int) <-chan satel.Event { returnChan := make(chan satel.Event) if len(allowedIndexes) == 0 { // no allowed indexes == all indexes are allowed go func() { for e := range ev { returnChan <- e } close(returnChan) }() } else { go func() { for e := range ev { for _, allowedIndex := range allowedIndexes { if allowedIndex == e.Index { returnChan <- e continue } } } close(returnChan) }() } return returnChan } func FilterByLastSeen(ev <-chan satel.Event, dataStore *DataStore, logger *log.Logger) <-chan satel.Event { returnChan := make(chan satel.Event) go func() { for e := range ev { lastSeen := dataStore.GetSystemState() val, ok := lastSeen[EventKey{e.Type, e.Index}] if !ok || val.Value != e.Value { returnChan <- e dataStore.SetSystemState(EventKey{e.Type, e.Index}, EventValue{e.Value}) } } close(returnChan) }() return returnChan } func CallWhenClosed(ev <-chan satel.Event, cbk func()) <-chan satel.Event { returnChan := make(chan satel.Event) go func() { for e := range ev { returnChan <- e } cbk() close(returnChan) }() return returnChan }