package producer



import (
	"container/list"
)

// 블럭되지 않는 큐체널
func NewTempetureQueue() (chan<- TempetureInfo, <-chan TempetureInfo) {
	send := make(chan TempetureInfo, 1)
	receive := make(chan TempetureInfo, 1)
	go manageTempetureQueue(send, receive)
	return send, receive
}

func manageTempetureQueue(send <-chan TempetureInfo, receive chan<- TempetureInfo) {
	queue := list.New()
	defer close(receive)
	for {
		if front := queue.Front(); front == nil {
			if value, ok := <-send; ok {
				queue.PushBack(value)
			} else {
				break
			}
		} else {
			select {
			case receive <- front.Value.(TempetureInfo):
				queue.Remove(front)
			case value, ok := <-send:
				if ok {
					queue.PushBack(value)
				}
			}
		}
	}
}
