From 3689c51b74b63a2d876109feef471e76a42bce19 Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 26 Apr 2017 18:56:07 +0900 Subject: [PATCH] Type --- queue.go | 34 +++++----------------------------- queue_test.go | 5 ++--- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/queue.go b/queue.go index 7f85b48..59245f6 100644 --- a/queue.go +++ b/queue.go @@ -3,17 +3,9 @@ package queue import ( "container/heap" "loafle.com/overflow/agent_api/observer" - "log" "time" ) -type EventType int - -const ( - DATA_TYPE EventType = 0 - EVENT_TYPE EventType = 1 -) - type Item struct { Value interface{} Priority int @@ -21,7 +13,7 @@ type Item struct { type LoafleQueue struct { items []*Item - queueType string + queueType observer.KEY interval time.Duration eventChanel chan interface{} @@ -78,41 +70,25 @@ func (lq *LoafleQueue) notifyEventHandler(c chan interface{}) { } } -func StringType(tp EventType) string { - var tempType string - - if tp == DATA_TYPE { - tempType = "DATA_TYPE" - } else if tp == EVENT_TYPE { - tempType = "EVENT_TYPE" - } else { - log.Fatal("Event Type Error") - } - - return tempType -} - func (lq *LoafleQueue) Close() { if lq.eventChanel != nil { - observer.Remove(lq.queueType, lq.eventChanel) + observer.Remove(lq.queueType.String(), lq.eventChanel) } } -func NewQueue(eventType EventType, interval time.Duration) *LoafleQueue { +func NewQueue(eventType observer.KEY, interval time.Duration) *LoafleQueue { items := make([]*Item, 0) event := make(chan interface{}, 0) - tempType := StringType(eventType) - lq := &LoafleQueue{ items: items, - queueType: tempType, + queueType: eventType, interval: interval, eventChanel: event, } heap.Init(lq) - observer.Add(lq.queueType, lq.eventChanel) + observer.Add(eventType.String(), lq.eventChanel) go lq.notifyEventHandler(event) return lq diff --git a/queue_test.go b/queue_test.go index 422afe5..1a5c7c1 100644 --- a/queue_test.go +++ b/queue_test.go @@ -7,10 +7,9 @@ import ( ) func TestNewQueue(t *testing.T) { - lq := NewQueue(EVENT_TYPE, 5) - typeEvent := StringType(EVENT_TYPE) + lq := NewQueue(observer.DATA_QUEUE, 5) - observer.Notify(typeEvent, "data") + observer.Notify(observer.DATA_QUEUE.String(), "data") re := lq.GetItems()