diff --git a/event/events.go b/event/events.go index 016a17c..eae2d40 100644 --- a/event/events.go +++ b/event/events.go @@ -1,34 +1,39 @@ // Copyright (c) 2014, B3log -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// Event manipulations. +// Package event includes event related manipulations. package event import "github.com/golang/glog" const ( - EvtCodeGOPATHNotFound = iota // event code: not found $GOPATH env variable - EvtCodeGOROOTNotFound // event code: not found $GOROOT env variable - EvtCodeGocodeNotFound // event code: not found gocode - EvtCodeIDEStubNotFound // event code: not found ide_stub - EvtCodeServerInternalError // event code: server internal error + // EvtCodeGOPATHNotFound indicates an event: not found $GOPATH env variable + EvtCodeGOPATHNotFound = iota + // EvtCodeGOROOTNotFound indicates an event: not found $GOROOT env variable + EvtCodeGOROOTNotFound + // EvtCodeGocodeNotFound indicates an event: not found gocode + EvtCodeGocodeNotFound + // EvtCodeIDEStubNotFound indicates an event: not found ide_stub + EvtCodeIDEStubNotFound + // EvtCodeServerInternalError indicates an event: server internal error + EvtCodeServerInternalError ) // Max length of queue. -const MaxQueueLength = 10 +const maxQueueLength = 10 -// Event. +// Event represents an event. type Event struct { Code int `json:"code"` // event code Sid string `json:"sid"` // wide session id related @@ -38,21 +43,21 @@ type Event struct { // Global event queue. // // Every event in this queue will be dispatched to each user event queue. -var EventQueue = make(chan *Event, MaxQueueLength) +var EventQueue = make(chan *Event, maxQueueLength) -// User event queue. +// UserEventQueue represents a user event queue. type UserEventQueue struct { Sid string // wide session id related Queue chan *Event // queue Handlers []Handler // event handlers } -type Queues map[string]*UserEventQueue +type queues map[string]*UserEventQueue // User event queues. // // -var UserEventQueues = Queues{} +var UserEventQueues = queues{} // Load initializes the event handling. func Load() { @@ -78,7 +83,7 @@ func (uq *UserEventQueue) AddHandler(handlers ...Handler) { } // New initializes a user event queue with the specified wide session id. -func (ueqs Queues) New(sid string) *UserEventQueue { +func (ueqs queues) New(sid string) *UserEventQueue { q := ueqs[sid] if nil != q { glog.Warningf("Already exist a user queue in session [%s]", sid) @@ -88,14 +93,14 @@ func (ueqs Queues) New(sid string) *UserEventQueue { q = &UserEventQueue{ Sid: sid, - Queue: make(chan *Event, MaxQueueLength), + Queue: make(chan *Event, maxQueueLength), } ueqs[sid] = q go func() { // start listening for evt := range q.Queue { - glog.V(5).Infof("Session [%s] received a event [%d]", sid, evt.Code) + glog.V(5).Infof("Session [%s] received an event [%d]", sid, evt.Code) // process event by each handlers for _, handler := range q.Handlers { @@ -108,7 +113,7 @@ func (ueqs Queues) New(sid string) *UserEventQueue { } // Close closes a user event queue with the specified wide session id. -func (ueqs Queues) Close(sid string) { +func (ueqs queues) Close(sid string) { q := ueqs[sid] if nil == q { return @@ -117,15 +122,15 @@ func (ueqs Queues) Close(sid string) { delete(ueqs, sid) } -// Type of event handler. +// Handler represents an event handler. type Handler interface { Handle(event *Event) } // Type of handler function. -type HandleFunc func(event *Event) +type handleFunc func(event *Event) // Default implementation of event handling. -func (fn HandleFunc) Handle(event *Event) { +func (fn handleFunc) Handle(event *Event) { fn(event) }