package producer

import (
	"container/list"
)

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

func manageFanspeedQueue(send <-chan FanspeedInfo, receive chan<- FanspeedInfo) {
	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.(FanspeedInfo):
				queue.Remove(front)
			case value, ok := <-send:
				if ok {
					queue.PushBack(value)
				}
			}
		}
	}
}
